Skip to content

Commit 689409c

Browse files
Merge pull request #465 from step-security/rp/cherry/udp
feat: enhance network monitoring for UDP packets
2 parents a33c48b + a6d8f78 commit 689409c

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

firewall.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func addBlockRules(firewall *Firewall, endpoints []ipAddressEndpoint, chain, net
152152
return errors.Wrap(err, "failed to add rule")
153153
}
154154

155+
// Log blocked traffic - UDP packets
156+
err = ipt.Append(filterTable, chain, direction, netInterface, protocol, "udp", "-j", "NFLOG", "--nflog-group", "100")
157+
158+
if err != nil {
159+
return errors.Wrap(err, "failed to add UDP NFLOG rule")
160+
}
161+
155162
// Block all other traffic
156163
err = ipt.Append(filterTable, chain, direction, netInterface, protocol, allProtocols, target, reject)
157164

netmon.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ func (netMonitor *NetworkMonitor) handlePacket(attrs nflog.Attribute) {
7171
packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default)
7272
port := ""
7373
isSYN := false
74+
isUDP := false
7475
// Get the TCP layer from this packet
7576
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
7677
// Get actual TCP data from this layer
7778
tcp, _ := tcpLayer.(*layers.TCP)
7879
port = tcp.DstPort.String()
7980
isSYN = tcp.SYN
8081

82+
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil {
83+
// Get actual UDP data from this layer
84+
udp, _ := udpLayer.(*layers.UDP)
85+
port = udp.DstPort.String()
86+
isUDP = true
8187
}
8288

8389
// Get the IP layer from this packet
@@ -90,7 +96,7 @@ func (netMonitor *NetworkMonitor) handlePacket(attrs nflog.Attribute) {
9096
if !found {
9197
ipAddresses[ipv4Address] = 1
9298

93-
if isSYN {
99+
if isSYN || isUDP {
94100
if netMonitor.Status == "Dropped" {
95101

96102
netMonitor.ApiClient.sendNetConnection(netMonitor.CorrelationId, netMonitor.Repo,

procmon_linux.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,19 @@ func (p *ProcessMonitor) MonitorProcesses(errc chan error) {
107107
errc <- errors.Wrap(err, "failed to add audit rule for syscall connect")
108108
}
109109

110-
WriteLog("Net monitor added")
110+
WriteLog("Net monitor added for TCP (connect)")
111+
112+
// syscall sendto (for UDP)
113+
r, _ = flags.Parse(fmt.Sprintf("-a exit,always -S sendto -S sendmsg -k %s", netMonitorTag))
114+
115+
actualBytes, _ = rule.Build(r)
116+
117+
if err = client.AddRule(actualBytes); err != nil {
118+
WriteLog(fmt.Sprintf("failed to add audit rule for sendto %v", err))
119+
errc <- errors.Wrap(err, "failed to add audit rule for syscall sendto")
120+
}
121+
122+
WriteLog("Net monitor added for UDP (sendto & sendmsg)")
111123

112124
// syscall process start
113125
r, _ = flags.Parse(fmt.Sprintf("-a exit,always -S execve -k %s", processMonitorTag))

0 commit comments

Comments
 (0)