Click to See Complete Forum and Search --> : [RESOLVED] store a string in an executable
Makiman
June 21st, 2010, 08:36 AM
hello,
maybe someone can give me a brief instruction how to achieve this best:
I would like to program a tool which stores a string in an executable. This string should somehow be created from for example the size of the executable and the checksum or something like that. But that's not that important at the moment.
I just want to know how to store that string and how to find it again in the exe to verify it. And this exe should of course be still executable.
I would appreciate any help.
Kind regards
hoxsiew
June 21st, 2010, 09:15 AM
Since you're posting in the WinAPI forum, I'll assume this is a windows exe. The easiest way I could think of is to store the string as a resource the same as an icon or a bitmap.
Alternatively, you could declare a new section in the PE executable structure with a pragma:
#pragma code_seg(push, rl,".mysection")
const char MyString[]="Hello Hackers!";
#pragma code_seg(pop)
then, after compilation, parse the exe file for that section, but the PE structure is rather complicated and difficult to parse.
Makiman
June 21st, 2010, 09:38 AM
thank you for your answer.
yes, it is a windows exe. Storing it as a resource sounds good. But how can I programmatically add a resource to a different executable? Is that possible?
I started to read
http://www.codeguru.com/cpp/w-p/win32/security/article.php/c11393__1/Inject-Your-Code-to-a-Portable-Executable-File.htm
and this PE structure really seems to be rather complicated. But the sample projects at the end of the page may be usefull at least.
hoxsiew
June 21st, 2010, 11:24 AM
You can get a module handle to any EXE with LoadLibrary(). With that handle, you can call FindResource(), LoadResource(), then LockResource() will get you a memory pointer to the raw resource and SizeOfResource() will get you the length.
VladimirF
June 21st, 2010, 05:43 PM
I would like to program a tool which stores a string in an executable. This string should somehow be created from for example the size of the executable and the checksum or something like that...You already have such a tool – linker.
Check out its /RELEASE option.
Makiman
June 22nd, 2010, 06:44 AM
i did the following and it seems to be exactly what I need
for storing:
HGLOBAL hResLoad;
HMODULE hExe;
HRSRC hRes;
HANDLE hUpdateRes;
TCHAR *lpResLock;
BOOL result;
hUpdateRes = BeginUpdateResource(_T("c:\\Example_1.exe"),false);
TCHAR* str = _T("abc123");
result = UpdateResource(hUpdateRes,
RT_STRING,
_T("KEY"),
MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
str,
wcslen(str)*2);
EndUpdateResource(hUpdateRes,false);
well, I am not completely happy about that "wcslen(str)*2" to deterimine the needed size. Maybe anyone has a better idea about that.
for reading:
hExe = LoadLibrary(_T("c:\\Example_1.exe"));
hRes = FindResource(hExe, _T("KEY"), RT_STRING);
hResLoad = LoadResource(hExe, hRes);
lpResLock = (TCHAR*)LockResource(hResLoad);
Thanks a lot to all!
hoxsiew
June 22nd, 2010, 07:59 AM
When you use the "T" macros for ansi/unicode compatability, you need to use them throughout. Use tclen() and sizeof(TCHAR):
result = UpdateResource(hUpdateRes,
RT_STRING,
_T("KEY"),
MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
str,
tclen(str)*sizeof(TCHAR));
Makiman
June 22nd, 2010, 10:55 AM
thanks, it works!
VladimirF
June 22nd, 2010, 07:39 PM
So now you can modify and read back resource strings. Great!
But how does it help you with checksum verification?
Makiman
June 23rd, 2010, 02:36 AM
I still have to think about that algorithm to create that string to store in the exe and this algorithm must be secret. Checksum was only an example.
I have a COM-API to my application (not the tool to "sign") and I need a way to verify that the calls to API methods come from a "signed" executable. And only people with a specific license can use that signing tool.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.