Skip to content

Commit

Permalink
Add local ip template
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 1, 2022
1 parent de8898f commit bffe95b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Almost every leaf `[string]` or `[object]` parameter can be templated with go te
- `random_payload`
- `random_mac_addr`
- `random_port`
- `local_ip`
- `base64_encode`
- `base64_decode`

Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ func parseStringTemplate(input string) string {
"random_ip": packetgen.RandomIP,
"random_port": packetgen.RandomPort,
"random_mac_addr": packetgen.RandomMacAddr,
"local_ip": packetgen.LocalIP,
"base64_encode": base64.StdEncoding.EncodeToString,
"base64_decode": base64.StdEncoding.DecodeString,
"json_encode": json.Marshal,
"json_decode": json.Unmarshal,
}
// TODO: consider adding ability to populate custom data
tmpl, err := template.New("test").Funcs(funcMap).Parse(input)
Expand Down Expand Up @@ -340,12 +342,19 @@ func packetgenJob(ctx context.Context, l *logs.Logger, args JobArgs) error {

for jobConfig.Next(ctx) {
packetConfigBytes := parseByteTemplate(jobConfig.Packet)
l.Debug("[packetgen] parsed packet config template:\n%s", string(packetConfigBytes))
var packetConfig packetgen.PacketConfig
err := json.Unmarshal(packetConfigBytes, &packetConfig)
if err != nil {
l.Error("error parsing json: %v", err)
return err
}
packetConfigBytes, err = json.Marshal(packetConfig)
if err != nil {
l.Error("error marshaling back to json: %v", err)
return err
}
l.Debug("[packetgen] parsed packet config:\n%v", string(packetConfigBytes))
len, err := packetgen.SendPacket(packetConfig, host, port)
if err != nil {
l.Error("error sending packet: %v", err)
Expand Down
17 changes: 17 additions & 0 deletions packetgen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ func RandomMacAddr() net.HardwareAddr {
return net.HardwareAddr(addr.String())
}

// GetLocalIP returns the non loopback local IP of the host
func LocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}

// getIps returns a string slice to spoof ip packets with dummy source ip addresses
func getIps() []string {
ips := make([]string, 0)
Expand Down
7 changes: 5 additions & 2 deletions testconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@
"dst_mac": "{{ random_mac_addr }}"
},
"ip": {
"src_ip": "{{ random_ip }}",
"src_ip": "{{ local_ip }}",
"dst_ip": "{{ random_ip }}"
},
"tcp": {
"src_port": "{{ random_port }}",
"dst_port": "{{ random_port }}"
"dst_port": "{{ random_port }}",
"flags": {
"syn": true
}
},
"udp": {
"src_port": "{{ random_port }}",
Expand Down

0 comments on commit bffe95b

Please sign in to comment.