Skip to content

Commit 54d3be4

Browse files
author
desword
committed
first udpate tools
1 parent 2078c69 commit 54d3be4

2 files changed

Lines changed: 251 additions & 0 deletions

File tree

patternLocOffset.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python
2+
#####################################################################################
3+
## Create pattern strings & location offset
4+
## Tested against Ubuntu 12.04 & Windows # #
5+
##
6+
## Example:
7+
## C:\Users\Lenov\Desktop> patterLocOffset.py -c -l 260 -f output.txt
8+
### [*] Create pattern string contains 260 characters ok!
9+
### [+] output to output.txt ok!
10+
##
11+
## C:\Users\Lenov\Desktop> patternLocOffset.py -s 0x41613141 -l 260
12+
### [*] Create pattern string contains 260 characters ok!
13+
### [*] Exact match at offset 3
14+
#
15+
## Nimdakey # 09-10-2013
16+
#####################################################################################
17+
18+
import argparse
19+
import struct
20+
import binascii
21+
import string
22+
import time
23+
import sys
24+
import re
25+
26+
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27+
b = "abcdefghijklmnopqrstuvwxyz"
28+
c = "0123456789"
29+
30+
def generate(count,output):
31+
#
32+
# pattern create
33+
codeStr = ''
34+
print '[*] Create pattern string contains %d characters'%count,
35+
timeStart = time.time()
36+
for i in range(0,count):
37+
codeStr += a[i/(26*10)]+b[(i%(26*10))/10]+c[i%(26*10)%10]
38+
print 'ok!'
39+
if output:
40+
print '[+] output to %s'%output,
41+
fw = open(output,'w')
42+
fw.write(codeStr)
43+
fw.close()
44+
print 'ok!'
45+
else:
46+
return codeStr
47+
print "[+] take time: %.4f s"%(time.time()-timeStart)
48+
49+
def patternMatch(searchCode, length=1024):
50+
#
51+
# pattern search
52+
offset = 0
53+
pattern = None
54+
55+
timeStart = time.time()
56+
is0xHex = re.match('^0x[0-9a-fA-F]{8}',searchCode)
57+
isHex = re.match('^[0-9a-fA-F]{8}',searchCode)
58+
59+
if is0xHex:
60+
#0x41613141
61+
pattern = binascii.a2b_hex(searchCode[2:])
62+
elif isHex:
63+
#41613141
64+
pattern = binascii.a2b_hex(searchCode)
65+
else:
66+
print '[-] seach Pattern eg:0x41613141'
67+
sys.exit(1)
68+
69+
source = generate(length,None)
70+
offset = source.find(pattern)
71+
72+
if offset != -1:
73+
print "[*] Exact match at offset %d"%offset
74+
else:
75+
print "[*] No exact matches, looking for likely candidates..."
76+
reverse = list(pattern)
77+
reverse.reverse()
78+
pattern = "".join(reverse)
79+
offset = source.find(pattern)
80+
if offset != -1:
81+
print "[+] Possible match at offset %d (adjusted another-endian)"%offset
82+
print "[+] take time: %.4f s"%(time.time()-timeStart)
83+
84+
def main():
85+
## parse argument
86+
parser = argparse.ArgumentParser()
87+
parser.add_argument('-s', '--search', help='search for pattern')
88+
parser.add_argument('-c', '--create', help='create a pattern',\
89+
action='store_true')
90+
parser.add_argument('-f', '--file', help='output file name',\
91+
default='patternShell.txt')
92+
parser.add_argument('-l', '--length',help='length of pattern code',\
93+
type=int,default=1024)
94+
#parser.add_argument('-v', dest='verbose', action='store_true')
95+
args = parser.parse_args()
96+
97+
## save all argument
98+
length = args.length
99+
output = args.file
100+
#verbose = args.verbose
101+
createCode = args.create
102+
searchCode = args.search
103+
104+
if createCode and (0 < args.length <= 26*26*10):
105+
#eg: -c -l 90
106+
generate(length,output)
107+
elif searchCode and (0 < args.length <= 26*26*10):
108+
#eg: -s 0x474230141
109+
patternMatch(searchCode,length)
110+
else:
111+
print '[-] You shoud chices from [-c -s]'
112+
print '[-] Pattern length must be less than 6760'
113+
print 'more help: pattern.py -h'
114+
# ...
115+
116+
if __name__ == "__main__":
117+
main()

