-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpintool2.py
executable file
·217 lines (159 loc) · 6.41 KB
/
pintool2.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
#!/usr/bin/env python
#coding: utf8
#
#prog_name= 'pintool2.py'
#prog_version = '2.0'
#prog_release = '20160724'
#prog_author = 'Sebastien Damaye'
#prog_author_mail = '[email protected]'
"""
pintool2 is an improved version of the pintool.py script written by wagiro (Eduardo García),
available here (https://github.com/wagiro/pintool).
This version integrates an additional reverse order option to brute force password in reverse order (starts from the end).
This tool can be useful for solving some reversing challenges in CTFs events.
Implements the technique described here (http://shell-storm.org/blog/A-binary-analysis-count-me-if-you-can/).
"""
import sys
import string as s
import subprocess
import argparse
import re
#configure by the user
PINBASEPATH = "/data/pin-3.0-76991-gcc-linux"
PIN = "%s/pin" % PINBASEPATH
INSCOUNT32 = "%s/source/tools/ManualExamples/obj-ia32/inscount0.so" % PINBASEPATH
INSCOUNT64 = "%s/source/tools/ManualExamples/obj-intel64/inscount0.so" % PINBASEPATH
def start():
parser = argparse.ArgumentParser(prog='pintool2.py')
parser.add_argument('-e', dest='study', action='store_true', default=False, help='Study the password length, for example -e -l 40, with 40 characters')
parser.add_argument('-l', dest='len', type=str, nargs=1, default='10', help='Length of password (Default: 10 )')
parser.add_argument('-c', dest='number', type=str, default=1, help="Charset definition for brute force\n (1-Lowercase,\n2-Uppecase,\n3-Numbers,\n4-Hexadecimal,\n5-Punctuation,\n6-All)")
parser.add_argument('-b', dest='character', type=str, nargs=1, default='', help='Add characters for the charset, example -b _-')
parser.add_argument('-a', dest='arch', type=str, nargs=1, default='32', help='Program architecture 32 or 64 bits, -a 32 or -a 64 ')
parser.add_argument('-i', dest='initpass', type=str, nargs=1, default='', help='Inicial password characters, example -i CTF{')
parser.add_argument('-s', dest='simbol', type=str, nargs=1, default='_', help='Simbol for complete all password (Default: _ )')
parser.add_argument('-d', dest='expression', type=str, nargs=1, default='!= 0', help="Difference between instructions that are successful or not (Default: != 0, example -d '== -12', -d '=> 900', -d '<= 17' or -d '!= 32')")
parser.add_argument('-r', dest='reverse', action='store_true', default=False, help='Start in reverse order')
parser.add_argument('Filename',help='Program for playing with Pin Tool')
if len(sys.argv) < 2:
parser.print_help()
print ("")
print ("Examples:")
print (" ./pintool2.py -l 30 -c 1,2,3 -b _{} -s - baleful")
print (" ./pintool2.py -l 37 -c 4 -i CTF{ -b }_ -s - -d '=> 651' reverse400")
print (" ./pintool2.py -c 1,2,3 -b _ -s - -a 64 -l 28 wyvern")
print (" ./pintool2.py -r -l 32 -c 1,2,3 -b _{$} -s - 01f47d58806a8264cd4b2b97b9dabb4a")
print ("")
sys.exit()
args = parser.parse_args()
return args
def getCharset(num,addchar):
char = ""
charset = { '1': s.ascii_lowercase,
'2': s.ascii_uppercase,
'3': s.digits,
'4': s.hexdigits,
'5': s.punctuation,
'6': s.printable}
if num is 1:
return charset['1']
else:
num = num.split(',')
for i in num:
if 1 <= int(i) <= 6:
i= '%s' % i
char += charset[i]
else:
print "Number %s out of range." % (i)
return char+''.join(addchar)
def pin(passwd):
try:
command = "echo " + passwd + " | " + PIN + " -t " + INSCOUNT + " -- ./"+ args.Filename + " ; cat inscount.out"
output = subprocess.check_output(command,shell=True,stderr=subprocess.PIPE)
except:
print "Unexpected error:", sys.exc_info()[0]
raise
output = re.findall(r"Count ([\w.-]+)", output)
return int(''.join(output))
def lengthdetect(passlen):
inicialdifference = 0
for i in range(1,passlen+1):
password = "_"*i
inscount = pin(password)
if inicialdifference == 0:
inicialdifference = inscount
print "%s = with %d characters difference %d instructions" %(password, i, inscount-inicialdifference)
def solve(initpass,passlen,symbfill,charset,expression,reverse):
initlen = len(initpass)
for i in range(initlen,passlen):
tempassword = initpass + symbfill*(passlen-i)
inicialdifference = 0
for char in charset:
password = tempassword[:i] + '\\'+char + tempassword[i+1:]
if reverse:
password = password[::-1]
inscount = pin(password)
if inicialdifference == 0:
inicialdifference = inscount
difference = inscount-inicialdifference
print "%s = %d difference %d instructions" %(password.replace("\\","",1), inscount, difference)
sys.stdout.write("\033[F")
if "!=" in expression:
if difference != int(number):
print "%s = %d difference %d instructions" %(password.replace("\\","",1), inscount, difference)
initpass += char
break
elif "==" in expression:
if difference == int(number):
print "%s = %d difference %d instructions" %(password.replace("\\","",1), inscount, difference)
initpass += char
break
elif "<=" in expression:
if difference <= int(number):
print "%s = %d difference %d instructions" %(password.replace("\\","",1), inscount, difference)
initpass += char
break
elif "=>" in expression:
if difference >= int(number):
print "%s = %d difference %d instructions" %(password.replace("\\","",1), inscount, difference)
initpass += char
break
else:
print "Unknown value for -d option"
sys.exit()
if char == charset[-1]:
print "\n\nPassword not found, try to change charset...\n"
sys.exit()
return password.replace("\\","",1)
if __name__ == '__main__':
args = start()
initpass = ''.join(args.initpass)
passlen = int(''.join(args.len))
symbfill = ''.join(args.simbol)
charset = symbfill+getCharset(args.number,args.character)
arch = ''.join(args.arch)
expression = ''.join(args.expression).rstrip()
number = expression.split()[1]
study = args.study
reverse = args.reverse
if len(initpass) >= passlen:
print "The length of init password must be less than password length."
sys.exit()
if passlen > 64:
print "The password must be less than 64 characters."
sys.exit()
if len(symbfill) > 1:
print "Only one symbol is allowed."
sys.exit()
if arch == "32":
INSCOUNT = INSCOUNT32
elif arch == "64":
INSCOUNT = INSCOUNT64
else:
print "Unknown architecture"
sys.exit()
if study is True:
lengthdetect(passlen)
sys.exit()
password = solve(initpass,passlen,symbfill,charset,expression,reverse)
print "Password: ", password