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/build.py
Views: 11784
#!/usr/bin/env python31#=============================================================================#2# A simple python build script to build the singles/stages/stagers and3# some usefull information such as offsets and a hex dump. The binary output4# will be placed in the bin directory. A hex string and usefull comments will5# be printed to screen.6#7# Example:8# >python build.py stager_reverse_tcp_nx9#10# Example, to build everything:11# >python build.py all > build_output.txt12#13# Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)14#=============================================================================#15import os16import sys17import time18from subprocess import Popen19from struct import pack2021def clean(dir='./bin/'):22for root, dirs, files in os.walk(dir):23for name in files:24if name != '.keep':25os.remove(os.path.join(root, name))2627def locate(src_file, dir='./src/'):28for root, dirs, files in os.walk(dir):29for name in files:30if src_file == name:31return root32return None3334def build(name):35location = locate('%s.asm' % name)36if location:37input = os.path.normpath(os.path.join(location, name))38output = os.path.normpath(os.path.join('./bin/', name))39p = Popen(['nasm', '-f bin', '-O3', '-o %s.bin' %40output, '%s.asm' % input])41p.wait()42xmit(name)43else:44print("[-] Unable to locate '%s.asm' in the src directory" % name)4546def xmit_dump_ruby(data, length=16):47dump = ''48for i in range(0, len(data), length):49bytes = data[i: i+length]50hex = "\"%s\"" % (''.join(['\\x%02X' % x for x in bytes]))51if i+length <= len(data):52hex += ' +'53dump += '%s\n' % (hex)54print(dump)5556def xmit_offset(data, name, value, match_offset=0):57offset = data.find(value)58if offset != -1:59print('# %s Offset: %d' % (name, offset + match_offset))6061def xmit(name, dump_ruby=True):62bin = os.path.normpath(os.path.join('./bin/', '%s.bin' % name))63f = open(bin, 'rb')64data = bytearray(f.read())65print('# Name: %s\n# Length: %d bytes' % (name, len(data)))66xmit_offset(data, 'Port', pack('>H', 4444)) # 444467xmit_offset(data, 'LEPort', pack('<H', 4444)) # 444468xmit_offset(data, 'Host', pack('>L', 0x7F000001)) # 127.0.0.169xmit_offset(data, 'IPv6Host', pack(70'<Q', 0xBBBBBBBBBBBBBBB1)) # An IPv6 Address71xmit_offset(data, 'IPv6ScopeId', pack(72'<L', 0xAAAAAAA1)) # An IPv6 Scope ID73# hostname filler74xmit_offset(data, 'HostName',75b'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\x00')76xmit_offset(data, 'RetryCounter', b'\x6a\x05', 1) # socket retry77xmit_offset(data, 'CodeLen', pack('<L', 0x12345678)) # Filler78xmit_offset(data, 'Hostname', b'https')79# kernel32.dll!ExitThread80xmit_offset(data, 'ExitFunk', pack('<L', 0x0A2A1DE0))81# kernel32.dll!ExitProcess82xmit_offset(data, 'ExitFunk', pack('<L', 0x56A2B5F0))83# kernel32.dll!SetUnhandledExceptionFilter84xmit_offset(data, 'ExitFunk', pack('<L', 0xEA320EFE))85xmit_offset(data, 'ExitFunk', pack('<L', 0xE035F044)) # kernel32.dll!Sleep86xmit_offset(data, 'EggTag1', pack('<L', 0xDEADDEAD)) # Egg tag 187xmit_offset(data, 'EggTag2', pack('<L', 0xC0DEC0DE)) # Egg tag 288xmit_offset(data, 'EggTagSize', pack('>H', 0x1122)) # Egg tag size89xmit_offset(data, 'RC4Key', b'RC4KeyMetasploit') # RC4 key90xmit_offset(data, 'XORKey', b'XORK') # XOR key91if(name.find('egghunter') >= 0):92null_count = data.count('\x00')93if(null_count > 0):94print('# Note: %d NULL bytes found.' % (null_count))95if dump_ruby:96xmit_dump_ruby(data)9798def main(argv=None):99if not argv:100argv = sys.argv101if len(argv) == 1:102print('Usage: build.py [clean|all|<name>]')103else:104print('# Built on %s\n' % (time.asctime(time.localtime())))105if argv[1] == 'clean':106clean()107elif argv[1] == 'all':108for root, dirs, files in os.walk('./src/egghunter/'):109for name in files:110build(name[:-4])111for root, dirs, files in os.walk('./src/migrate/'):112for name in files:113build(name[:-4])114for root, dirs, files in os.walk('./src/single/'):115for name in files:116build(name[:-4])117for root, dirs, files in os.walk('./src/stage/'):118for name in files:119build(name[:-4])120for root, dirs, files in os.walk('./src/stager/'):121for name in files:122build(name[:-4])123for root, dirs, files in os.walk('./src/kernel/'):124for name in files:125build(name[:-4])126else:127build(argv[1])128129if __name__ == '__main__':130main()131132133