ARTeam Tutorial

Removing Nag from CaptureNPrint 6.6.6.17


Information RCE on a Delphi program with Olly or with Dede
Target CaptureNPrint 6.6.6.17
Available http://www.geocities.com/blue_dragon_1964/main.html
Tools OllyDbg 1.10, CommandBar, MapConv Olly plugin by godfather+, DeDe 3.50 or above.
Protection Nag screen called at the beginning of the app
level Low of  made with DeDe, Intermediate if only with Olly
Category Patching
Author &8~) Shub-Nigurrath May 2004


1. Introduction

Hi all, today's target is CaptureNPrint. Its protection is quite simple, a simple Nag, but it's a Delphi program so, loading it into Olly shows you a lot of apparently unesplicable ASM.

Well I'll explain how I solved it the false steps and the correct solution both using Olly in two different ways and also using DeDe and Olly together....

The characteristics of this crack is that the program is written in Delphi (the assembly is a little different than with MFC programs). Moreover Delphi's program ASM is generally built in a way that doesn't allow to Olly to trace stack calls, so whenver you stop at a specific handler the call stack is generally empty and you don't exactly know from where you're coming. The only informations are the return addresses from the application stack.

There are three sections in the remaining of this tutorial:
1. Longest way using Olly and a lot of stack with now knowledge of how much different is the ASM of  the Delphi programs
2. Short way using Olly and some intelligence about how window handles are created and read
3. Very simple way using DeDe and Olly together.
 



2. Let first look at the program


The first Step is to look how the program acts. It creates a complex dialog with a timer that remains on top with a disabled button that would allow you to start the application.





3. Longest way using Olly and a lot of stack


Well, the first idea is to set a BP on Settimer, which is obviouslly involved in the dialog, but BP on SetTimer (using the CommandBar plugin) do not gives the expected results, because the call stack is empty. Anyway changing it I saw that this timer is the timer that determine the elapsed time between two intervals of the 30 that are observed by the Nag: 30 intervals of 1000 ms gives a 30 seconds elapsed time that's how the nag obblige you to stop.

Realized this you can search using Olly through all the program for all constants in the code for 30dec, 1Eh and placed a BP on each MOV that copies the value to some variable.

One demonstrated to be useful:

0052A2A0 C780 30030000>MOV DWORD PTR DS:[EAX+330],1E ; it's the point where the 30 cycles of 1000 ms is set. 

If I modify this the nag get's longer or shorter, excellent but not what I need to patch the application.

Now, looking at the call stack I noticed once more that the routine that build the whole nag is at the one at 0052AEAA (it's the routine that place buttons and text inside).

0052AEAA . E8 E5F1F1FF CALL snapit.0044A094

So, place the BP at this address and see before and after how the windows handles changes: before there's no Nag window, after there's one.

Before the call there are these windows

Windows
Handle Title Parent WinProc ID Style ExtStyle Thread ClsProc Class
00230374 Topmost 84000000 Main FFFF0557 DDEMLMom
002A0368 Topmost C4000000 Main DDEMLEvent
00540392 snapit Topmost 84CA0000 00000100 Main 004073AC TApplication
00580354 Topmost 04C00000 00000100 Main 004180F8 TThreadWindow


after the call there are these instead



Bingo, the whole frmAds hyerachy is new, did by the call above! Well, remember frmAds, well use it later on in the DeDe section of the tutorial.

Ok now we would see from who this nag is called and where. To do it press right click on the frmAds window and choose to Follow the ClassProc, you should land at 00431D88, and press F2 to set a breakpoint.

Ok, so, for the moment we stopped the program just after the call at 0052AEAA and we have a prepared windows, not yet shown. The window is ready so what we might thing is that the next thing the program will do on it will be to show it. So using the CommandBar command bar, place a BP ShowWindow to get a break at the each call of this api.

Well, the first break reports

0012FBAC 0044DBB7 /CALL to ShowWindow from snapit.0044DBB2
0012FBB0 002205F6 |hWnd = 002205F6 ('frmAds',class='TfrmAds',parent=001C05EC)
0012FBB4 00000001 \ShowState = SW_SHOWNORMAL


look, the handle given to the API is the same of the frmAds above: the program is trying to get the nag active!

Well, now see from who it has been called from!

Well, now it's time to climb up the code: the point where we stopped is called by 0044DBB2 and we have been directly taken there from the JNZ above, at 0044DADC

0044DADC . /0F85 B5000000 JNZ snapit.0044DB97

Do not try to modify it is's useless ;-). What we want to reach is the entry point of this call, becayse because the stack isn't of help (it's empty, damn Delphi!). So move up from jmp to jmp up to which could be the entry point of the procedure where we landed.

Well, there are several candidates point, remember that a procedure generically starts with a PUSH EBP that's used to save the return to caller address.
Proceed in this way: place a breakpoint on any suspected point, re-run the program and see where it stops. When you find a point, place a break just in the instruction above and see if it is hit: if not that's the entrypoint (I hope I have explained it correctly). This dicothomic approach is due because Olly isn't able to show the procedure start exactly, because everithing depends on the event maps Deplhi programs creates for handling events.

In our case the result is:

0044D884 . 55 PUSH EBP

it's an event handler, that's is referenced only buy the messagemap (use CTRL-R) and you  should get:

004471C8 . /84D84400 DD snapit.0044D884

Well, are we getting closer? It seems so ;-)

Set a BP on memory access at the instruction above and rerun the program

We land here:

0040312C |. 8B5C47 FC MOV EBX,DWORD PTR DS:[EDI+EAX*2-4] ; snapit.0044D884

that is called by:

004031A3 . E8 5CFFFFFF CALL snapit.00403104

the routine to which it belongs (use the same considerations above) is at this address:

00403190 . 53 PUSH EBX

and it's a very basic routine because if you press CTRL-R you'll see a lot of references!! Damn, we have reached the block!

Ho, let's go on with a second way!!
~~~~~~~~~~~~~~~~~~~~~~~

Well no worry, try another way: let's start using a SPY API tool, like Auto.Debug or the free http://www.rohitab.com/apimonitor/ and see which APIs are called (I'll not teach how to use these tools).

