-
Notifications
You must be signed in to change notification settings - Fork 42
/
udp.go
54 lines (46 loc) · 1.34 KB
/
udp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package generate
import (
"errors"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/pcapx"
"github.com/yaklang/yaklang/common/suricata/data/modifier"
"github.com/yaklang/yaklang/common/suricata/rule"
)
var _ Generator = (*UDPGen)(nil)
type UDPGen struct {
r *rule.Rule
payload ModifierGenerator
}
func newUDPGen(r *rule.Rule) (Generator, error) {
if r.ContentRuleConfig == nil {
return nil, errors.New("empty content rule config")
}
g := &UDPGen{
r: r,
}
for mdf, r := range contentRuleMap(r.ContentRuleConfig.ContentRules) {
switch mdf {
case modifier.UDPHDR:
// 暂不支持
case modifier.Default:
g.payload = parse2ContentGen(r, WithNoise(noiseAll))
default:
log.Warnf("not support modifier %v", mdf)
}
}
return g, nil
}
func (g *UDPGen) Gen() []byte {
var opts []any
opts = append(opts, pcapx.WithIPv4_SrcIP(g.r.SourceAddress.Generate()))
opts = append(opts, pcapx.WithIPv4_DstIP(g.r.DestinationAddress.Generate()))
opts = append(opts, pcapx.WithUDP_SrcPort(uint16(g.r.SourcePort.GetAvailablePort())))
opts = append(opts, pcapx.WithUDP_DstPort(uint16(g.r.DestinationPort.GetAvailablePort())))
opts = append(opts, pcapx.WithPayload(g.payload.Gen()))
raw, err := pcapx.PacketBuilder(opts...)
if err != nil {
log.Errorf("generate udp packet failed: %s", err)
return nil
}
return raw
}