In my previous article Exploring the MS-DOS Stub I stated that after experimenting, the Windows loader only cares about the e_magic
and the e_lfanew
members from the _IMAGE_DOS_HEADER
. Because the rest of the members of the DOS header is used by MS-DOS to execute the stub program. Check it out if you have not.
If you take a PE file and null out the MS-DOS header and the MS-DOS stub program leaving out the e_magic
and the e_lfanew
values, the PE will still work fine as the rest is not needed by the Windows PE loader. The e_lfanew
address at offset 0x3c
is important as it points to the beginning of the _IMAGE_NT_HEADERS
structure which is the actual start of the PE file.
Since those values are not important we can insert an HTML comment from offset 0x2 which is the e_cblp
value and begin an HTML comment and end the comment at the end of the PE and append our HTML/PHP/ASP/JSP file contents.
I wrote a simple program in C to automate this task. You can provide your PE file and the HTML/PHP/ASP/JSP file to inject and it will generate an HTML file. You can rename the file into the extension you desire.
https://github.com/OsandaMalith/PE2HTML
Another thing to note is that in Windows, cmd.exe and rundll32 will treat any file with any extension as a valid PE as long as it begins with the IMAGE_DOS_SIGNATURE
.
By abusing these Windows features (bugs) we can execute our HTML files as executables as well as run in the web browser displaying HTML/PHP/ASP/JSP content.
You can run the newly created PE file with HTML extension or with any extension using cmd.
cmd /c file.html
Process explorer output would look like this.
Rundll32 does not validate any extensions, therefore you can execute any DLL with any extension.
By combining these features (bugs) an attacker can achieve social engineering. This won’t bypass any AV or any EDR. But will surely confuse the analyzer. Might be a handy trick to use at the last stage once your payload is undetectable.
A checksum check can be used to prevent attackers from modifying the MS-DOS header. But a skilled reverse engineer may find the checksum routine and patch it to bypass the anti-reversing technique.
The author takes no responsibility for any damage you cause. This is strictly written for educational purposes.
So how do you get the malicious HTML file to execute? Wouldn’t the user have to open the executable in a web browser for that to happen?
I have mentioned on how to execute the file. It will work on both the web browser and in CMD. You can also use a PE loader to execute.
It doesn’t work on the browser, but it works fine on the cmd,Can you make a video of how to use it ):
I manually nulled and added wanted html. it works fine in the browser but when I exec with cmd it gives me this error, “cannot run or start due to incompatibility with 64-bit windows” also that binary I have edited was 64bit compiled. any ideas?
Did you try using the code I have posted?
Yes but it takes some time to complete, anyhow I was able successfully add my HTML code to putty PE manually with the second try :D. Great tutorial !
Great!
Hello, i try this soft, but when html file not appending with payload, how solve this problem?
Did you try manually?
no via script, adding does not start after transferring the load to html, compiled with gcc, I would be grateful if you could throw off an example of the apended html or suggest how to fix the problem.