The question is: what happens to a window when it's called? It get's in foreground and it's activated! So if you spy through all the users32.dll exported APIs you would see that soon the program calls a GetActiveWindow at this point:

0044E0F7 |. E8 9093FBFF CALL <JMP.&user32.GetActiveWindow> ; [GetActiveWindow

Well, move up to the procedure's start at 0044E058

0044E058 /. 55 PUSH EBP

well re-run the app and stop at this point! The call stack cannot give you much, but the application stack gives you the return address that's the same thing: who called this routine!

0012FFA8 0052AEC6 RETURN to snapit.0052AEC6
0012FFAC 00FFFFFF
0012FFB0 7FFDF000
0012FFB4 0012FFE0 Pointer to next SEH record
0012FFB8 004037A8 SE handler


the caller is just above the instruction at 0052AEC6 and...it's a call directly from the main starting routine of the program.

Well this is the place to patch it, NOP the call and you're ready!

Original Code
0052AEC0 . FF92 CC000000 CALL DWORD PTR DS:[EDX+CC]
0052AEC6 . 8B06 MOV EAX,DWORD PTR DS:[ESI]
0052AEC8 . E8 0F61F2FF CALL snapit.00450FDC


Patched Code
0052AEC0 90 NOP
0052AEC1 90 NOP
0052AEC2 90 NOP
0052AEC3 90 NOP
0052AEC4 90 NOP
0052AEC5 90 NOP
0052AEC6 . 8B06 MOV EAX,DWORD PTR DS:[ESI]
0052AEC8 . E8 0F61F2FF CALL snapit.00450FDC


Save the patched code, save the program and run, you have removed the Nag.



4. Short way using Olly and some knowledge on window handles

Well, after having learnt the longest way try to use this one. Using the first steps of the above methos we still be able to reach the call which builds the window.

Do you remember the call at 0052AEAA? The one that creates the Nag screen? Well, place a Breakpoint on it (to discover it follow the early steps of the previos part of this tutorial).

0052AEAA E8 E5F1F1FF CALL snapit.0044A094

Execute the program from the beginning and once at the breakpoint press F8 to execute the call. No go to the Windows window of Olly and see which is the handle of the just created window (title is frmAds, remember). In my case is 0052AEAA, but each time is different, of course.

To obtain the Windows handle use Olly as in the picture below


 

Well, now set a preakpoint on EAX whenever it has the value of the handle

BP EAX=E0039012C

and press F9 to continue.
Why a breakpoint on EAX? Well, simple, the EAX register is always used to push on the stack the value of the handle that is always used to call Windows APIs. The concept is: once the nag windows has been created from the CaptureNPrint it wants for sure to make something with it and so it will call an API that will receive its handle.

We land here

004030D0 /$ 85C0 TEST EAX,EAX
004030D2 |. 74 10 JE SHORT snapit.004030E4
004030D4 |> 8B00 /MOV EAX,DWORD PTR DS:[EAX] <-- we landed here!
004030D6 |. 39D0 |CMP EAX,EDX
004030D8 |. 74 08 |JE SHORT snapit.004030E2
004030DA |. 8B40 DC |MOV EAX,DWORD PTR DS:[EAX-24]
004030DD |. 85C0 |TEST EAX,EAX
004030DF |.^ 75 F3 \JNZ SHORT snapit.004030D4
004030E1 |. C3 RETN
004030E2 |> B0 01 MOV AL,1
004030E4 \> C3 RETN

 

but it's not impoartant, the important thing is the application stack. We cannot use the Olly Call Stack window because it doesn't survives to Delphi ASM calls and thus isn't of help for us (in this case doesn't give anything at all).

