forked from cloudflare/bpftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex2pcap
executable file
·72 lines (56 loc) · 1.58 KB
/
hex2pcap
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import getopt
import os
import pcappy
import sys
def usage():
print """
hex2pcap.py [ OPTIONS ] [ hex file... ]
Read hex packets from stdin or given files and print a pcap file to
stdout. Input lines starting with hash or semicolon are ignored, only
part of a line before tab character is parsed as hex.
Options are:
-h, --help print this message
""".lstrip()
sys.exit(2)
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError as err:
print str(err)
usage()
for o, a in opts:
if o in ("-h", "--help"):
usage()
else:
assert False, "unhandled option"
if not args:
readfds = [sys.stdin]
else:
readfds = [open(fname, 'rb') for fname in args]
dump = pcappy.PcapPyDead(snaplen=65536).dump_open(sys.stdout)
hdr = {'ts':{'tv_sec':0, 'tv_usec':0}}
for fd in readfds:
for line in fd:
line = line.lstrip()
if not line or line[0] in '#;':
continue
h, _, _ = line.partition("\t")
if not h:
continue
raw = h.rstrip().decode('hex')
hdr['caplen'] = hdr['len'] = len(raw)
dump.write(hdr, raw)
sys.stdout.flush()
dump.flush()
# normal exit crashes due to a double free error in pcappy
os._exit(0)
if __name__ == "__main__":
try:
main()
except IOError, e:
if e.errno == 32:
os._exit(-1)
raise
except KeyboardInterrupt:
os._exit(-1)