Daeth Posted September 12, 2016 Posted September 12, 2016 I'm trying to dump a process' memory to a file in the temporary directory, similar to Microsoft's ProcDump. The code uses the MinidumpWriteDump function in the dbghelp.dll. Here is the following code. (You need to open Notepad to start) expandcollapse popup#NoTrayIcon #RequireAdmin #include <WinAPI.au3> Global Const $MiniDumpNormal = "0x00000000" Global Const $MiniDumpWithDataSegs = "0x00000001" Global Const $MiniDumpWithFullMemory = "0x00000002" Global Const $MiniDumpWithHandleData = "0x00000004" Global Const $MiniDumpFilterMemory = "0x00000008" Global Const $MiniDumpScanMemory = "0x00000010" Global Const $MiniDumpWithUnloadedModules = "0x00000020" Global Const $MiniDumpWithIndirectlyReferencedMemory = "0x00000040" Global Const $MiniDumpFilterModulePaths = "0x00000080" Global Const $MiniDumpWithProcessThreadData = "0x00000100" Global Const $MiniDumpWithPrivateReadWriteMemory = "0x00000200" Global Const $MiniDumpWithoutOptionalData = "0x00000400" Global Const $MiniDumpWithFullMemoryInfo = "0x00000800" Global Const $MiniDumpWithThreadInfo = "0x00001000" Global Const $MiniDumpWithCodeSegs = "0x00002000" Global Const $MiniDumpWithoutAuxiliaryState = "0x00004000" Global Const $MiniDumpWithFullAuxiliaryState = "0x00008000" Global Const $MiniDumpWithPrivateWriteCopyMemory = "0x00010000" Global Const $MiniDumpIgnoreInaccessibleMemory = "0x00020000" Global Const $MiniDumpWithTokenInformation = "0x00040000" Global Const $MiniDumpWithModuleHeaders = "0x00080000" Global Const $MiniDumpFilterTriage = "0x00100000" Global Const $MiniDumpValidTypeFlags = "0x001fffff" Global $iProcessPID = ProcessWait("notepad.exe") Global $hProcess = _WinAPI_OpenProcess("0x0400", 0, $iProcessPID) Global $hFile = _WinAPI_CreateFile(@TempDir & "\test.dmp", 1) ConsoleWrite("$iProcessPID = " & $iProcessPID & @CRLF & "$hProcess = " & $hProcess & @CRLF & "$hFile = " & $hFile & @CRLF) DumpFile($hProcess, $iProcessPID, $hFile, $MiniDumpWithFullMemory) _WinAPI_CloseHandle($hFile) _WinAPI_CloseHandle($hProcess) Exit Func DumpFile($hProcess, $iPID, $hFile, $dDumpType) $hDLL = DllOpen(@SystemDir & "\dbghelp.dll") $aResult = DllCall($hDLL, "BOOL", "MiniDumpWriteDump", "HANDLE", $hProcess, "DWORD", $iPID, "HANDLE", $hFile, "DWORD", $dDumpType, "DWORD", Null, "DWORD", Null, "DWORD", Null) DllClose($hDLL) ConsoleWrite($aResult[0]) EndFunc $aResult[0] always returns 0, and the "test.dmp" file is always 0 kilobytes.
JohnOne Posted September 12, 2016 Posted September 12, 2016 Probably need to add $PROCESS_VM_READ Global $hProcess = _WinAPI_OpenProcess(BitOr($PROCESS_QUERY_INFORMATION, $PROCESS_VM_READ), 0, $iProcessPID) ;0x00000410 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Daeth Posted September 12, 2016 Author Posted September 12, 2016 (edited) @JohnOne I still get a return value of 0 with that code. I tried with this, but still to no avail: Global $hProcess = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, 0, $iProcessPID, True) Could there be anything wrong with the DllCall? Edited September 12, 2016 by Daeth
PACaleala Posted September 12, 2016 Posted September 12, 2016 @OP: you should be content - that zero as a return value means "success" a dump file was created!
Daeth Posted September 12, 2016 Author Posted September 12, 2016 @PACaleala No, according to MSDN, it says the return value should be True if a successful dump file was written. Furthermore, the dump file created is 0 bytes.
PACaleala Posted September 12, 2016 Posted September 12, 2016 Comment the require admin line and insert the next line before the "Exit" line: if FileExists(@TempDir & "\test.dmp") Then run ("notepad" & " " & @TempDir & "\test.dmp") Now run the script from SciTe.
Daeth Posted September 12, 2016 Author Posted September 12, 2016 What is that meant to do? There's nothing in the dumpfile.
Terenz Posted September 12, 2016 Posted September 12, 2016 #include <WinAPI.au3> ;~ #RequireAdmin try to un-comment if not work for you Local $hFile = _WinAPI_CreateFile(@ScriptDir & "\Test.dmp", 1) ; Creates a new file. If a file exists, it is overwritten _DumpFile(@AutoItPID, $hFile) _WinAPI_CloseHandle($hFile) Func _DumpFile($iPID, $hFile, $dDumpType = 0) Local $hProcess = DllCall("kernel32.dll", "handle", "OpenProcess", "dword", 0x0450, "bool", 0, "dword", $iPID) If @error Then Return SetError(@error, @extended, 0) $aResult = DllCall("dbghelp.dll", "bool", "MiniDumpWriteDump", "handle", $hProcess[0], "dword", $iPID, "handle", $hFile, "dword", $dDumpType, "dword", "", "dword", "", "dword", "") DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hProcess[0]) If $aResult[0] = 0 Then Return SetError(@error, @extended, False) Return $aResult[0] EndFunc Daeth and JohnOne 2 Nothing is so strong as gentleness. Nothing is so gentle as real strength
Daeth Posted September 13, 2016 Author Posted September 13, 2016 @Terenz Hmm that's odd, your code writes a dump file for @AutoItPID, so I tried using @AutoItPID, in my script as well - which actually works. How do I create a dump file of a system process or "notepad.exe". I tested the DumpFile on different applications such as "chrome.exe", but "notepad.exe" doesn't work. When I use the sysinternals 'ProcDump' tool and create a process dump of notepad.exe (procdump -ma notepad.exe), it worked fine.
Terenz Posted September 13, 2016 Posted September 13, 2016 ? #include <WinAPI.au3> ;~ #RequireAdmin try to un-comment if not work for you Local $iPID = Run("notepad.exe") ;~ Local $iPID = ProcessWait("notepad.exe") Local $hFile = _WinAPI_CreateFile(@ScriptDir & "\Test.dmp", 1) ; Creates a new file. If a file exists, it is overwritten _DumpFile($iPID, $hFile) _WinAPI_CloseHandle($hFile) ProcessClose($iPID) Func _DumpFile($iPID, $hFile, $dDumpType = 0) Local $hProcess = DllCall("kernel32.dll", "handle", "OpenProcess", "dword", 0x0450, "bool", 0, "dword", $iPID) If @error Then Return SetError(@error, @extended, 0) $aResult = DllCall("dbghelp.dll", "bool", "MiniDumpWriteDump", "handle", $hProcess[0], "dword", $iPID, "handle", $hFile, "dword", $dDumpType, "dword", "", "dword", "", "dword", "") DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $hProcess[0]) If $aResult[0] = 0 Then Return SetError(@error, @extended, False) Return $aResult[0] EndFunc Daeth 1 Nothing is so strong as gentleness. Nothing is so gentle as real strength
JohnOne Posted September 13, 2016 Posted September 13, 2016 Are you compiling 32 bit? Perhaps notepad is 64 bit. Daeth 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Daeth Posted September 13, 2016 Author Posted September 13, 2016 @JohnOne you're a genius! That did the trick. How did you know that would solve the problem?
JohnOne Posted September 13, 2016 Posted September 13, 2016 32 bit processes cannot read 64 bit, but 64 bit can read both. Daeth 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now