The call stack taken from the application stack window in the lower right of Olly's assembly view, reports these things

0012FF90 0044C7D5 RETURN to snapit.0044C7D5 from snapit.004030D0
0012FF94 0288314C
0012FF98 005437B4 snapit.005437B4
0012FF9C 00000070
0012FFA0 00529DCC snapit.00529DCC
0012FFA4 0012FFC0
0012FFA8 0052AEC6 snapit.0052AEC6 <-- this is the important part!
0012FFAC 77D444A8 user32.77D444A8

 

well, quite interesting, the first place where the program has been executed is at 0052AEC6, directly called from user32.77D444A8 and at the same level of the call at the address 0052AEAA.

Excellent, go on that line in the application stack window and press enter. We land here in the CaptureNPrint application

0052AEC6 . 8B06 MOV EAX,DWORD PTR DS:[ESI]

This is the return address, of the call, so the call which interests us is above

0052AEC0 FF92 CC000000 CALL DWORD PTR DS:[EDX+CC]

Do you already know this line? It's the same line we patched with the other approach. NOP it and you're gone!




5. Very simple way using DeDe and Olly together

Well, now it's time to usa another tool that works better on Delphi programs than Olly. Deplhi program's assembler is quite different from MFC highly compact ASM and most of the language calls are directly placed inside the executable, so the resulting program is difficult to understand.

Well, fortunately exists DeDe, a disassembler specifically thought for Delphi and Borland programs (I'll do not explain how to use DeDe, you can find the latest copy here, http://www.wasm.ru/tools/13/DeDe.zip which has also inside some excellent generic tutorials).

Little theory for continuing. Delphi deals with global and locals variables references it on the following way: [ebp+xy] means that is pointer on global variable, [ebp-xy] means that it is pointer on local variable. Also be before pressing process that you loaded all the dsf files, from Option->Configuration->Symbols menu), them are very important to let DeDe correctly resolve the Delphi calls inside the call.

Now, simply run Dede and select the CaptureNPrint program and press Process. After a while Dede finishes to elaborate the program and what you will get is a list of form, classes and variables with thei relative handlers. Dede takes you where the important code is.

Well, after the processing are finished you should get something like below



Do you see the ads form of the class TfrmAds? Do you also remember the frmAds window we met earlier? It is the right one. Well, go on the DeDe's Form tag and press on twice on TfrmAds, and see if it is what we think it might be..you'll obtain this

Moreover there are some buttons like startme, Order and others..it's definitely our old dear Nag window!

Well, see which are the events this window uses, just to underline how much different is now the asm that Olly was showing to you!

Go to Procedures tag and select the FormActivate handler or the TfrmAds. You'll see a much more clear code. Let's give a short example..

in Dede
* Possible String Reference to: 'question and answer'
|
0052A41B BA78A55200 mov edx, $0052A578
0052A420 8D8534FEFFFF lea eax, [ebp+$FFFFFE34]
* Reference to: system.@Write0Bool;
|
0052A426 E8599DEDFF call 00404184
* Reference to: System.Proc_00406087
|
0052A42B E857BCEDFF call 00406087
* Reference to: System.Proc_004027E4
|
0052A430 E8AF83EDFF call 004027E4
0052A435 8D8534FEFFFF lea eax, [ebp+$FFFFFE34]
* Reference to: System.Proc_00405C3C
|
0052A43B E8FCB7EDFF call 00405C3C
* Reference to: System.Proc_004027E4
|
0052A440 E89F83EDFF call 004027E4
0052A445 E9E7000000 jmp 0052A531
0052A44A 8D952CFEFFFF lea edx, [ebp+$FFFFFE2C]
* Reference to TApplication instance
|
0052A450 A118215400 mov eax, dword ptr [$00542118]
0052A455 8B00 mov eax, [eax]

In Olly
0052A41B |. BA 78A55200 MOV EDX,snapit.0052A578 ; ASCII "question and answer"
0052A420 |. 8D85 34FEFFFF LEA EAX,DWORD PTR SS:[EBP-1CC]
0052A426 |. E8 599DEDFF CALL snapit.00404184
0052A42B |. E8 57BCEDFF CALL snapit.00406087
0052A430 |. E8 AF83EDFF CALL snapit.004027E4
0052A435 |. 8D85 34FEFFFF LEA EAX,DWORD PTR SS:[EBP-1CC]
0052A43B |. E8 FCB7EDFF CALL snapit.00405C3C
0052A440 |. E8 9F83EDFF CALL snapit.004027E4
0052A445 |. E9 E7000000 JMP snapit.0052A531
0052A44A |> 8D95 2CFEFFFF LEA EDX,DWORD PTR SS:[EBP-1D4]
0052A450 |. A1 18215400 MOV EAX,DWORD PTR DS:[542118]
0052A455 |. 8B00 MOV EAX,DWORD PTR DS:[EAX]
 

