The goal of this library is to securely mask imports, so they cannot be found on-disk, and so that finding what is called during runtime becomes more difficult.
Code:
#include "fxn.h"
int main()
{
fxn i;
try
{
i.SetFunction("user32.dll", HASH("MessageBoxA"));
i.Call(NULL, "Hi", "ANSI (MessageBoxA)", MB_OK);
i.SetFunction("user32.dll", HASH("MessageBoxW"));
i.Call(NULL, L"Hi", L"Unicode (MessageBoxW)", MB_OK);
}
catch(std::runtime_error& e)
{
std::cout << e.what() << std::endl;
std::cin.ignore();
}
return (0);
}
The library will first attempt to locate the library in memory (Using a custom GetModuleHandle rewrite), then it'll call LoadLibrary to load it manually (LoadLibrary being hidden in the imports, of course).
The get procedure address function (Rewrite of GetProcAddress) does not support forwarded functions, sorry, it's just a feature I consider unnecessary.
Imports are located in the export table of the module in memory; as an extra little trick, if any NOP's are located before the function, the call is offsetted by X number of NOP's, making none of the addresses being called resolve to anything in the EAT of the DLLs.
I don't use return-code logic for error checking, I use C++ exceptions (try/catch); you could change that yourself if you want, nothing I wrote is terribly C++ specific (Classes, use of reinterpret cast, command line IO, and C++ exceptions are about it).
One other security benefit: a hash for the requested function to be imported is handed off, not the raw string; you never actually see the raw string in memory or on disk, making import recovery that much more of a pain in the ass.
Diagram explaining what SetFunction does:

Have fun!

UPDATE:
- The address to the function stored is now encrypted.
- Updated compile-time string hashing; strings up to a length of 32 characters are supported now.
It's recommended that you change the hash seed, the number being used for creating the checksum:
Code:
static const unsigned short hash_seed = 0x6281;
I forgot to add, the library supports three compilers right now: GCC, MSVC, and ICC. The only compiler-specific item that the library uses is inline assembly for reading the FS/GS register; for MSVC, I use intrinsics to do so (Giving the library x86_64 compatibility), and for GCC/ICC, I use rewrites of said intrinsic functions using inline assembly.
I forgot to add a special thanks to NTInternals.net for their documenting of undocumented structures, such as the PEB, which I used in my GetModuleHandle rewrite.

Download: http://www.sendspace.com/file/25cb4e


Section Widget
Category Widget (bottom-up)
30 Comments
Recent Article Comments Widget

Rate this article