Пример #1
0
def launch(exepath):
    # on Linux and MacOS, use ld/dyld
    if platform.system() == 'Linux':
        os.environ['LD_PRELOAD'] = REMOTEPATH
    if platform.system() == 'Darwin':
        os.environ['DYLD_INSERT_LIBRARIES'] = REMOTEPATH
        os.environ['DYLD_FORCE_FLAT_NAMESPACE'] = '1'

    # start the game
    gpop = subprocess.Popen([exepath],
                            stderr=subprocess.STDOUT,
                            stdout=subprocess.PIPE,
                            cwd=os.path.dirname(exepath))

    # on Windows, inject into the running process
    if platform.system() == 'Windows':
        try:
            proc = NativeProcess(pid=gpop.pid)
            lib_h = proc.load_library(REMOTEPATH)
            # start remote thread
            addr = lib_h + STARTUP_OFFSET
            thr = proc.start_thread(addr)
            logging.info('starting from 0x{:x}'.format(addr))
            proc.join_thread(thr)
        except ProcessError as error:
            gpop.kill()
            logging.error(error.msg)
            return False

    return gpop
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        description='memgrep: memory search utility',
        conflict_handler='resolve')
    parser.add_argument('pid',
                        action='store',
                        type=int,
                        help='process to control')
    parser.add_argument('search_data',
                        action='store',
                        help='data to search for')
    parser.add_argument('-e',
                        '--encoding',
                        default='utf-8',
                        help='the encoding of search_data')
    arguments = parser.parse_args()

    search_data = arguments.search_data.encode(arguments.encoding)
    if len(search_data) < 4:
        print(
            '[-] searching for less than 4 bytes will yield too many results')
        return 0

    process_h = NativeProcess(pid=arguments.pid)
    print("[*] searching {0} regions of memory".format(len(process_h.maps)))

    num_matches = 0
    num_skips = 0
    num_errors = 0

    for mem_region in process_h.maps.values():
        print("[*] searching 0x{0:08x} - 0x{1:08x} (0x{2:08x} bytes)".format(
            mem_region.addr_low, mem_region.addr_high, mem_region.size))
        if not mem_region.is_readable:
            print("[-] skipped unreadable region at 0x{0:08x}".format(
                mem_region.addr_low))
            num_skips += 1
            continue
        try:
            data = process_h.read_memory(mem_region.addr_low, mem_region.size)
        except ProcessError as error:
            print("[-] encountered {0} while reading at 0x{1:08x}".format(
                error.__class__.__name__, mem_region.addr_low))
            num_errors += 1
            continue
        cursor = data.find(search_data)
        while cursor != -1:
            data_slice = data[align_down(cursor):align_up(cursor +
                                                          len(search_data))]
            low_addr = align_down(mem_region.addr_low + align_down(cursor))
            print("[+] found match at 0x{0:08x}".format(mem_region.addr_low +
                                                        cursor))
            num_matches += 1
            print_hexdump(data_slice, low_addr)
            cursor = data.find(search_data, cursor + 1)

    process_h.close()
    print("[*] summary - matches: {0} errors: {1} skipped regions: {2}".format(
        num_matches, num_errors, num_skips))
Пример #3
0
def main():
    parser = argparse.ArgumentParser(
        description='syringe: library & shellcode injection utility',
        conflict_handler='resolve',
        epilog=
        'The PID argument can be specified as -1 to inject into the context of the syringe process.'
    )
    parser.add_argument('-l',
                        '--load',
                        dest='library',
                        action='store',
                        help='load the library in the target process')
    shellcode_group = parser.add_mutually_exclusive_group()
    shellcode_group.add_argument('-i',
                                 '--inject',
                                 dest='shellcode',
                                 action='store',
                                 help='inject code into the process')
    shellcode_group.add_argument(
        '-f',
        '--inject-file',
        dest='shellcode_file',
        type=argparse.FileType('rb'),
        help='inject code from a file into the process')
    parser.add_argument('-d',
                        '--decode',
                        dest='decode',
                        action='store',
                        choices=('b64', 'hex', 'raw'),
                        default='b64',
                        help='decode the shellcode prior to execution')
    parser.add_argument('pid',
                        action='store',
                        type=int,
                        help='process to control')
    arguments = parser.parse_args()

    try:
        process_h = NativeProcess(pid=arguments.pid)
    except ProcessError as error:
        print("[-] {0}".format(error.msg))
        return
    print("[+] Opened a handle to pid: {0}".format(arguments.pid))

    if arguments.library:
        try:
            lib_h = process_h.load_library(arguments.library)
        except ProcessError as error:
            print("[-] {0}".format(error.msg))
        else:
            print("[+] Loaded {0} with handle 0x{1:08x}".format(
                arguments.library, lib_h))

    if arguments.shellcode or arguments.shellcode_file:
        if arguments.shellcode:
            shellcode = arguments.shellcode
        else:
            shellcode = arguments.shellcode_file.read()
            arguments.shellcode_file.close()
        if arguments.decode == 'b64':
            shellcode = shellcode.decode('base64')
        elif arguments.decode == 'hex':
            shellcode = shellcode.decode('hex')
        stub = ''  # no stub by default
        if architecture_is_32bit(process_h.arch):
            stub = b'\x8b\x44\x24\x04'  # mov eax,[esp+4]
        elif architecture_is_64bit(process_h.arch):
            stub = b'\x48\x8b\x44\x24\x08'  # mov rax,[rsp+8]

        shellcode_sz = align_up(len(stub + shellcode), 1024)
        address = process_h.allocate(size=shellcode_sz, address=0)
        print("[+] Allocated {0} bytes at 0x{1:08x}".format(
            shellcode_sz, address))
        process_h.protect(address, size=shellcode_sz)
        process_h.write_memory(address, stub + shellcode)
        thread_id = process_h.start_thread(address, (address + len(stub)))
        print("[+] Started thread at 0x{0:08x}".format(address))
        print("[*] Waiting for thread to complete...")
        try:
            process_h.join_thread(thread_id)
            print("[+] Thread completed")
        except ProcessError as err:
            print("[-] {0} {1}".format(err.__class__.__name__, err.msg))

    process_h.close()