isn't much clear now..Dede clearly tells to you what is Delphi and what's i program's logic!

But Olly isn't out! The major drwaback of Dede is that doesn't allow live patching and the jumps are not highlighted, so long procedure aren't easy to follow. Fortunately there's an interesting plugin that allows to import .map files in the format of SoftICE or IDA into Olly: MapConv.

Well, go into DeDe in the tab Exports and select to export in MAP/SYM format for IDA/SoftIce and save all. Now go into Olly and select to import comment and labels with MapConv. The same portion of code earlier not so clearly shown by Olly now becomes this one.

In Olly with MapConv imported comments and labels
0052A41B |. BA 78A55200 MOV EDX,snapit.0052A578 ; ASCII "question and answer"
0052A420 |. 8D85 34FEFFFF LEA EAX,DWORD PTR SS:[EBP-1CC]
0052A426 >|. E8 599DEDFF CALL snapit.00404184 ; ->system.@Write0Bool;
0052A42B >|. E8 57BCEDFF CALL snapit.00406087 ; ->System.Proc_00406087
0052A430 >|. E8 AF83EDFF CALL snapit.004027E4 ; ->System.Proc_004027E4
0052A435 |. 8D85 34FEFFFF LEA EAX,DWORD PTR SS:[EBP-1CC]
0052A43B >|. E8 FCB7EDFF CALL snapit.00405C3C ; ->System.Proc_00405C3C
0052A440 >|. E8 9F83EDFF CALL snapit.004027E4 ; ->System.Proc_004027E4
0052A445 |. E9 E7000000 JMP snapit.0052A531
0052A44A |> 8D95 2CFEFFFF LEA EDX,DWORD PTR SS:[EBP-1D4]
0052A450 |. A1 18215400 MOV EAX,DWORD PTR DS:[542118]
0052A455 |. 8B00 MOV EAX,DWORD PTR DS:[EAX]


Now it's really possible to still use Olly and take beside Dede to jummp across references.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Well, but take again our scope: remove the Nag of CaptureNPrint!
Looking at the event table of the TfrmAds we see this:

TfrmAds.startmeTimer 0052A188
TfrmAds.FormCreate 0052A2A0
TfrmAds.AdsClickThrough 0052A2B8
TfrmAds.lblOrderClick 0052A2C0
TfrmAds.FormActivate 0052A35C
TfrmAds.MainAdImageClick 0052A6AC
TfrmAds.lblFreeClick 0052A7E4
TfrmAds.lblRegisterClick 0052A8C8
TfrmAds.btnStartClick 0052A91C
TfrmAds._PROC_0052A5E4 0052A5E4
TfrmAds._PROC_0052A925 0052A925
TfrmAds.ads.Initialization 0052A954
TfrmAds.ads.Finalization 0052A924

We really want to remove the call that activates the application and not to patch the dialog itself, so all these procedures are useless for us, because can only modify the dialog's behaviour but it remains active. The solution is to see where the TfrmAds is created or referenced from other procedures. DeDe interface doesn't allow to do this easily but offers all the instruments to make it.

Go into the Project folder and select the export options as follows


 

"Include PAS files" is uncecked because it's useless and takes a lot of time (it recreate a .pas source with asm code inside). Remember it when you'll create keygens :-)

Well Dede creates a lot of dfm files (containing exports from the program, see DeDe help to undestand what they are) and a snapit.dpr file. Open it with a notepad and make a search for TfrmAds!

After some references to the object we'll find this point!

* Reference to method TfrmAds.ShowModal()
|
0052AEC0 FF92CC000000 call dword ptr [edx+$00CC]
0052AEC6 8B06 mov eax, [esi]

Do you remenber, it's the same point where we landed in the two previous methods and the solution is once again to NOP the call..you now also understand what that damn call was, a simple ShowModal!

 


6. Making the Target Run


Well, run the program and see that the nag has been removed, succesfully. This is the only limitation of the program. Of course if you'll like it buy it, costs very few and author needs to eat also ^__^



7. Conclusion


A BIG thanks goes to, +DaFixer, all ARTeam members
and all the other from who I have learnt so much as well as the crew on exetools & other places..
This tute would not have been possible without their hard work
and willingness to pass the knowledge on to others.

I hope someone may find this tut useful

Best Wishes

&8~) Ŝħůβ¬Ňïĝµŕřāŧħ ₪