forked from riverloopsec/killerbee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzbgoodfind
More file actions
228 lines (166 loc) · 5.25 KB
/
Copy pathzbgoodfind
File metadata and controls
228 lines (166 loc) · 5.25 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/python
from __future__ import print_function
import argparse
import os
import random
import signal
import sys
from killerbee import *
parser = argparse.ArgumentParser(
prog="zbgoodfind",
description="search a binary file to identify the encryption key for a given SNA or libpcap IEEE 802.15.4 encrypted packet",
epilog="Enjoy! - jwright@willhackforsushi.com",
)
parser.add_argument(
"-F",
"--skip-fcs",
action="store_false",
default=True,
help="Don't skip 2-byte FCS at end of each frame",
)
file_group = parser.add_mutually_exclusive_group(required=True)
file_group.add_argument("-R", "--daintree", help="Daintree SNA file path", type=str)
file_group.add_argument("-r", "--pcap", help="PCAP file path", type=str)
file_group.add_argument(
"-d",
"--test",
action="store_true",
default=False,
help="genenerate binary file (test mode)",
)
parser.add_argument(
"-f",
"--binary-file",
help="Binary file path (used as key inputs)",
type=str,
)
parser.add_argument(
'-v',
'--verbose',
action='store_true',
help='enable verbose output'
)
args = parser.parse_args()
def testmode():
"""
Generate a search file of 4K in size of random data, then "hide" a static
key in the file to locate using this tool. For demonstration purposes and
testing.
"""
key = b"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
key_len = len(key)
searchfilelen = 8192
fin = open("/dev/urandom", "rb")
fout = open("searchfile.dat", "wb")
searchdata = fin.read(searchfilelen)
randoffset = int(random.uniform(0, searchfilelen - key_len))
fout.write(searchdata[0:randoffset] + key + searchdata[randoffset + key_len :])
fout.close()
fin.close()
sys.exit(0)
def interrupt(signum, frame):
global cap
if cap:
cap.close()
sys.exit(0)
def keysearch(packet, searchdata):
global args
offset = 0
guesses = 0
d = Dot154PacketParser()
searchdatalen = len(searchdata)
while offset < (searchdatalen - 16):
if args.verbose:
key = ""
for i in range(0, 16):
key += "%02x:" % searchdata[offset + i]
print(f"Calling decrypt with key: {key[:-1]}", end="\n")
guesses += 1
try:
if d.decrypt(packet, searchdata[offset : offset + 16]) != "":
print("Key found after %d guesses: " % guesses, end=" ")
for i in range(0, 15):
sys.stdout.write("%02x:" % searchdata[offset + i])
print("%02x" % searchdata[offset + 15])
return True
except (UnsupportedPacket, BadKeyLength, BadPayloadLength):
pass
except Exception as e:
import traceback
traceback.print_exc()
finally:
offset += 1
return False
if args.test:
testmode()
# Pcap or Daintree reader object
cap = None
if args.binary_file == None:
print("ERROR: Must specify a search file with -f", file=sys.stderr)
parser.print_usage()
sys.exit(1)
if not os.path.exists(args.binary_file):
print(f"ERROR: Search file not found: {args.binary_file}", file=sys.stderr)
sys.exit(1)
if (args.pcap == None and args.daintree == None):
print(
"ERROR: Must specify a file with frames to decrypt with -r (pcap) or -R (Daintree SNA)",
file=sys.stderr,
)
parser.print_usage()
sys.exit(1)
fnfm = False
if args.pcap and not os.path.exists(args.pcap):
fnfm = f"ERROR: Capture file {args.pcap} was not found"
if args.daintree and not os.path.exists(args.daintree):
fnfm = f"ERROR: Capture file {args.pcap} was not found"
if fnfm != False:
print(
fnfm,
file=sys.stderr,
)
sys.exit(1)
# Open and read the search file
fh = open(args.binary_file, "rb")
searchdata = fh.read()
fh.close()
if len(searchdata) <= 0:
print(f"ERROR: no data found in {args.binary_file} populate it with possible key material")
sys.exit(1)
if args.pcap != None:
savefile = args.pcap
cap = PcapReader(args.pcap)
elif args.daintree != None:
savefile = args.daintree
cap = DainTreeReader(args.daintree)
signal.signal(signal.SIGINT, interrupt)
print(
f"zbgoodfind: searching the contents of {args.binary_file} for encryption keys with the first encrypted packet in {savefile}"
)
packetfound = 0
packetcount = 0
while 1:
try:
packet = cap.pnext()[1]
packetcount += 1
# Byte swap
fcf = struct.unpack("<H", packet[0:2])[0]
if (fcf & DOT154_FCF_SEC_EN) == 0:
# Packet is not encrypted
if args.verbose:
print("Skipping unencrypted packet %d." % packetcount)
continue
packetfound = 1
if args.skip_fcs:
packet = packet[:-2]
if args.verbose:
print("Starting key search with packet %d." % packetcount)
if keysearch(packet, searchdata) == True:
break
else:
print("Failed to locate the encryption key for frame %d." % packetcount)
except TypeError: # raised when pnext returns Null (end of capture)
break
if packetfound == 0:
print("No encrypted packets found in the capture file %s." % savefile)
cap.close()