mersenne
January 10th, 2001, 22:52
Hi all,
This is my solution for task three which follows from my post above. In order to write a fixed value to the registry I figured I needed to find where the value was being incremented.
After disassembling the program in W32Dasm we can search the imported functions for the RegSetValueExA function (registry functions are found in advapi32.dll) we found using regmon. It is only called once in the entire program which makes our life easier

If you check your trusty win32 API reference for this function you will find the address of the data written to the registry is pushed at 467B08 and is stored at ebp-04.
:00467AFF 8BF0 mov esi, eax
:00467B01 8B450C mov eax, dword ptr [ebp+0C]
:00467B04 50 push eax
:00467B05 8B45FC mov eax, dword ptr [ebp-04]
:00467B08 50 push eax
:00467B09 56 push esi
:00467B0A 6A00 push 00000000
:00467B0C 8BC7 mov eax, edi
:00467B0E E82DC4F9FF call 00403F40
:00467B13 50 push eax
:00467B14 8B4304 mov eax, dword ptr [ebx+04]
:00467B17 50 push eax
* Reference To: advapi32.RegSetValueExA, Ord:0000h
|
:00467B18 E8FFE8F9FF Call 0040641C
Time to start tracing back through the code looking for references to ebp-04. If you move up the disassembly a little way you will come to the following
:00467AE2 894DFC mov dword ptr [ebp-04], ecx
where the updated value gets moved from ecx to ebp-04. So now we need to look for references to ecx. Keep tracing up the code and you will see that this code is referenced by two calls
* Referenced by a CALL at Addresses:
|:00467A85 , :00467A9C
Let's look at the second one. Directly before the call is made the value we are interested in is moved into ecx from the stack at address esp+8. Three lines further up the value from ecx is moved to the stack adress esp (which incidently is the same as esp+8 further down due to the two pushes).
* Referenced by a CALL at Address:
|:004680BE
|
:00467A90 51 push ecx
:00467A91 890C24 mov dword ptr [esp], ecx
:00467A94 6A04 push 00000004
:00467A96 6A03 push 00000003
:00467A98 8D4C2408 lea ecx, dword ptr [esp+08]
:00467A9C E833000000 call 00467AD4
So we are still interested in ecx, lets keep tracing up to where this code is called from, namely 4680BE.
* Referenced by a (U)nconditional or (C)onditional Jump at Address:
|:004680A3(U)
|
:004680B1 8D7701 lea esi, dword ptr [edi+01]
:004680B4 8BCE mov ecx, esi
:004680B6 BAA4814600 mov edx, 004681A4
:004680BB 8B45FC mov eax, dword ptr [ebp-04]
:004680BE E8CDF9FFFF call 00467A90
At 4680B4 the value in esi is moved into ecx and directly before that we see that value in esi=edi + 1 which is exactly what we are looking for. It is good to confirm this "live" in a debugger, which I did. So how do we patch it? If we change
:004680B1 8D7701 lea esi, dword ptr [edi+01]
to
:004680B1 8D37 lea esi, dword ptr [edi]
:004680B3 90 nop
then whatever value we have in the registry will remain and never get incremented. Problem solved

If you have any questions, just ask and help will be forthcoming.
Regards
Mersenne