forked from sebastiendamaye/pintool2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pintool3.py
executable file
·171 lines (141 loc) · 6.48 KB
/
pintool3.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# prog_name= 'pintool3.py'
# prog_version = '3.0'
# prog_release = '20221203'
# prog_author = 'Tr4ccK'
# prog_author_mail = '[email protected]'
import sys
import string as s
import subprocess
import argparse
import re
PINBASEPATH = '/home/track/CTF-Tools/pin-3.25-gcc-linux/'
PIN = PINBASEPATH + 'pin'
INSCOUNT32 = PINBASEPATH + 'source/tools/ManualExamples/obj-ia32/inscount0.so'
INSCOUNT64 = PINBASEPATH + 'source/tools/ManualExamples/obj-intel64/inscount0.so'
def start():
parser = argparse.ArgumentParser(prog='pintool3.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='passlen', type=int, default=10, help='Length of password (Default: 10 )')
parser.add_argument('-c', dest='charnum', type=str, help="Charset definition for brute force (1-Lowercase, 2-Uppecase, 3-Numbers, 4-Hexadecimal, 5-Punctuation, 6-All)")
parser.add_argument('-b', dest='exchar', type=str, nargs=1, default='', help='Add characters for the charset, example -b _-')
parser.add_argument('-a', dest='arch', type=str, nargs=1, default='64', help='Program architecture 32 or 64 bits, -a 32 or -a 64 (Default: 64)')
parser.add_argument('-i', dest='initpass', type=str, nargs=1, default='', help='Inicial password characters, example -i CTF{')
parser.add_argument('-s', dest='symbfill', type=str, nargs=1, default='_', help='Symbol 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(" ./pintool3.py -l 30 -c 1,2,3 -b _{} -s - baleful")
print(" ./pintool3.py -l 37 -c 4 -i CTF{ -b }_ -s - -d '=> 651' reverse400")
print(" ./pintool3.py -c 1,2,3 -b _ -s - -a 64 -l 28 wyvern")
print(" ./pintool3.py -r -l 32 -c 1,2,3 -b _{$} -s - 01f47d58806a8264cd4b2b97b9dabb4a")
print()
sys.exit(1)
args = parser.parse_args()
return args
def getCharset(num):
charset = {
1: s.ascii_lowercase,
2: s.ascii_uppercase,
3: s.digits,
4: s.hexdigits,
5: s.punctuation,
6: s.printable
}
char = ''
if num == None:
return charset[1]
else:
num = [int(i) for i in num.split(',')]
assert all(1 <= i <= 6 for i in num), "Charset must be between 1 and 6"
for i in num:
char += charset[i]
return char
def pin(passwd):
try:
command = f'echo {passwd} | {PIN} -t {INSCOUNT} -- ./{args.filename} ; cat inscount.out'
output = subprocess.check_output(command, shell=True, stderr=subprocess.PIPE)
except:
print(f"Unexpected error: {sys.exc_info()[0]}")
raise
output = re.findall(r"Count ([\w.-]+)", output.decode())
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(f"{password} = with {i} characters difference {inscount-inicialdifference} instructions")
def solve(initpass, passlen, symbfill, diffinst, charset, expression, reverse):
initlen = len(initpass)
for i in range(initlen, passlen):
tempassword = initpass + symbfill * (passlen-i)
initdiff = 0
for char in charset:
password = tempassword[:i] + char + tempassword[i+1:]
if reverse:
password = password[::-1]
inscount = pin(password)
if initdiff == 0:
initdiff = inscount
difference = inscount - initdiff
print(f"{password} <= \033[1;31m{difference}\033[0m + {inscount} instructions")
sys.stdout.write('\033[F')
cmpsym = expression.split()[0]
match cmpsym:
case '!=':
if difference != diffinst:
print(f"{password} <= \033[1;31m{difference}\033[0m + {inscount} instructions")
initpass += char
break
case '==':
if difference == diffinst:
print(f"{password} <= \033[1;31m{difference}\033[0m + {inscount} instructions")
initpass += char
break
case '<=':
if difference <= diffinst:
print(f"{password} <= \033[1;31m{difference}\033[0m + {inscount} instructions")
initpass += char
break
case '=>':
if difference >= diffinst:
print(f"{password} <= \033[1;31m{difference}\033[0m + {inscount} instructions")
initpass += char
break
case _:
print("Unknown value for -d option")
sys.exit()
else:
print("\n\nPassword not found, try to change charset...\n")
sys.exit()
return password
if __name__ == '__main__':
args = start()
initpass = ''.join(args.initpass)
passlen = args.passlen
symbfill = ''.join(args.symbfill)
charset = symbfill + getCharset(args.charnum) + ''.join(args.exchar)
arch = ''.join(args.arch)
expression = ''.join(args.expression).strip()
diffinst = int(expression.split()[1])
study = args.study
reverse = args.reverse
assert len(initpass) < passlen, "Initial password must be less than password length"
assert passlen <= 64, "Password length must be less or equal to 64"
assert len(symbfill) == 1, "Symbol for complete all password must be only one character"
assert arch in ['32','64'], "Architecture must be 32 or 64"
INSCOUNT = eval('INSCOUNT' + arch)
if study is True:
lengthdetect(passlen)
sys.exit()
password = solve(initpass, passlen, symbfill, diffinst, charset, expression, reverse)
print(f"\033[1;32m[+]\033[0m Password: {password}")