Skip to content

Commit 9261160

Browse files
added more checks to eliminate false positives while parsing packets
1 parent 0d51cf8 commit 9261160

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

layers/ftp.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ func (f *FTPMessage) Parse(data []byte) error {
2626
if !checkFTP(buf) {
2727
return fmt.Errorf("malformed ftp message")
2828
}
29-
f.summary = nil
30-
f.data = nil
3129
sp := bytes.Split(buf, crlf)
3230
lsp := len(sp)
3331
switch {
3432
case lsp > 2:
35-
f.summary = bytes.Join(sp[:2], bspace)
33+
f.summary = bytes.TrimSpace(bytes.Join(sp[:2], bspace))
3634
sp[0] = joinBytes(dash, sp[0])
37-
f.data = bytes.TrimSuffix(bytes.TrimSuffix(bytes.Join(sp, lfd), dash), lf)
35+
f.data = bytes.TrimSpace(bytes.TrimSuffix(bytes.TrimSuffix(bytes.Join(sp, lfd), dash), lf))
3836
case lsp > 1:
39-
f.summary = sp[0]
37+
f.summary = bytes.TrimSpace(sp[0])
4038
sp[0] = joinBytes(dash, sp[0])
41-
f.data = bytes.TrimSuffix(bytes.TrimSuffix(bytes.Join(sp, lfd), dash), lf)
39+
f.data = bytes.TrimSpace(bytes.TrimSuffix(bytes.TrimSuffix(bytes.Join(sp, lfd), dash), lf))
4240
default:
41+
return fmt.Errorf("failed parsing FTP message")
42+
}
43+
if len(f.summary) == 0 || len(f.data) == 0 {
44+
return fmt.Errorf("failed parsing FTP message")
4345
}
4446
return nil
4547
}

layers/icmp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func (i *ICMPSegment) Parse(data []byte) error {
6565
return fmt.Errorf("minimum payload length for ICMP with type %d is %d bytes", i.Type, pLen)
6666
}
6767
i.TypeDesc, i.CodeDesc = i.typecode()
68+
if i.TypeDesc == "Unknown" || i.CodeDesc == "Unknown" {
69+
return fmt.Errorf("failed determining type or code")
70+
}
6871
return nil
6972
}
7073

layers/ipv4.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,14 @@ func (p *IPv4Packet) UnmarshalBinary(data []byte) error {
170170
dscpECN := buf[1]
171171
p.DSCP = dscpECN >> 2
172172
p.DSCPDesc = dscpdesc(p.DSCP)
173+
if p.DSCPDesc == "Unknown" {
174+
return fmt.Errorf("unknown DSCP")
175+
}
173176
p.ECN = dscpECN & 3
174177
p.TotalLength = binary.BigEndian.Uint16(buf[2:4])
178+
if int(p.TotalLength) != len(buf) {
179+
return fmt.Errorf("total length is not equal to actual packet size")
180+
}
175181
p.Identification = binary.BigEndian.Uint16(buf[4:6])
176182
flagsOffset := binary.BigEndian.Uint16(buf[6:8])
177183
flags := uint8(flagsOffset >> 13)

layers/ipv6.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type TrafficClass struct {
1515
ECN uint8
1616
}
1717

18-
func newTrafficiClass(tc uint8) *TrafficClass {
18+
func newTrafficClass(tc uint8) *TrafficClass {
1919
dscpbin := tc >> 2
2020
return &TrafficClass{
2121
Raw: tc,
@@ -87,7 +87,7 @@ func (p *IPv6Packet) Parse(data []byte) error {
8787
if p.Version != 6 {
8888
return fmt.Errorf("unknown version")
8989
}
90-
p.TrafficClass = newTrafficiClass(uint8((versionTrafficFlow >> 20) & 0xFF))
90+
p.TrafficClass = newTrafficClass(uint8((versionTrafficFlow >> 20) & 0xFF))
9191
if p.TrafficClass.DSCPDesc == "Unknown" {
9292
return fmt.Errorf("unknown DSCP")
9393
}
@@ -109,6 +109,9 @@ func (p *IPv6Packet) Parse(data []byte) error {
109109
return fmt.Errorf("malformed IPv6 address")
110110
}
111111
p.Payload = buf[headerSizeIPv6:]
112+
if p.PayloadLength != 0 && int(p.PayloadLength) != len(p.Payload) {
113+
return fmt.Errorf("payload length filed is not equal to actual payload size")
114+
}
112115
return nil
113116
}
114117

0 commit comments

Comments
 (0)