|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2009-2025, Mario Vilas |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# Redistribution and use in source and binary forms, with or without |
| 8 | +# modification, are permitted provided that the following conditions are met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice,this list of conditions and the following disclaimer in the |
| 14 | +# documentation and/or other materials provided with the distribution. |
| 15 | +# * Neither the name of the copyright holder nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived from |
| 17 | +# this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +# POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +import os |
| 32 | + |
| 33 | +from winappdbg import win32 |
| 34 | +from winappdbg.debug import Debug |
| 35 | +from winappdbg.textio import HexDump |
| 36 | + |
| 37 | + |
| 38 | +def my_event_handler(event): |
| 39 | + # Get the event name. |
| 40 | + name = event.get_event_name() |
| 41 | + |
| 42 | + # Get the event code. |
| 43 | + code = event.get_event_code() |
| 44 | + |
| 45 | + # Get the process ID where the event occured. |
| 46 | + pid = event.get_pid() |
| 47 | + |
| 48 | + # Get the thread ID where the event occured. |
| 49 | + tid = event.get_tid() |
| 50 | + |
| 51 | + # Get the value of EIP/RIP at the thread. |
| 52 | + pc = event.get_thread().get_pc() |
| 53 | + |
| 54 | + # Show something to the user. |
| 55 | + bits = event.get_process().get_bits() |
| 56 | + format_string = "%s (%s) at address %s, process %d, thread %d" |
| 57 | + message = format_string % ( |
| 58 | + name, |
| 59 | + HexDump.integer(code, bits), |
| 60 | + HexDump.address(pc, bits), |
| 61 | + pid, |
| 62 | + tid, |
| 63 | + ) |
| 64 | + print(message) |
| 65 | + |
| 66 | + # If the event is a crash... |
| 67 | + if code == win32.EXCEPTION_DEBUG_EVENT and event.is_last_chance(): |
| 68 | + print("Crash detected, generating minidump...") |
| 69 | + |
| 70 | + # Generate a filename based on the process name and exception code. |
| 71 | + process = event.get_process() |
| 72 | + filename = process.get_filename() |
| 73 | + if filename: |
| 74 | + basename = os.path.basename(filename) |
| 75 | + basename = os.path.splitext(basename)[0] |
| 76 | + else: |
| 77 | + basename = "process_%d" % pid |
| 78 | + |
| 79 | + exception_code = event.get_exception_code() |
| 80 | + minidump_filename = "%s_crash_%08X.dmp" % (basename, exception_code) |
| 81 | + |
| 82 | + try: |
| 83 | + # Generate a minidump with full memory and exception information. |
| 84 | + # The event.generate_minidump() method automatically includes |
| 85 | + # the exception context and exception record. |
| 86 | + event.generate_minidump( |
| 87 | + minidump_filename, |
| 88 | + DumpType=win32.MiniDumpWithFullMemory |
| 89 | + | win32.MiniDumpWithHandleData |
| 90 | + | win32.MiniDumpWithThreadInfo, |
| 91 | + ) |
| 92 | + print("Minidump saved to: %s" % minidump_filename) |
| 93 | + |
| 94 | + except Exception as e: |
| 95 | + print("Error generating minidump: %s" % e) |
| 96 | + import traceback |
| 97 | + |
| 98 | + traceback.print_exc() |
| 99 | + |
| 100 | + # You can also launch the interactive debugger from here. Try it! :) |
| 101 | + # event.debug.interactive() |
| 102 | + |
| 103 | + # Kill the process. |
| 104 | + event.get_process().kill() |
| 105 | + |
| 106 | + |
| 107 | +def simple_debugger(argv): |
| 108 | + # Instance a Debug object, passing it the event handler callback. |
| 109 | + debug = Debug(my_event_handler, bKillOnExit=True) |
| 110 | + try: |
| 111 | + # Start a new process for debugging. |
| 112 | + debug.execv(argv) |
| 113 | + |
| 114 | + # Wait for the debugee to finish. |
| 115 | + debug.loop() |
| 116 | + |
| 117 | + # Stop the debugger. |
| 118 | + finally: |
| 119 | + debug.stop() |
| 120 | + |
| 121 | + |
| 122 | +# When invoked from the command line, |
| 123 | +# the first argument is an executable file, |
| 124 | +# and the remaining arguments are passed to the newly created process. |
| 125 | +if __name__ == "__main__": |
| 126 | + import sys |
| 127 | + |
| 128 | + simple_debugger(sys.argv[1:]) |
| 129 | + |
0 commit comments