-
Notifications
You must be signed in to change notification settings - Fork 812
Unhook kernel32!BaseThreadInitThunk #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can you guarantee that the addresses returned by GetProcAddress and GetModuleHandle are the same for both the current process and the target process?
You saved the address of BaseThreadInitThunk for the current process in line 219 and wrote to this same address into the target process in line 235, but you can't assume for sure that the BaseThreadInitThunk function is located at the same address in both processes.
You're very likely overwriting unknown memory region in the target process thinking it holds the BaseThreadInitThunk when in reality that memory address may even crash the target process if manipulated.
|
My understanding is that kernel32 (as well as other dlls) are loaded at the
same base across processes to maximize memory sharing (at least on a
default install).
Cheers
Le mer. 14 oct. 2020 à 12:14, Frederico F. de Oliveira <
[email protected]> a écrit :
… ***@***.**** commented on this pull request.
How can you guarantee that the addresses returned by GetProcAddress and
GetModuleHandle are the same for both the current process and the target
process?
You saved the address of BaseThreadInitThunk for the *current process* in
line 219 and wrote to this same address into the *target process* in line
235, but you can't assume for sure that the BaseThreadInitThunk function
is located at the same address in both processes.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#17 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALIORLTKJFM5DGIARRVXWLSKX2BFANCNFSM4G5HYANA>
.
|
|
You're almost right, but there's a catch: when same DLLs are imported by different process they usually share the same physical memory address for those DLLs. But, the processes have different virtual addresses for them. For example, let's suppose that our rootkit.exe and firefox.exe uses the A good answer on how to get DLL functions address in a remote process is given here: https://stackoverflow.com/questions/26395243/getmodulehandle-for-a-dll-in-another-process Cheers |
|
I am familiar with how copy-on-write and how virtual memory works. Boot a
Windows 10 machine, open ProcessHacker2 on 5 different processes and notice
`kernel32` having the same VA in all of them.
It's been a well known behavior for years and I'm sure it's documented
somewhere.
Cheers
Le mer. 14 oct. 2020 à 13:20, Frederico F. de Oliveira <
[email protected]> a écrit :
… You're almost right, but there's a catch: when same DLLs are imported by
different process they *usually* share the same *physical memory address*
for those DLLs. But, the processes have different *virtual addresses* for
these DLL.
For example, let's suppose that our rootkit.exe and firefox.exe uses the
kernel32.dll. To use memory efficiently, Windows will load kernel32.dll
into physical address 0x003000 and share this addr with all processes that
uses this DLL instead of loading it to RAM each time. When starting
rootkit.exe process, it will map kernel32.dll into the virtual address
0x001000 that in reality maps to physical address 0x003000. When starting
Firefox.exe process, the kernel32.dll will be mapped at 0x008000 virtual
address that also maps to physical address 0x003000. (All these addresses
are fictitious) But it's important to note that despite sharing the same
physical address for the DLLs, the process have different virtual
addresses. When using GetProcAddress or GetModuleHandle or any other
function that deals with the process memory, they are returning virtual
addresses which is unique for each process.
A good answer on how to get DLL functions address in a remote process is
given here:
https://stackoverflow.com/questions/26395243/getmodulehandle-for-a-dll-in-another-process
Cheers
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#17 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALIORKPSSZMLEDSDEMB67TSKYBZFANCNFSM4G5HYANA>
.
|
|
Hmm, that's an interesting behavior, looks like the DLLs are not ASLR and I'm just not sure if this will be always the case. I'm also lacking documentation from my part. |
|
Though not finding official documentation, after some research I found out:
References:
|
Firefox on Windows hooks
kernel32!BaseThreadInitThunkwhich prevents the remote thread to start in the target, see https://dxr.mozilla.org/mozilla-central/source/mozglue/build/WindowsDllBlocklist.cpp#821:This PR restores the function prologue if it is detected as indeed hooked - happy to fix / adapt anything you'd like.
Also took the extra liberty of fixing the indentation in
Rva2Offset:).Cheers