Skip to content
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

Fix: Allows 64-bit processes to modify the import table of 32-bit processes. #161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

number201724
Copy link
Contributor

Allows 64bit process to inject 32bit DLLs into 32bit process.
Since the modification is IAT, it seems that there is no problem.

@number201724
Copy link
Contributor Author

influence function: DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx

@number201724 number201724 changed the title Allows 64bit process to inject 32bit DLLs into 32bit process. Allows 64-bit processes to modify the import table of 32-bit processes. Jan 10, 2021
@sonyps5201314
Copy link
Contributor

In fact, on a 64-bit system, 32-bit processes can also read and write the memory of 64-bit processes. Through the NtWow64WriteVirtualMemory64/NtWow64ReadVirtualMemory64/NtWow64QueryInformationProcess64 series of APIs, rundll32 and this direct memory read and write operation are all ways to access memory across processes. So you can also implement UpdateImports64 from a 32-bit process, if you want.

@number201724
Copy link
Contributor Author

number201724 commented Jan 12, 2021

In fact, on a 64-bit system, 32-bit processes can also read and write the memory of 64-bit processes. Through the NtWow64WriteVirtualMemory64/NtWow64ReadVirtualMemory64/NtWow64QueryInformationProcess64 series of APIs, rundll32 and this direct memory read and write operation are all ways to access memory across processes. So you can also implement UpdateImports64 from a 32-bit process, if you want.

This is not a documented API and may be changed in future versions.

detours will automatically detect the size of IMAGE_NT_HEADERS(32/64), so this patch is not a problem.

@sonyps5201314
Copy link
Contributor

What I mean is that you tried to remove the use of rundll32 in this way, but you did not completely remove it. Presumably the official team knew that it can be done like this. Maybe considering the undocumented API, they decided to use rundll32. Realize reading and writing the memory of non-current architecture processes

@number201724
Copy link
Contributor Author

What I mean is that you tried to remove the use of rundll32 in this way, but you did not completely remove it. Presumably the official team knew that it can be done like this. Maybe considering the undocumented API, they decided to use rundll32. Realize reading and writing the memory of non-current architecture processes

Its main modification is not DetourCreateProcessWithDll, but an improvement to DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx.

@sonyps5201314
Copy link
Contributor

DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx is just an internal call of DetourCreateProcessWithDll.

@number201724
Copy link
Contributor Author

number201724 commented Jan 16, 2021

DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx is just an internal call of DetourCreateProcessWithDll.

Some third-party programs may directly call DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx, such as anti-virus software.

@sonyps5201314
Copy link
Contributor

After the process is started, it is useless to update its import table.

@number201724
Copy link
Contributor Author

After the process is started, it is useless to update its import table.

e.g.: Set PsCreateProcessNotifyRoutine in the kernel, and then notify the user-level program.

@sonyps5201314
Copy link
Contributor

So since you have used the kernel-level modification, why not do the UpdateImports64 from 32bit process as well, so that the dependency on rundll32 is completely removed. Now you only remove one situation, which is incomplete. The kernel should It is easy to access the memory of a 32-bit process.

@number201724
Copy link
Contributor Author

So since you have used the kernel-level modification, why not do the UpdateImports64 from 32bit process as well, so that the dependency on rundll32 is completely removed. Now you only remove one situation, which is incomplete. The kernel should It is easy to access the memory of a 32-bit process.

Under normal circumstances, a 32-bit process cannot access the address space of a 64-bit process. On the contrary, 64-bit can easily access the 32-bit address space. Of course, there is an exception. The use of undocumented APIs is not reliable. Another reason is that the 32-bit operating system may not provide the Wow64 API you mentioned. Because the driver needs to notify the user-level program to do some related operations, and the kernel does not provide VirtualProtect-related APIs, you cannot safely modify the user-level memory protection.

So for 64-bit processes you need to consider two cases, the child is 32-bit or 64-bit.

@sonyps5201314
Copy link
Contributor

sonyps5201314 commented Jan 18, 2021

Another reason is that the 32-bit operating system may not provide the Wow64 API you mentioned

32-bit systems do not have 64-bit processes at all, so there is no need to use these WOW64 API functions

@bgianfo bgianfo added bug Something isn't working needs-attention 👋 This issue / or pull request requires maintainer attention. labels Feb 1, 2021
@bgianfo
Copy link
Contributor

bgianfo commented Mar 4, 2021

@number201724 can you rebase and add a test case to cover this scenario? (maybe in the new tests\ unit tests?)

@bgianfo bgianfo added needs-author-feedback This issue / or pull request requires author feedback before more action can be taken. and removed needs-attention 👋 This issue / or pull request requires maintainer attention. labels Mar 4, 2021
@bgianfo bgianfo changed the title Allows 64-bit processes to modify the import table of 32-bit processes. Fix: Allows 64-bit processes to modify the import table of 32-bit processes. Mar 5, 2021
@ghost ghost removed the needs-author-feedback This issue / or pull request requires author feedback before more action can be taken. label Mar 5, 2021
@number201724
Copy link
Contributor Author

Rebase

1 similar comment
@number201724
Copy link
Contributor Author

Rebase

@number201724 number201724 reopened this Mar 5, 2021
@number201724
Copy link
Contributor Author

@number201724 can you rebase and add a test case to cover this scenario? (maybe in the new tests\ unit tests?)

It seems that the test is more troublesome, because I use the software that drives the notification application layer to call DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx, I did not test other cases.
Maybe can try CreateProcess to create a 32-bit process and then test DetourUpdateProcessWithDll/DetourUpdateProcessWithDllEx. I will add it when I have time.

@bgianfo bgianfo self-assigned this Mar 6, 2021
@bgianfo bgianfo added the needs-author-feedback This issue / or pull request requires author feedback before more action can be taken. label Mar 6, 2021
@ghost
Copy link

ghost commented Mar 13, 2021

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days.

@jaykrell
Copy link
Member

jaykrell commented Jun 8, 2021

Separately, please change to C++ template.
And yes good idea imho.
Even 32bit to 64bit. Too bad the Win32 API is so limited.

@sonyps5201314
Copy link
Contributor

sonyps5201314 commented Jun 8, 2021

Even 32bit to 64bit. Too bad the Win32 API is so limited.

Yes, NtWow64WriteVirtualMemory64 can only be available from Windows Vista.

@ghost
Copy link

ghost commented Jun 15, 2021

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days.

@jaykrell
Copy link
Member

jaykrell commented Oct 2, 2022

I added those NtWow64ReadVirtualMemory64 functions, way back when.
At the time, whether we had 32bit tlist or 64bit tlist first in %PATH% varied and I wanted whatever was first to work, so I added them for 32bit tlist to use.
Again, it is unfortunate how limited the public Win32 API is.

Keep in mind, Detours does not patch IAT.
It patches code. IAT patching is so much easier and almost as effective.. and both are kinda hacky.

@ghost
Copy link

ghost commented Oct 9, 2022

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days.

@bgianfo bgianfo removed their assignment Oct 24, 2022
@ghost
Copy link

ghost commented Oct 31, 2022

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days.

@number201724
Copy link
Contributor Author

Separately, please change to C++ template. And yes good idea imho. Even 32bit to 64bit. Too bad the Win32 API is so limited.

I think if it is modified as a C++ template, a lot of code needs to be modified.
This functionality can currently be accomplished with minimal changes.

@ghost ghost removed needs-author-feedback This issue / or pull request requires author feedback before more action can be taken. status-no recent activity labels Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants