Skip to content

Commit b553f73

Browse files
committed
Custom SIP portrange, some error checks
1 parent 7c328ef commit b553f73

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

build_static.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/sh
22

3+
# Thanks for the idea to use musl!
4+
# https://www.moiji-mobile.com/2017/10/15/static-binaries-for-go-with-docker/
5+
36
set -ex
47

58
apk update

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type InterfacesConfig struct {
2121
Type string `config:"type"`
2222
ReadFile string `config:"read_file"`
2323
WriteFile string `config:"write_file"`
24+
PortRange string `config:"port_range"`
2425
Snaplen int `config:"snaplen"`
2526
BufferSizeMb int `config:"buffer_size_mb"`
2627
ReadSpeed bool `config:"top_speed"`

example/rtp_rtcp_sip.pcap

902 KB
Binary file not shown.

main.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ func parseFlags() {
2525
var keepLogFiles int
2626

2727
flag.StringVar(&ifaceConfig.Device, "i", "", "Listen on interface")
28-
flag.StringVar(&ifaceConfig.Type, "t", "pcap", "Capture types are [af_packet, pcap, file]")
29-
flag.StringVar(&ifaceConfig.ReadFile, "rf", "", "Read packets from file. Please use -t file")
30-
flag.StringVar(&ifaceConfig.WriteFile, "wf", "", "Write packets to file")
31-
flag.IntVar(&ifaceConfig.Loop, "lp", 1, "Loop count over ReadFile")
32-
flag.BoolVar(&ifaceConfig.ReadSpeed, "rs", false, "Maximum read speed. Doesn't use packet timestamps")
33-
flag.IntVar(&ifaceConfig.Snaplen, "s", 32768, "Snap length")
28+
flag.StringVar(&ifaceConfig.Type, "t", "pcap", "Capture types are [pcap, af_packet]")
29+
flag.StringVar(&ifaceConfig.ReadFile, "rf", "", "Read packets from pcap file")
30+
flag.StringVar(&ifaceConfig.WriteFile, "wf", "", "Write packets to pcap file")
31+
flag.IntVar(&ifaceConfig.Loop, "lp", 1, "Loop count over ReadFile. Use 0 to loop forever")
32+
flag.BoolVar(&ifaceConfig.ReadSpeed, "rs", false, "Maximum pcap read speed. Doesn't use packet timestamps")
33+
flag.IntVar(&ifaceConfig.Snaplen, "s", 32768, "Snaplength")
34+
flag.StringVar(&ifaceConfig.PortRange, "pr", "5060-5090", "Portrange to capture SIP")
3435
flag.IntVar(&ifaceConfig.BufferSizeMb, "b", 64, "Interface buffersize (MB)")
3536
flag.IntVar(&keepLogFiles, "kl", 4, "Rotate the number of log files")
3637
flag.StringVar(&logging.Level, "l", "info", "Log level [debug, info, warning, error]")
3738
flag.BoolVar(&ifaceConfig.OneAtATime, "o", false, "Read packet for packet")
3839
flag.StringVar(&fileRotator.Path, "p", "./", "Log filepath")
3940
flag.StringVar(&fileRotator.Name, "n", "heplify.log", "Log filename")
4041
flag.Uint64Var(&rotateEveryKB, "r", 16384, "Log filesize (KB)")
41-
flag.StringVar(&config.Cfg.Mode, "m", "SIP", "Capture modes [DNS, LOG, SIP, RTCP, TLS]")
42+
flag.StringVar(&config.Cfg.Mode, "m", "SIP", "Capture modes [DNS, LOG, SIP, SIPRTCP, TLS]")
4243
flag.BoolVar(&config.Cfg.Dedup, "dd", true, "Deduplicate packets")
43-
flag.StringVar(&config.Cfg.Filter, "fi", "", "Filter out interesting packets like SIP INVITES, Handshakes ...")
44-
flag.StringVar(&config.Cfg.Discard, "di", "", "Discard uninteresting packets like SIP OPTIONS, HTTP Requests ...")
44+
flag.StringVar(&config.Cfg.Filter, "fi", "", "Filter interesting packets")
45+
flag.StringVar(&config.Cfg.Discard, "di", "", "Discard uninteresting packets")
4546
flag.StringVar(&config.Cfg.HepServer, "hs", "127.0.0.1:9060", "HEP Server address")
4647
flag.Parse()
4748

protos/rtcp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func ParseRTCP(data []byte) ([]byte, error) {
183183
offset := 0
184184

185185
for dataLen > 0 {
186-
if dataLen%4 != 0 || dataLen < 4 {
186+
if dataLen < 4 || dataLen > 576 {
187187
return nil, fmt.Errorf("Fishy RTCP packet=%v length=%d", data, dataLen)
188188
}
189189

sniffer/sniffer.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func (sniffer *SnifferSetup) setFromConfig(cfg *config.InterfacesConfig) error {
9595

9696
switch sniffer.mode {
9797
case "SIP":
98-
sniffer.filter = "(greater 256 and portrange 5060-5090 or ip[6:2] & 0x1fff != 0) or (vlan and (greater 256 and portrange 5060-5090 or ip[6:2] & 0x1fff != 0))"
98+
sniffer.filter = "(greater 256 and portrange " + sniffer.config.PortRange + " or ip[6:2] & 0x1fff != 0) or (vlan and (greater 256 and portrange " + sniffer.config.PortRange + " or ip[6:2] & 0x1fff != 0))"
9999
case "RTCP":
100100
sniffer.filter = "(ip and ip[6] & 0x2 = 0 and ip[6:2] & 0x1fff = 0 and udp and udp[8] & 0xc0 = 0x80 and udp[9] >= 0xc8 && udp[9] <= 0xcc)"
101101
case "SIPRTCP":
102-
sniffer.filter = "(greater 256 and portrange 5060-5090 or ip[6:2] & 0x1fff != 0) or (ip and ip[6] & 0x2 = 0 and ip[6:2] & 0x1fff = 0 and udp and udp[8] & 0xc0 = 0x80 and udp[9] >= 0xc8 && udp[9] <= 0xcc)"
102+
sniffer.filter = "(greater 256 and portrange " + sniffer.config.PortRange + " or ip[6:2] & 0x1fff != 0) or (ip and ip[6] & 0x2 = 0 and ip[6:2] & 0x1fff = 0 and udp and udp[8] & 0xc0 = 0x80 and udp[9] >= 0xc8 && udp[9] <= 0xcc)"
103103
case "LOG":
104104
sniffer.filter = "greater 128 and port 514"
105105
case "DNS":
@@ -108,7 +108,7 @@ func (sniffer *SnifferSetup) setFromConfig(cfg *config.InterfacesConfig) error {
108108
sniffer.filter = "tcp and port 443 and tcp[(((tcp[12:1] & 0xf0) >> 2)):1] = 0x16 and ((tcp[(((tcp[12:1] & 0xf0) >> 2)+5):1] = 0x01) or (tcp[(((tcp[12:1] & 0xf0) >> 2)+5):1] = 0x02))"
109109
default:
110110
sniffer.mode = "SIP"
111-
sniffer.filter = "(greater 256 and portrange 5060-5090 or ip[6:2] & 0x1fff != 0) or (vlan and (greater 256 and portrange 5060-5090 or ip[6:2] & 0x1fff != 0))"
111+
sniffer.filter = "(greater 256 and portrange " + sniffer.config.PortRange + " or ip[6:2] & 0x1fff != 0) or (vlan and (greater 256 and portrange " + sniffer.config.PortRange + " or ip[6:2] & 0x1fff != 0))"
112112
}
113113

114114
logp.Info("Sniffer type: [%s] device: [%s] mode: [%s]", sniffer.config.Type, sniffer.config.Device, sniffer.mode)
@@ -167,13 +167,17 @@ func (sniffer *SnifferSetup) Init(testMode bool, mode string, factory WorkerFact
167167
sniffer.mode = mode
168168

169169
if interfaces.Device == "" && interfaces.ReadFile == "" {
170-
fmt.Printf("\nPlease use one of the following devices:\n\n")
170+
fmt.Printf("Please use one of the following devices:\n\n")
171171
_, err := ListDeviceNames(false, false)
172172
if err != nil {
173173
return fmt.Errorf("getting devices list: %v", err)
174174
}
175175
fmt.Println("")
176176
os.Exit(1)
177+
} else if interfaces.Device == "any" && interfaces.Type == "pcap" {
178+
fmt.Println("Interface 'any' and capture type 'pcap' will break VLAN capture!")
179+
fmt.Println("To listen on interface 'any' please use 'af_packet' capture type!")
180+
os.Exit(1)
177181
}
178182

179183
if !testMode {

0 commit comments

Comments
 (0)