shell_extractor.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python
2+
#####################################################################################
3+
## extract shellcode binarys for c or python
4+
## Tested against Ubuntu 12.04 & Windows # #
5+
##
6+
## Example:
7+
# $ python shell_extractor.py execve c
8+
# char shellcode[] = {
9+
# "\x24\x06\x06\x66"
10+
# "\x04\xd0\xff\xff"
11+
# "\x28\x06\xff\xff"
12+
# "\x27\xbd\xff\xe0"
13+
# "\x27\xe4\x10\x01"
14+
# "\x24\x84\xf0\x1f"
15+
# "\xaf\xa4\xff\xe8"
16+
# "\xaf\xa0\xff\xec"
17+
# "\x27\xa5\xff\xe8"
18+
# "\x24\x02\x0f\xab"
19+
# "\x00\x00\x00\x0c"
20+
# "\x2f\x62\x69\x6e"
21+
# "\x2f\x73\x68\x00"
22+
# };
23+
## desword # 2018-8-2
24+
#####################################################################################
25+
26+
27+
28+
import os
29+
import sys
30+
31+
def ExtractOffset(line):
32+
sp = line.split(' ')
33+
while '' in sp:
34+
sp.remove('')
35+
# print sp
36+
37+
try:
38+
section = sp[2]
39+
except:
40+
section = -1
41+
try:
42+
offset = int(sp[5], 16)
43+
except:
44+
offset = -1
45+
try:
46+
size = int(sp[6], 16)
47+
except:
48+
size = -1
49+
return [section, offset, size]
50+
51+
def usage():
52+
print "[+] Usage: python shell_extractor.py [filename] [format]"
53+
print "[*] Where format can be c or py"
54+
55+
56+
57+
try:
58+
fileName = sys.argv[1]
59+
printformat = sys.argv[2]
60+
except:
61+
usage()
62+
exit(1)
63+
64+
65+
extractionCmd = "readelf -S " + fileName
66+
67+
68+
result = os.popen(extractionCmd)
69+
res = result.read()
70+
lines = res.splitlines()
71+
72+
section = 0; offset = 0; size = 0;
73+
74+
for line in lines:
75+
[section, offset, size] = ExtractOffset(line)
76+
if section == ".text":
77+
# print line
78+
# print "find, offset", offset, "size", size
79+
break;
80+
81+
82+
83+
f = open(fileName,"rb")
84+
outfile = []
85+
i = 0
86+
while 1:
87+
c = f.read(1)
88+
i = i + 1
89+
if not c:
90+
break
91+
if ord(c) <= 15:
92+
outfile.append("0x0"+hex(ord(c))[2:])
93+
else:
94+
outfile.append(hex(ord(c)))
95+
96+
f.close()
97+
98+
99+
### extract shell code
100+
101+
extracted = outfile[offset:offset+size]
102+
103+
104+
105+
### print for shellcode book.
106+
107+
if printformat == "py":
108+
shellOutput = ["shellcode = \"\""]
109+
shellHeader = "shellcode +="
110+
for i in range(0, len(extracted), 4):
111+
shelltmp = ""
112+
for eachByte in extracted[i:i+4]:
113+
shelltmp += ("\\x" + eachByte[2:])
114+
shellOutput.append("%s \"%s\"" % (shellHeader, shelltmp))
115+
116+
shellOutputStr = "\n".join(shellOutput)
117+
print shellOutputStr
118+
elif printformat == "c":
119+
120+
### print for c source code test
121+
122+
shellOutput = ["char shellcode[] = {"]
123+
for i in range(0, len(extracted), 4):
124+
shelltmp = ""
125+
for eachByte in extracted[i:i+4]:
126+
shelltmp += ("\\x" + eachByte[2:])
127+
shellOutput.append("\"%s\"" % (shelltmp))
128+
shellOutput.append("};")
129+
130+
shellOutputStr = "\n".join(shellOutput)
131+
print shellOutputStr
132+
133+
134+

0 commit comments

Comments
 (0)