forked from ChrisTheCoolHut/PinCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinCTF.py
executable file
·470 lines (384 loc) · 17.7 KB
/
pinCTF.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/python3
import os
import sys
import argparse
import IPython
import configparser
import string
import concurrent.futures
import shutil
from subprocess import PIPE, Popen
from multiprocessing import Pool
def main():
#Defaults
configLocation = "config.ini"
config = checkConfig(configLocation)
pinLocation = config.get("DEFAULTS","PinLocation")
libraryLocation = config.get("DEFAULTS","LibraryLocation")
count = config.get("DEFAULTS","Count")
seed = config.get("DEFAULTS","Seed")
variable_range = config.get("DEFAULTS","Range")
start = 0
threading = False
#a-Z
# variable_range = string.ascii_letters
parser = argparse.ArgumentParser()
#Add arguments
parser.add_argument('-f','--file',help="file to run pin against")
parser.add_argument('-a', '--arg',help="Trace instructions for passed in argument",action="store_true")
parser.add_argument('-al', '--argLength',help="Trace instructions for passed in argument length",action="store_true")
parser.add_argument('-i', '--input',help="Trace instructions for given input",action="store_true")
parser.add_argument('-il', '--inputLength',help="Trace instructions for input length",action="store_true")
parser.add_argument('-p', '--pinLocation',help="Location of pin's directory")
parser.add_argument('-l', '--pinLibraryLocation',help="Location of pin's instruction0.so libraries")
#If length based instruction counting you can provide a count
parser.add_argument('-c','--count',help="MaxLength to for length based pin")
#If arg or input based, we need a seed to start and can use a different
#range to iterate over
parser.add_argument('-s','--seed',help="Initial seed for input or arg pin")
parser.add_argument('-r','--range',help="range of characters to iterate pin over")
parser.add_argument('-rev','--reversed',help="Reverse the direction of guesses",action='store_true')
#Optionally we can specify a length for our seed, further we can choose where to start guessing
parser.add_argument('-sl','--seedLength',help="Initial seed length for input or arg pin")
parser.add_argument('-st','--seedStart',help="Initial seed index for pin")
#Speed up process with threading options
parser.add_argument('-t','--threading',help="Enables threading",action='store_true')
parser.add_argument('-tc','--threadCount',help="Number of threads",default=2)
parser.add_argument('-sk','--skip',help="Skip extra favored paths",action='store_true')
#Parse Arguments
args =parser.parse_args()
#Check for argument errors
if not args.file:
print("[-] Error missing file")
exit(0)
if args.file:
args.file = os.path.abspath(args.file)
if not (args.arg or args.argLength or args.input or args.inputLength): #TODO change to A xor B xor C xor D
print("[-] Error missing pin instruction counting technique")
exit(0)
if args.pinLocation:
pinLocation = args.pinLocation
if args.pinLibraryLocation:
libraryLocation = args.pinLibraryLocation
if args.count:
count = int(args.count)
if args.seedLength:
seed = 'A'*int(args.seedLength)
if args.seed:
seed = args.seed
if args.range:
variable_range = args.range
if args.seedStart:
start = int(args.seedStart)
if args.threading:
threading = True
#Can I get a switch statement please?
if args.argLength:
argLengthTuple = pinLength(pinLocation,libraryLocation,args.file,count,arg=True, multi_core=int(args.threadCount))
print("[+] Found Length {} : Count {}".format(argLengthTuple[0], argLengthTuple[1]))
if args.inputLength:
inputLengthTuple = pinLength(pinLocation,libraryLocation,args.file,count,arg=False, multi_core=int(args.threadCount))
print("[+] Found Num {} : Count {}".format(inputLengthTuple[0], inputLengthTuple[1]))
if args.arg:
pattern = pinIter(pinLocation,libraryLocation,args.file,seed,variable_range,arg=True,start=start,threading=threading,threadCount=int(args.threadCount),reverseRange=args.reversed,skip=args.skip)
print("[+] Found pattern {}".format(pattern))
if args.input:
pattern = pinIter(pinLocation,libraryLocation,args.file,seed,variable_range,arg=False,start=start,threading=threading,threadCount=int(args.threadCount),reverseRange=args.reversed,skip=args.skip)
print("[+] Found pattern {}".format(pattern))
#Checks for existence of config
#Creates config if not found, else returns config
def checkConfig(configPath):
config = None
if not os.path.isfile(configPath):
print("[-] No config found. Building now")
cwd = os.getcwd()
config = configparser.ConfigParser()
config.add_section("DEFAULTS")
#Set defaults if no config is found
config.set("DEFAULTS","PinLocation","")
if os.path.isdir("{}/pin".format(cwd)):
config.set("DEFAULTS","PinLocation","{}/pin".format(cwd))
config.set("DEFAULTS","LibraryLocation","")
if os.path.isdir("{}/obj-ia32".format(cwd)):
config.set("DEFAULTS","LibraryLocation","{}/obj-ia32".format(cwd))
config.set("DEFAULTS","Count","20")
config.set("DEFAULTS","Seed","ABCD")
config.set("DEFAULTS","Range","abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-")
configFile = open(configPath,'w')
config.write(configFile)
configFile.close()
else:
config = configparser.ConfigParser()
config.read(configPath)
return config
def readCount(fileName="inscount.out"):
inscountFileName = fileName
inscountFile = open(inscountFileName)
line = inscountFile.read()
count = 0
try:
count = int(line.split(' ')[1])
except:
print("[-] Expected number, got {}".format(line))
inscountFile.close()
return count
def sendPinArgCommand(pin,library,binary,arg):
#The delay given by Popen causes inconsistencies in PIN
#So use os.system instead
COMMAND = "{}/pin -t {}/inscount0.so -- {} {} > /dev/null".format(pin,library,binary,arg)
os.system(COMMAND)
count = readCount()
return count
def sendPinInputCommand(pin,library,binary,input):
#The delay given by Popen causes inconsistencies in PIN
#So use os.system instead
ARGS = "{}/pin -t {}/inscount0.so -- {} ".format(pin,library,binary)
#Send the output to /dev/null since it will pollute the screen otherwise
#os.system("echo {} | {} > /dev/null".format(input,ARGS))
os.system("echo {} | {} > /dev/null".format(input,ARGS))
count = readCount()
return count
def sendPinArgCommandThread(pin,library,binary,arg,ident, inIMAP=False):
#The delay given by Popen causes inconsistencies in PIN
#So use os.system instead
if not os.path.exists("pin_{}".format(ident)):
os.mkdir("pin_{}".format(ident))
COMMAND = "cd pin_{} > /dev/null; {}/pin -t {}/inscount0.so -- {} {} > /dev/null".format(ident,pin,library,binary,arg)
os.system(COMMAND)
count = readCount("pin_{}/inscount.out".format(ident))
shutil.rmtree('pin_{}'.format(ident))
if not inIMAP:
return count
else:
return ident,count
def sendPinInputCommandThread(pin,library,binary,input,ident, inIMAP=False):
#The delay given by Popen causes inconsistencies in PIN
#So use os.system instead
if not os.path.exists("pin_{}".format(ident)):
os.mkdir("pin_{}".format(ident))
ARGS = "{}/pin -t {}/inscount0.so -- {} ".format(pin,library,binary)
#Send the output to /dev/null since it will pollute the screen otherwise
COMMAND = "cd pin_{} > /dev/null; echo {} | {} > /dev/null".format(ident,input,ARGS)
os.system(COMMAND)
count = readCount("pin_{}/inscount.out".format(ident))
shutil.rmtree('pin_{}'.format(ident))
if not inIMAP:
return count
else:
return ident,count
def pinLength(pin,library,binary,length,arg=False, multi_core=1):
lengthDict = {}
arg_list = []
if multi_core > 1:
m_pool = Pool(multi_core)
for i in range(1,int(length)+1):
parallel_arg = (pin, library, binary, 'A'*i,'A'*i,i-1, arg)
arg_list.append(parallel_arg)
#(runThreadedCommand,pin,library,binary,path,item,i,arg)
for i in m_pool.imap_unordered(runThreadedCommandWrapper, arg_list):
sys.stdout.write("[~] Trying {}\r".format('A'*len(i[0])))
sys.stdout.flush()
lengthDict[len(i[0])] = i[1]
else:
for i in range(1,int(length)+1):
if arg:
count = sendPinArgCommand(pin,library,binary,'A'*(i))
else:
count = sendPinInputCommand(pin,library, binary, 'A' * (i))
sys.stdout.write("[~] Trying {}\r".format('A'*(i)))
sys.stdout.flush()
lengthDict[i] = count
#Get largest count value
largestCount = 0
largestNum = 0
print("{:<4} : {:<15}".format("Num","Instr Count"))
for num in lengthDict:
if lengthDict[num] > largestCount:
largestCount = lengthDict[num]
largestNum = num
print("{:<4} : {:<15}".format(num,lengthDict[num]))
return (largestNum,largestCount)
def pinIter(pin,library,binary,seed,variable_range,arg=False,start=0,threading=False,threadCount=2,reverseRange=False,skip=False):
seedLength = len(seed)
#FavoredPaths help us reset iterations when it's not sure
#whether more or fewer instructions gets the analysis closer
favoredPaths = set()
favoredPaths.add(seed)
print("[~] Status:\nthreading : {}\nreverseRange : {}\nskipFavoredPaths : {}".format(threading,reverseRange,skip))
iterRange = range(start,seedLength)
if reverseRange:
print("[~] Running in reverse direction")
iterRange = reversed(iterRange)
for i in iterRange:
rangeDict = {}
#A copy is made since we modify favoredPaths inside
#the loop
favoredPathsCopy = favoredPaths.copy()
pathIter = 0
favored = True
for path in favoredPathsCopy:
favored = True
#An unmodified path is needed to remove from the
#Favored path list
originalPath = path
#This could be parallelized
#Each thread/process could use a unique directory
#And send input from a pool of choices given by
#varaible_range
if threading:
with concurrent.futures.ThreadPoolExecutor(max_workers=threadCount) as executor:
futureToItem = {executor.submit(runThreadedCommand,pin,library,binary,path,item,i,arg): item for item in variable_range}
for future in concurrent.futures.as_completed(futureToItem):
itemInstance = futureToItem[future]
try:
countTuple = future.result()
item = countTuple[0]
count = countTuple[1]
if count == 0:
print("[-] Count was zero for path {}".format(item))
except Exception as exc:
print('{} had exception {}'.format(itemInstance,exc))
else:
#Do I need to make a lock?
rangeDict[item] = count
else:
for item in variable_range:
#Exchange value in seed for our range values
#Python strings can't do it, so we use a list
sys.stdout.write("[~] Trying {}\r".format(path))
sys.stdout.flush()
seedList = list(path)
seedList[i] = item
path = ''.join(seedList)
if arg:
count = sendPinArgCommand(pin,library,binary,path)
else:
count = sendPinInputCommand(pin,library,binary,path)
rangeDict[item] = count
#New line to fix carriage return
print()
countTuple = getItemByCount(rangeDict)
largestItem = countTuple[0]
largestCount = countTuple[1]
#Does our largest count match other instructions?
minMatchCount = 3
uniqueCounts = list(rangeDict.values())
uniqueUniques = set(rangeDict.values())
if len(uniqueUniques) == 1:
print("[-] Single unique instruction count")
print("[~] Switching to other favored paths")
print("Removing {}".format(originalPath))
try:
favoredPaths.remove(originalPath)
except:
print("[-] Error path already gone")
if len(favoredPaths) > 1:
print("Multiple FavoredPaths : {}".format(len(favoredPaths)))
for favoredPath in favoredPaths:
print(favoredPath)
favored = False
rangeList = list(rangeDict.values())
average = sum(rangeList) / float(len(rangeList))
extraPaths = set()
if (uniqueCounts.count(largestCount) > minMatchCount or (largestCount - average) < 4) and favored:
deltaTuple = getItemByDelta(rangeDict)
largestItem = deltaTuple[0]
largestCount = deltaTuple[1]
#Check for exact matching deltas
deltaDict = deltaTuple[2]
if not skip:
for k,v in deltaDict.items():
if v == deltaDict[largestItem] and k is not largestItem:
seedList = list(path)
seedList[i] = k
favoredSeed = ''.join(seedList)
extraPaths.add(favoredSeed)
print("[~] Adding delta favored path {}".format(favoredSeed))
#Slot the value in
if favored:
try:
seedList = list(path)
seedList[i] = largestItem
path = ''.join(seedList)
print("[+] iter {} using {} for {}".format(i,largestItem,path))
except:
print("[-] Unable to slot value into seed string. Likely 0 delta from other inputs")
print("[~] Switching to other favored paths")
print("Removing {}".format(originalPath))
favoredPaths.remove(originalPath)
if len(favoredPaths) > 1:
print("Multiple FavoredPaths : {}".format(favoredPaths))
favored = False
if favored:
favoredPaths.clear()
#Building a favored paths list for exploration
if not skip:
for x in uniqueCounts:
if uniqueCounts.count(x) == 1:
temp = ""
for k, v in rangeDict.items():
if v == x:
temp = k
seedList = list(path)
seedList[i] = temp
favoredSeed = ''.join(seedList)
favoredPaths.add(favoredSeed)
if len(favoredPaths) > 1:
print("Multiple FavoredPaths : {}".format(favoredPaths))
for delta in extraPaths:
favoredPaths.add(delta)
extraPaths.clear()
favoredPaths.add(path)
favored = True
else:
print("[+] Ignoring path {}".format(path))
favored = True
return favoredPaths.pop()
def getItemByDelta(rangeDict):
rangeList = list(rangeDict.values())
print("[~] Largest instruction count found to match several others or very close")
print("[~] Locating largest difference from average instead")
deltaDict = {}
#Get largest delta
largestCount = 0
largestItem = 0
#Get Average
rangeList = list(rangeDict.values())
average = sum(rangeList) / float(len(rangeList))
for k, v in rangeDict.items():
# print("Key {} : Count {} : Delta {} vs {}".format(k,v,abs(v-average),largestCount))
if abs(v - average) > largestCount:
largestItem = k
largestCount = abs(v - average)
deltaDict[k] = abs(v - average)
return(largestItem,largestCount,deltaDict)
def getItemByCount(rangeDict):
# Get largest count value
largestCount = 0
largestItem = 0
# print("{:<4} : {:<15}".format("Num","Instr Count"))
#for k, v in rangeDict.items():
for key in sorted(rangeDict.keys()):
if rangeDict[key] > largestCount:
largestCount = rangeDict[key]
largestItem= key
# print("{:<4} : {:<15}".format(key,rangeDict[key]))
return(largestItem,largestCount)
def runThreadedCommandWrapper(mapped_data):
return runThreadedCommand(mapped_data[0], mapped_data[1], mapped_data[2], mapped_data[3], mapped_data[4], mapped_data[5], mapped_data[6])
#(runThreadedCommand,pin,library,binary,path,item,i,arg)
def runThreadedCommand(pin,library,binary,path,item,i,arg=False):
if len(item) == 1:
seedList = list(path)
seedList[i] = item
path = ''.join(seedList)
else:
seedList = list(path)
seedList[i] = item[0]
path = ''.join(seedList)
if arg:
count = sendPinArgCommandThread(pin,library,binary,path,item)
else:
count = sendPinInputCommandThread(pin,library,binary,path,item)
return (item,count)
main()