SIP and RTP: Voice over IP
A phone call over IP uses two protocols. SIP (RFC 3261) sets the call up: it registers phones, rings the other side, negotiates codecs and ports, and hangs up. RTP (RFC 3550) carries the audio and video itself, as a stream of small UDP packets. Both were deployed for years without encryption, and SIP servers exposed to the Internet attract constant scanning, because a hijacked extension can be used to place expensive international calls at the owner's cost (toll fraud). This lesson reads SIP and RTP from bytes, spots a registration scan in a synthetic capture, and covers the protections: TLS for signalling, SRTP for media, and not exposing the PBX in the first place.
SIP: text, like HTTP
SIP messages look like HTTP: a request line or status line, headers, a blank line, a body. The body of an INVITE is usually SDP (RFC 8866), which says where to send the media. The program parses a synthetic INVITE between two lab phones and extracts what a firewall (and an eavesdropper) needs: who calls whom, and which IP address and UDP port the audio will use.
invite = """INVITE sip:102@lab.test SIP/2.0
Via: SIP/2.0/UDP 10.10.0.5:5060;branch=z9hG4bK1a2b
From: "Alice" <sip:101@lab.test>;tag=88a1
To: <sip:102@lab.test>
Call-ID: 4f2e9c@10.10.0.5
CSeq: 1 INVITE
Contact: <sip:101@10.10.0.5:5060>
Content-Type: application/sdp
Content-Length: 156
v=0
o=101 1 1 IN IP4 10.10.0.5
s=call
c=IN IP4 10.10.0.5
t=0 0
m=audio 16384 RTP/AVP 0 8 101
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
""".replace("\n", "\r\n")
head, _, sdp = invite.partition("\r\n\r\n")
lines = head.split("\r\n")
headers = dict(l.split(": ", 1) for l in lines[1:])
print("request:", lines[0])
print("caller :", headers["From"], "\ncallee :", headers["To"])
media = {}
for l in sdp.split("\r\n"):
if l.startswith("c="):
media["ip"] = l.split()[-1]
if l.startswith("m=audio"):
_, port, proto, *fmts = l.split()
media.update(port=int(port), proto=proto, codecs=fmts)
print("media :", media)
print("encrypted media?", "yes (SRTP)" if "SAVP" in media["proto"] else "no: plain RTP, audio readable by anyone on the path")request: INVITE sip:102@lab.test SIP/2.0
caller : "Alice" <sip:101@lab.test>;tag=88a1
callee : <sip:102@lab.test>
media : {'ip': '10.10.0.5', 'port': 16384, 'proto': 'RTP/AVP', 'codecs': ['0', '8', '101']}
encrypted media? no: plain RTP, audio readable by anyone on the path
RTP/AVP with codec 0 (PCMU, G.711 µ-law) means unencrypted audio at 8,000 samples per second; RTP/SAVP would mean SRTP. Because the media ports are negotiated inside SIP, firewalls and NAT devices need SIP helpers (ALGs) or a session border controller to open them, which is a source of both breakage and vulnerabilities.
RTP: a 12-byte header and the audio
import struct
# RTP header of a synthetic G.711 packet (then 160 bytes of audio = 20 ms)
hdr = bytes.fromhex("800001f4000001401234abcd")
b0, b1, seq, ts, ssrc = struct.unpack("!BBHII", hdr)
print(f"version {b0 >> 6}, padding {(b0 >> 5) & 1}, extension {(b0 >> 4) & 1}, CSRC count {b0 & 15}")
print(f"marker {b1 >> 7}, payload type {b1 & 127} (0 = PCMU), sequence {seq}, timestamp {ts}, SSRC 0x{ssrc:08x}")
print("timestamp step per 20 ms packet at 8 kHz:", 8000 * 20 // 1000)version 2, padding 0, extension 0, CSRC count 0
marker 0, payload type 0 (0 = PCMU), sequence 500, timestamp 320, SSRC 0x1234abcd
timestamp step per 20 ms packet at 8 kHz: 160
Sequence numbers and timestamps let the receiver reorder packets and play them at the right time; they also let anyone with a capture reassemble the call. Wireshark's "Telephony → RTP → Stream analysis" and "Play streams" do exactly that, which is why unencrypted RTP is a confidentiality finding.
A registration scan in a capture
Internet-facing SIP servers receive a steady stream of REGISTER and OPTIONS probes that walk through extension numbers and passwords. The session writes a synthetic capture of such a scan against a lab PBX (10.10.0.60) from a documentation address, plus a few RTP packets of a normal internal call, then summarises it with tshark.
python3 - <<'PY'
from scapy.all import Ether, IP, UDP, Raw, wrpcap
from scapy.layers.rtp import RTP
t0, pkts = 1768471200.0, []
EDGE, PBX, PH1, PH2 = "02:00:00:0a:0a:01", "02:00:00:0a:0a:3c", "02:00:00:0a:0a:05", "02:00:00:0a:0a:06"
def add(p, dt):
p.time = t0 + dt; pkts.append(p)
for i, ext in enumerate([100, 101, 102, 103, 104]):
req = (f"REGISTER sip:lab.test SIP/2.0\r\nVia: SIP/2.0/UDP 203.0.113.9:5070;branch=z9hG4bK{i}\r\n"
f"From: <sip:{ext}@lab.test>;tag={i}\r\nTo: <sip:{ext}@lab.test>\r\nCall-ID: scan{i}@203.0.113.9\r\n"
f"CSeq: 1 REGISTER\r\nUser-Agent: lab-scanner\r\nContent-Length: 0\r\n\r\n")
add(Ether(src=EDGE, dst=PBX)/IP(src="203.0.113.9", dst="10.10.0.60")/UDP(sport=5070, dport=5060)/Raw(req.encode()), i * 0.05)
resp = (f"SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 203.0.113.9:5070;branch=z9hG4bK{i}\r\n"
f"From: <sip:{ext}@lab.test>;tag={i}\r\nTo: <sip:{ext}@lab.test>;tag=srv\r\nCall-ID: scan{i}@203.0.113.9\r\n"
f"CSeq: 1 REGISTER\r\nWWW-Authenticate: Digest realm=\"lab.test\", nonce=\"n{i}\"\r\nContent-Length: 0\r\n\r\n")
add(Ether(src=PBX, dst=EDGE)/IP(src="10.10.0.60", dst="203.0.113.9")/UDP(sport=5060, dport=5070)/Raw(resp.encode()), i * 0.05 + 0.01)
for s in range(3):
add(Ether(src=PH1, dst=PH2)/IP(src="10.10.0.5", dst="10.10.0.6")/UDP(sport=16384, dport=16386)
/RTP(version=2, payload_type=0, sequence=500 + s, timestamp=160 * s, sourcesync=0x1234abcd)/Raw(b"\xff" * 160), 1 + s * 0.02)
wrpcap("voip.pcap", pkts)
PY
echo "SIP requests by source, method and user agent:"
tshark -r voip.pcap -Y 'sip.Method' -T fields -e ip.src -e sip.Method -e sip.User-Agent | sort | uniq -c
echo "extensions probed: $(tshark -r voip.pcap -Y 'sip.Method == "REGISTER"' -T fields -e sip.from.user | sort -u | tr '\n' ' ')"
echo -n "401 responses: "; tshark -r voip.pcap -Y 'sip.Status-Code == 401' | wc -l
echo "RTP (decoded on port 16384):"
tshark -r voip.pcap -d udp.port==16384,rtp -Y rtp -T fields -e ip.src -e ip.dst -e rtp.p_type -e rtp.seq -e rtp.timestampSIP requests by source, method and user agent:
5 203.0.113.9 REGISTER lab-scanner
extensions probed: 100 101 102 103 104
401 responses: 5
RTP (decoded on port 16384):
10.10.0.5 10.10.0.6 0 500 0
10.10.0.5 10.10.0.6 0 501 160
10.10.0.5 10.10.0.6 0 502 320
Five consecutive extensions from one external source in a quarter of a second, each answered with 401 Unauthorized (a digest challenge): a classic enumeration. Real scanners send thousands; the next step would be password guessing against the extensions that exist.
How VoIP abuse is detected
- SIP from the Internet to a PBX that has no external users; many REGISTER/OPTIONS/INVITE requests from one source; sequential extensions; bursts of 401/403/404.
- Known scanner user agents (many tools announce themselves; attackers can change the string, so don't rely on it alone).
- Toll-fraud indicators: calls to premium or international ranges at night, many simultaneous calls from one extension, registrations from new countries.
- Unencrypted media:
RTP/AVPin SDP and RTP flows in network monitoring where policy requires encryption.
How it is protected
- Don't expose the PBX. Remote phones connect through a VPN or a session border controller (SBC) that filters, rate-limits and hides the internal servers.
- Encrypt: SIP over TLS (
sips:, TCP 5061) and SRTP for media (keys exchanged with SDES inside TLS-protected SIP, or DTLS-SRTP as WebRTC does). - Strong per-extension passwords, rate limiting and temporary bans on failed registrations (fail2ban and similar).
- Dial-plan restrictions: block premium and unneeded international destinations, per-extension call limits, alerts on cost spikes.
- Separate voice VLAN for phones, so a compromised PC can't sniff calls on the same broadcast domain (VLANs).
Pitfalls
- Encrypting SIP with TLS but leaving media as plain RTP: the call content is still readable.
- Relying on the firewall's SIP ALG. It often breaks calls and has been the source of NAT-slipstreaming-style bypasses; most deployments disable it and use an SBC.
- Default or numeric passwords equal to the extension number on new phones.
Practice
1. A G.711 call sends one RTP packet every 20 ms. How many packets per second per direction, and how big is the audio payload?
50 packets per second; 8,000 samples/s × 1 byte × 0.02 s = 160 bytes of audio per packet (plus 12 bytes RTP, 8 UDP, 20 IP headers).
2. Why do the 401 responses help an attacker enumerate extensions?
If the PBX answers 401 (challenge) for existing extensions and 404 for non-existent ones, the difference reveals valid numbers. A hardened PBX answers the same way for both.
3. Change the SDP so the call uses SRTP. What changes in the media line?
m=audio 16384 RTP/SAVP 0 8 101 (or UDP/TLS/RTP/SAVPF with DTLS-SRTP) plus a key line such as a=crypto:… (SDES) or a=fingerprint:… (DTLS). The parser then prints "yes (SRTP)".
Test yourself
Which protocol carries the audio of a VoIP call?
- SIP
- RTP
- SDP
- DNS
What does m=audio 16384 RTP/AVP 0 in SDP tell an observer?
- The call is encrypted
- Audio goes to UDP port 16384 as plain RTP with codec 0 (PCMU)
- The call uses TCP
- The caller's password
In the capture, what does the sequence of REGISTER requests for extensions 100-104 from 203.0.113.9 indicate?
- Normal phone registration
- An extension enumeration scan
- A DNS lookup
- An RTP stream
Which measure protects call content from eavesdropping?
- SIP over TLS alone
- SRTP for the media
- A longer extension number
- UDP port 5060