|
| 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() |
0 commit comments