2020-06-11 11:19:40 -05:00
## On this page
* [EXE Example ](#exe-example )
* [DLL Example ](#dll-example )
* [Printf() ](#printf )
* [Custom Headers ](#custom-headers )
* [Code Randomization ](#code-randomization )
2018-12-11 08:44:46 -06:00
2018-05-21 12:35:14 -05:00
```Metasploit::Framework::Compiler::Windows` `` is a wrapper of [Metasm](https://github.com/jjyg/metasm) specifically for compiling C code for the Windows platform. The purpose of the wrapper is to support default headers, such as ` stdio.h`, ` stdio.h`, ` String.h`, ` Windows.h`, or some other important headers that you might use while writing in C.
2020-06-11 11:19:40 -05:00
## EXE example
2018-05-21 12:35:14 -05:00
2018-05-21 12:44:00 -05:00
` ``ruby
c_template = %Q|#include <Windows.h>
2018-05-21 12:35:14 -05:00
int main(void) {
2018-05-21 12:40:53 -05:00
LPCTSTR lpMessage = "Hello World";
LPCTSTR lpTitle = "Hi";
MessageBox(NULL, lpMessage, lpTitle, MB_OK);
2018-05-21 12:35:14 -05:00
return 0;
2018-05-21 12:44:00 -05:00
}|
require 'metasploit/framework/compiler/windows'
2018-05-21 12:49:45 -05:00
2020-06-11 11:19:40 -05:00
2023-10-06 19:51:39 -04:00
## Save as an exe variable
2018-05-21 12:44:00 -05:00
exe = Metasploit::Framework::Compiler::Windows.compile_c(c_template)
2018-05-21 12:49:45 -05:00
2020-06-11 11:19:40 -05:00
## Save the binary as a file
2018-05-21 12:49:45 -05:00
Metasploit::Framework::Compiler::Windows.compile_c_to_file('/tmp/test.exe', c_template)
` ``
2020-06-11 11:19:40 -05:00
## DLL example
2018-05-21 12:49:45 -05:00
` ``ruby
2018-10-11 14:22:46 +10:00
c_template = %Q|#include <Windows.h>
2018-05-21 12:49:45 -05:00
2018-05-22 11:19:40 -05:00
BOOL APIENTRY DllMain __attribute__((export))(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
2018-05-21 12:49:45 -05:00
switch (dwReason) {
case DLL_PROCESS_ATTACH:
MessageBox(NULL, "Hello World", "Hello", MB_OK);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
2018-05-22 11:12:59 -05:00
// This will be a function in the export table
2018-05-22 11:25:37 -05:00
int Msg __attribute__((export))(void) {
2018-05-22 11:12:59 -05:00
MessageBox(NULL, "Hello World", "Hello", MB_OK);
return 0;
}
2018-05-21 12:49:45 -05:00
|
2018-05-22 11:21:01 -05:00
require 'metasploit/framework/compiler/windows'
dll = Metasploit::Framework::Compiler::Windows.compile_c(c_template, :dll)
2018-05-21 12:35:14 -05:00
` ``
2018-05-21 17:23:36 -05:00
To load a DLL, you can use the LoadLibrary API:
` ``c
#include <Windows.h>
2018-05-21 17:23:58 -05:00
#include <stdio.h>
2018-05-21 17:23:36 -05:00
int main(void) {
HMODULE hMod = LoadLibrary("hello_world.dll");
if (hMod) {
printf("hello_world.dll loaded\n");
} else {
printf("Unable to load hello_world.dll\n");
}
}
` ``
2018-05-22 11:12:59 -05:00
Or call the function in export with rundll32:
` ``
rundll32 hell_world.dll,Msg
` ``
2020-06-11 11:19:40 -05:00
## Printf()
2018-05-21 12:40:53 -05:00
2020-06-11 11:19:40 -05:00
Methods like ` printf()` won't actually print anything, because it's not connected up to stdout. If you want to use ` printf()` for debugging purposes, consider using ` OutputDebugString`, or ` MessageBox`.
2018-05-21 12:40:53 -05:00
2020-06-11 11:19:40 -05:00
## Custom Headers
2018-05-21 12:35:14 -05:00
2018-06-29 14:39:53 -05:00
Currently, the Metasm wrapper does not support custom headers from an arbitrary location. To work around this, you can place your headers in ` data/headers/windows`, and then add that file name in ` lib/metasploit/framework/compiler/headers/windows.h`.
2020-06-11 11:19:40 -05:00
## Code Randomization
` Metasploit::Framework::Compiler` supports obfuscation that randomizes code at the source code level, and then compile. There are two methods we can use:
* ` Metasploit::Framework::Compiler::Windows.compile_random_c`
* ` Metasploit::Framework::Compiler::Windows.compile_random_c_to_file`
2018-06-29 14:39:53 -05:00
2020-06-11 11:19:40 -05:00
Metasploit::Framework::Compiler::Windows.compile_random_c_to_file example:
2018-06-29 14:39:53 -05:00
2018-07-01 08:38:25 -05:00
` ``ruby
2018-06-29 14:39:53 -05:00
require 'msf/core'
require 'metasploit/framework/compiler/windows'
c_source_code = %Q|
#include <Windows.h>
int main() {
const char* content = "Hello World";
const char* title = "Hi";
MessageBox(0, content, title, MB_OK);
return 0;
}|
outfile = "/tmp/helloworld.exe"
weight = 70 # This value is used to determine how random the code gets.
Metasploit::Framework::Compiler::Windows.compile_random_c_to_file(outfile, c_source_code, weight: weight)
2023-10-06 19:51:39 -04:00
` ``