Skip to content

Commit f0d48fc

Browse files
ipv4 identification randomization, changed ttl and flags when creating war ipv4 packets
1 parent 9261160 commit f0d48fc

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

layers/ipv4.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ func NewIPv4Packet(srcIP, dstIP netip.Addr, proto IPProto, payload []byte) (*IPv
7373
return nil, fmt.Errorf("malformed IPv4 address")
7474
}
7575
ipPacket := &IPv4Packet{
76-
Version: 4,
77-
IHL: 5,
78-
TotalLength: uint16(headerSizeIPv4 + len(payload)),
79-
Flags: NewIPv4Flags(0),
80-
TTL: 64,
81-
Protocol: &IPv4Proto{Val: proto, Desc: protodesc(proto)},
82-
SrcIP: srcIP,
83-
DstIP: dstIP,
84-
Payload: payload,
76+
Version: 4,
77+
IHL: 5,
78+
TotalLength: uint16(headerSizeIPv4 + len(payload)),
79+
Identification: MustGenerateRandomUint16NE(),
80+
Flags: NewIPv4Flags(2),
81+
TTL: 128,
82+
Protocol: &IPv4Proto{Val: proto, Desc: protodesc(proto)},
83+
SrcIP: srcIP,
84+
DstIP: dstIP,
85+
Payload: payload,
8586
}
8687
headerChecksum, err := CalculateIPv4Checksum(ipPacket.ToBytes())
8788
if err != nil {

layers/layers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package layers
33

44
import (
55
"bytes"
6+
"crypto/rand"
67
"encoding/binary"
78
"fmt"
89
"unsafe"
10+
11+
"github.com/shadowy-pycoder/mshark/native"
912
)
1013

1114
const maxLenSummary = 110
@@ -273,3 +276,45 @@ func isDigit(b byte) bool {
273276
func isUpper(b byte) bool {
274277
return b >= 'A' && b <= 'Z'
275278
}
279+
280+
func GenerateRandomBytes(n int) ([]byte, error) {
281+
b := make([]byte, n)
282+
_, err := rand.Read(b)
283+
if err != nil {
284+
return nil, err
285+
}
286+
287+
return b, nil
288+
}
289+
290+
func GenerateRandomUint16LE() (uint16, error) {
291+
b, err := GenerateRandomBytes(2)
292+
if err != nil {
293+
return 0, err
294+
}
295+
return binary.LittleEndian.Uint16(b), nil
296+
}
297+
298+
func GenerateRandomUint16BE() (uint16, error) {
299+
b, err := GenerateRandomBytes(2)
300+
if err != nil {
301+
return 0, err
302+
}
303+
return binary.BigEndian.Uint16(b), nil
304+
}
305+
306+
func GenerateRandomUint16NE() (uint16, error) {
307+
b, err := GenerateRandomBytes(2)
308+
if err != nil {
309+
return 0, err
310+
}
311+
return native.Endian.Uint16(b), nil
312+
}
313+
314+
func MustGenerateRandomUint16NE() uint16 {
315+
rn, err := GenerateRandomUint16NE()
316+
if err != nil {
317+
panic(err)
318+
}
319+
return rn
320+
}

0 commit comments

Comments
 (0)