-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package pktmon | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cilium/cilium/api/v1/flow" | ||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
"github.com/microsoft/retina/pkg/utils" | ||
Check failure on line 9 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func (w *WinPktMon) getUnixTimestamp(StreamMetaData C.PACKETMONITOR_STREAM_METADATA_RETINA) int64 { | ||
Check failure on line 13 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
// use C conversion to pull Windows timestamp | ||
var timestampint C.longlong | ||
C.LargeIntegerToInt(StreamMetaData.TimeStamp, ×tampint) | ||
timestamp := int64(timestampint) | ||
|
||
// convert from windows to unix time | ||
var epochDifference int64 = 116444736000000000 | ||
return (timestamp - epochDifference) / 10000000 | ||
} | ||
|
||
func (w *WinPktMon) getVerdict(StreamMetaData C.PACKETMONITOR_STREAM_METADATA_RETINA) flow.Verdict { | ||
Check failure on line 24 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
if StreamMetaData.DropReason != 0 { | ||
return flow.Verdict_DROPPED | ||
} | ||
return flow.Verdict_FORWARDED | ||
} | ||
|
||
func (w *WinPktMon) parseL4(packet gopacket.Packet) (*layers.IPv4, error) { | ||
Check failure on line 31 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
ip := &layers.IPv4{} | ||
if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil { | ||
ip, _ = ipLayer.(*layers.IPv4) | ||
} else { | ||
return nil, fmt.Errorf("Failed to parse IP layer %w", ErrFailedToParseWithGoPacket) | ||
} | ||
return ip, nil | ||
} | ||
|
||
func (w *WinPktMon) parseTCP(packet gopacket.Packet) (*layers.TCP, error) { | ||
Check failure on line 41 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
tcp := &layers.TCP{} | ||
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { | ||
tcp, _ = tcpLayer.(*layers.TCP) | ||
} else { | ||
return nil, fmt.Errorf("Failed to parse TCP layer %w", ErrFailedToParseWithGoPacket) | ||
} | ||
return tcp, nil | ||
} | ||
|
||
func (w *WinPktMon) parseUDP(packet gopacket.Packet) (*layers.UDP, error) { | ||
Check failure on line 51 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
udp := &layers.UDP{} | ||
if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil { | ||
udp, _ = udpLayer.(*layers.UDP) | ||
} else { | ||
return nil, fmt.Errorf("Failed to parse UDP layer %w", ErrFailedToParseWithGoPacket) | ||
} | ||
return udp, nil | ||
} | ||
|
||
func (w *WinPktMon) parsePacket(buffer []byte, StreamMetaData C.PACKETMONITOR_STREAM_METADATA_RETINA) (gopacket.Packet, error) { | ||
Check failure on line 61 in pkg/plugin/windows/pktmon/pktmon_parsing_windows.go GitHub Actions / Lint (windows, arm64)
|
||
var packet gopacket.Packet | ||
// Ethernet | ||
if StreamMetaData.PacketType == 1 { | ||
packet = gopacket.NewPacket(buffer, layers.LayerTypeEthernet, gopacket.NoCopy) | ||
|
||
// IPv4 | ||
} else if StreamMetaData.PacketType == 3 { | ||
packet = gopacket.NewPacket(buffer, layers.LayerTypeIPv4, gopacket.NoCopy) | ||
} else { | ||
return nil, ErrUnknownPacketType | ||
} | ||
return packet, nil | ||
} | ||
|
||
func (w *WinPktMon) ParseDNS(fl *flow.Flow, metadata *utils.RetinaMetadata, packet gopacket.Packet) error { | ||
dns := &layers.DNS{} | ||
if dnsLayer := packet.Layer(layers.LayerTypeDNS); dnsLayer != nil { | ||
dns, _ = dnsLayer.(*layers.DNS) | ||
} else { | ||
return nil | ||
} | ||
|
||
if dns != nil { | ||
//fmt.Printf("qType %d\n", packet.dns.OpCode) | ||
var qtype string | ||
switch dns.OpCode { | ||
case layers.DNSOpCodeQuery: | ||
qtype = "Q" | ||
case layers.DNSOpCodeStatus: | ||
qtype = "R" | ||
default: | ||
qtype = "U" | ||
} | ||
|
||
var as, qs []string | ||
for _, a := range dns.Answers { | ||
if a.IP != nil { | ||
as = append(as, a.IP.String()) | ||
} | ||
} | ||
for _, q := range dns.Questions { | ||
qs = append(qs, string(q.Name)) | ||
} | ||
|
||
var query string | ||
if len(dns.Questions) > 0 { | ||
query = string(dns.Questions[0].Name[:]) | ||
} | ||
w.l.Debug("DNS packet", zap.String("query", query), zap.String("qtype", qtype), zap.String("answers", fmt.Sprintf("%v", as))) | ||
fl.Verdict = utils.Verdict_DNS | ||
utils.AddDNSInfo(fl, metadata, qtype, uint32(dns.ResponseCode), query, []string{qtype}, len(as), as) | ||
} | ||
|
||
return nil | ||
} |