Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/shellcode/windows/x86/src/hash.py
Views: 11789
#!/usr/bin/env python31#=============================================================================#2# This script can detect hash collisions between exported API functions in3# multiple modules by either scanning a directory tree or just a single module.4# This script can also just output the correct hash value for any single API5# function for use with the 'api_call' function in 'block_api.asm'.6#7# Example: Detect fatal collisions against all modules in the C drive:8# >hash.py /dir c:\9#10# Example: List the hashes for all exports from kernel32.dll (As found in 'c:\windows\system32\')11# >hash.py /mod c:\windows\system32\ kernel32.dll12#13# Example: Simply print the correct hash value for the function kernel32.dll!WinExec14# >hash.py kernel32.dll WinExec15#16# Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)17#=============================================================================#18import pefile19from sys import path20import os21import time22import sys2324# Modify this path to pefile to suit your machine...25pefile_path = 'D:\\Development\\Frameworks\\pefile\\'2627path.append(pefile_path)28collisions = [(0x006B8029, 'ws2_32.dll!WSAStartup'),29(0xE0DF0FEA, 'ws2_32.dll!WSASocketA'),30(0x6737DBC2, 'ws2_32.dll!bind'),31(0xFF38E9B7, 'ws2_32.dll!listen'),32(0xE13BEC74, 'ws2_32.dll!accept'),33(0x614D6E75, 'ws2_32.dll!closesocket'),34(0x6174A599, 'ws2_32.dll!connect'),35(0x5FC8D902, 'ws2_32.dll!recv'),36(0x5F38EBC2, 'ws2_32.dll!send'),3738(0x5BAE572D, 'kernel32.dll!WriteFile'),39(0x4FDAF6DA, 'kernel32.dll!CreateFileA'),40(0x13DD2ED7, 'kernel32.dll!DeleteFileA'),41(0xE449F330, 'kernel32.dll!GetTempPathA'),42(0x528796C6, 'kernel32.dll!CloseHandle'),43(0x863FCC79, 'kernel32.dll!CreateProcessA'),44(0xE553A458, 'kernel32.dll!VirtualAlloc'),45(0x300F2F0B, 'kernel32.dll!VirtualFree'),46(0x0726774C, 'kernel32.dll!LoadLibraryA'),47(0x7802F749, 'kernel32.dll!GetProcAddress'),48(0x601D8708, 'kernel32.dll!WaitForSingleObject'),49(0x876F8B31, 'kernel32.dll!WinExec'),50(0x9DBD95A6, 'kernel32.dll!GetVersion'),51(0xEA320EFE, 'kernel32.dll!SetUnhandledExceptionFilter'),52(0x56A2B5F0, 'kernel32.dll!ExitProcess'),53(0x0A2A1DE0, 'kernel32.dll!ExitThread'),5455(0x6F721347, 'ntdll.dll!RtlExitUserThread'),5657(0x23E38427, 'advapi32.dll!RevertToSelf')58]5960collisions_detected = {}61modules_scanned = 062functions_scanned = 06364def ror(dword, bits):65return (dword >> bits | dword << (32 - bits)) & 0xFFFFFFFF6667def unicode(string, uppercase=True):68result = ''69if uppercase:70string = string.upper()71for c in string:72result += c + '\x00'73return result7475def hash(module, function, bits=13, print_hash=True):76module_hash = 077function_hash = 078for c in unicode(module + '\x00'):79module_hash = ror(module_hash, bits)80module_hash += ord(c)81for c in str(function + b'\x00'):82function_hash = ror(function_hash, bits)83function_hash += ord(c)84h = module_hash + function_hash & 0xFFFFFFFF85if print_hash:86print('[+] 0x%08X = %s!%s' % (h, module.lower(), function))87return h8889def scan(dll_path, dll_name, print_hashes=False, print_collisions=True):90global modules_scanned91global functions_scanned92dll_name = dll_name.lower()93modules_scanned += 194pe = pefile.PE(os.path.join(dll_path, dll_name))95for export in pe.DIRECTORY_ENTRY_EXPORT.symbols:96if export.name is None:97continue98h = hash(dll_name, export.name, print_hash=print_hashes)99for (col_hash, col_name) in collisions:100if col_hash == h and col_name != '%s!%s' % (dll_name, export.name):101if h not in collisions_detected.keys():102collisions_detected[h] = []103collisions_detected[h].append(104(dll_path, dll_name, export.name))105break106functions_scanned += 1107108def scan_directory(dir):109for dot, dirs, files in os.walk(dir):110for file_name in files:111if file_name[-4:] == '.dll': # or file_name[-4:] == ".exe":112scan(dot, file_name)113print('\n[+] Found %d Collisions.\n' % (len(collisions_detected)))114for h in collisions_detected.keys():115for (col_hash, col_name) in collisions:116if h == col_hash:117detected_name = col_name118break119print('[!] Collision detected for 0x%08X (%s):' % (h, detected_name))120for (collided_dll_path, collided_dll_name, collided_export_name) in collisions_detected[h]:121print('\t%s!%s (%s)' %122(collided_dll_name, collided_export_name, collided_dll_path))123print('\n[+] Scanned %d exported functions via %d modules.\n' %124(functions_scanned, modules_scanned))125126def usage():127print(128'Usage: hash.py [/dir <path>] | [/mod <path> <module.dll>] | [<module.dll> <function>]')129130131def main(argv=None):132if not argv:133argv = sys.argv134if len(argv) == 1:135usage()136else:137print('[+] Ran on %s\n' % (time.asctime(time.localtime())))138if argv[1] == '/dir':139print("[+] Scanning directory '%s' for collisions..." % argv[2])140scan_directory(argv[2])141elif argv[1] == '/mod':142print("[+] Scanning module '%s' in directory '%s'..." %143(argv[3], argv[2]))144scan(argv[2], argv[3], print_hashes=True)145elif len(argv) < 3:146usage()147else:148hash(argv[1], argv[2])149150if __name__ == '__main__':151main()152153154