| 
			
				|  | VB Decompiler Hosted by TheAutomaters.com
 
 |  
 
	
		| Author | Message |  
		| _aLfa_ Site Admin
 
 
 Joined: 21 Sep 2002
 Posts: 233
 Location: Aveiro, Portugal
 
 | 
				
					| Posted: Fri Feb 15, 2008 10:54 pm
							    Post subject: VISUAL BASIC REVERSED - A decompiling approach |  
					| 
 |  
					| AndreaGeddon Reverse Engineering Team
 
 Abstract
 
 Keywords: Reverse Code Engineering, Visual Basic, VB, Decompiling
 
 Frameworks are getting more and more popular today, Visual Basic is one of them. Personally I hate frameworks, and also most reversers do. So, why this tutorial? We can consider both the light and the dark side of the problem: frameworks usually put a lot of code in the compiled programs, so it becomes hard to find the way among all that jungle. But they also use sets of pre-built objects, so these objects are always the same and can be recognized, helping the reverser to understand the code itself. In a VB PE you have alot of information inside the exe, so you can easily extract all the information you need about all components of the program. To analyze a VB application I used this program that was written by a friend of mine (thank you _d31m0s_!). It's a sort of name/serial crackme, but we are not interested in serial fishing, we are interested in how it works and how VB knows how to build the app itself. I asked my friend to write it adding some event handling (colors, on over, etc...) and a simple algorithm to check serial. He also wrote the proggy using more source files and making various subs (some null sub too). We also have the source of all, but we will check them later.
 
 Let's make some introduction now!
 _________________
 One thing only I know, and that is that I know nothing. (Socrates)
 |  |  
		| Back to top |  |  
		|  |  |  
		| _aLfa_ Site Admin
 
 
 Joined: 21 Sep 2002
 Posts: 233
 Location: Aveiro, Portugal
 
 | 
				
					| Posted: Fri Feb 15, 2008 10:57 pm
							    Post subject: 1 - INTRODUCTION |  
					| 
 |  
					| Before VB5 the VB programs were not truly traduced in assembler, they were coded in Pseudo Code (hehe you all remember those hating pcode exe!), and the VB virtual machine had the task of itnerpreting the pseudo code and execute it. Those programs were linked to vbrun100, vbrun200, vbrun300, vbrun400 dlls (depending on the version); well thing are a little different because there were variations between 16bit and 32bit modules (pcode were mostly 16bit apps), but this is not what we are looking for. Today we have version 5 and 6 of VB, they use MSVBVM50.dll and MSVBVM60.dll, and now VB exes are really compiled and traduced in asm. As you all know you can't use usual breakpoints like "GetWindowTextA" when debugging VB programs, infact you should use the apis exported from the VBVM dll (e.g. for a serial I would use __vbaStrCmp, or rtcMsgBox), if you want to use these apis in softice you just have to load the VB dll (in winice.dat or via symbol loader). This will help you debug VB applications. _________________
 One thing only I know, and that is that I know nothing. (Socrates)
 |  |  
		| Back to top |  |  
		|  |  |  
		| _aLfa_ Site Admin
 
 
 Joined: 21 Sep 2002
 Posts: 233
 Location: Aveiro, Portugal
 
 | 
				
					| Posted: Fri Feb 15, 2008 11:01 pm
							    Post subject: 2 - ANALISYS |  
					| 
 |  
					| Let's start disassembling the proggy. I am using Ida, I advice you to use it too, but you can use other disassemblers if you want. First of all lets have a general look, so we can have a general idea. You can easily see the following: 
 [code=text:nmwo94j5]
 00401000 ...      IAT (First Thunk ok apis)004010F0 ...      some data004011A5 ...      transfer area (declspec(dllimport) style)0040130E ...      lots of data004023EC ...      local transfer area (for internal event handlers)0040242C ...      other data00402E44 ...      code00403D06 ...      other data
 |  
 Ok we have a general idea of the mapping of the program. Notice that all read only data is in the .text section, the data before and after the code contains names of imported function (IT original first thunks), however now we begin analisys.
 
 We start from entry point. What we see is:
 [asm:nmwo94j5]
 [/asm:nmwo94j5]00401310 push offset RT_MainStruct00401315 call ThunRTMain
 if you debug this, you execute the call the program runs. The analisys here is very simple: the entry point simply consists of:
 
 ThunRTMain(&RT_Main_Struct);
 
 this function is the main function of the VBVM (the Thunder Runtime Engine) and has one parameter, the pointer to a complex structure that describes all the application. It is clear that all the data, all the callbacks and so on are described by this structure, so we can extract alot of information. We just have to check this big structure! Let's go!
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5]00401970  RT_MainStruct db ’VB5!’   ;signature00401974                db 1Ch ;00401975  aVb6it_dll    db ’#VB6IT.DLL’,0  ;italian language00401980                dd 000401984                dd 2Ah00401988                dd 00040198C                dd 000401990                dd 0A0000h00401994                dd 410h00401998                dd 409h0040199C                dd 0004019A0                dd offset ProjectStruct004019A4                dd 30F016h004019A8                dd 0FFFFFF00h004019AC                dd 8004019B0                dd 1004019B4                dd 2004019B8                dd 0E9h004019BC                dd offset DialogsStruct004019C0                dd offset ExternStruct004019C4                dd offset ProjectInfo004019C8                dd 78h004019CC                dd 7Eh004019D0                dd 84h004019D4                dd 85h004019D8                dd 0004019DC                dd 0004019E0                dd 0004019E4                dd 0
 the first field is the signature of the struct itself. Note that the program has been written with VB 6.0, but the signature is "VB5!", probably they didn't change it for cross compatibility? Or just forgot it? Who knows! However we can see various infos, the "VB6IT.dll" that should be the module to load the language (the app has been written with italian version of vb). What should really abtract your attention are four pointers (ProjectStruct, DialogStruct, ExternStruct, ProjectInfo). I gave them a name because I know their function, you should see just four addresses in the dasm. However we will analyse these structs to find infos we need.
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5]0040131C ProjectInfo dd 0 ; DATA XREF: 004019C4o00401320            dd 30h00401324            dd 40h00401328            dd 00040132C            dd 0EACF9A13h00401330            dd 4F93898Bh00401334            dd 0DF0C5493h00401338            dd 159AAEEDh0040133C            dd 000401340            dd 10000h00401344 a020430progetto db 0,0,’020430Progetto1’,000401356 aC000      db ’-C000-’0040135C            dd 0 ;
 here we can see various numbers, in particula there is the project name as last field. If we go and search in the source of the app, we can see in the .vbp the following line
 
 Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#D:\WINNT\System32\STDOLE2.TLB#OLE Automation
 
 so we can think all the data bytes represent the type of project, modules contained etc etc. This isn't very useful, so we go on. We go back to main struct and see another sub structure:
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5]004018F8  ExternStruct dd 6   ;flag004018FC              dd offset InsideImport   ;import data
 it is a bit harder to figure out what this structure is. This handles other imported functions, they can be inside or outside virtual machine module. The flag indicates the type of import (6 = inside, 7 = outside). If you look at InsideImport you will find:
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5]00402B64  InsideImport dd offset  Descr00402B68              dd offset  Thunk
 Descr will point to four dwords that seem to be the same in all vb apps. Thunk will contain a pointer to an area where addresses (of some code of the cirtual machine) are stored. I analysed an other vb app, its ExternStruct is:
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5];snippet from another vb app 00401A5C  ExternStruct dd 7  ;outside 00401A60              dd offset  ImportData 00401A64              dd 6  ;inside 00401A68              dd offset  InsideImport;end snippet from another vb app
 as you can see in that app there was an external function, infact the app used ShellExecute to open the internet browser and link to a site. The ImportData is as follows:
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5];snippet from another vb app 00402DA4  ImportData dd offset  aShell32  ;modulename 00402DA8            dd offset  aShellexecutea  ;apiname 00402DAC            align 8 00402DB0            dd offset  IAT_Data;end snippet from another vb app
 where you can see you have the pointers to the name of the module and the name of the api to import. The IAT_Data fields points to thunking data (hInstance of module and api address). This data is also used from the primitive DLL_Import of VB, used to thunk to ouside apis. Back to our app, we check the DialogStruct pointer:
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5] 00401A00  DialogsStruct[0] dd 50h        ;sizeof struct  00401A04                  dd 356022C6h 00401A08                  dd 400E3F28h 00401A0C                  dd 495240B8h 00401A10                  dd 0C9491BB6h 00401A14                  dd 0 00401A18                  dd 0 00401A1C                  dd 0 00401A20                  dd 0 00401A24                  dd 0 00401A28                  dd 310h 00401A2C                  dd 0 00401A30                  dd 0 00401A34                  dd 0 00401A38                  dd 0 00401A3C                  dd 0 00401A40                  dd 596h 00401A44                  dd 0 00401A48                  dd offset  MainDialog 00401A4C                  dd 4Ch 00401A50  DialogsStruct[1] dd 50h        ;sizeofstruct 00401A54                  dd 78CBBB9Fh 00401A58                  dd 401EB563h 00401A5C                  dd 0DB80B296h 00401A60                  dd 7EFFD31Ah 00401A64                  dd 0 00401A68                  dd 0 00401A6C                  dd 0 00401A70                  dd 0 00401A74                  dd 3 00401A78                  dd 100h 00401A7C                  dd 0 00401A80                  dd 0 00401A84                  dd 0 00401A88                  dd 0 00401A8C                  dd 0 00401A90                  dd 1A3h 00401A94                  dd 0 00401A98                  dd offset  AboutDialog 00401A9C                  dd 9Ch
 we have an array of dialog descriptors. Each descriptor has various data values, in particular has in the 19th field a pointer to a struct were the resource info are stored. These structure have variable size, because it depends on the data contained by the resources. Lets go and see MainDialog; I will not paste all the data but only the important things.
 
 [asm:nmwo94j5]
 [/asm:nmwo94j5]004013C2  aForm1          db 5,0,’Form1’,0  004013CC aLeimcrackme     db 0Bh,0,’Leimcrackme’,0    004013E2 IconData         db 6,3,0,0,6Ch,74h,0,0,0FEh...  004016FA                 dw 2Dh ; clientleft  004016FE                 dw 14Ah ; clienttop  00401702                 dw 1248h ; clientwidth  00401706                 dw 0B7Ch ; clientheight    00401715  aText2          db 5,0,’Text2’,0  00401731  aText1          db 5,0,’Text1’,0  0040174D  aCommand2       db 8,0,’Command2’,0  0040175A  aCheck          db 7,0,’&Check!’,0  00401777  aCommand1       db 8,0,’Command1’,0  00401784  aAbout          db 6,0,’&About’,0  004017A2  aLabel2         db 6,0,’Label2’,0 004017AD  aSerial         db 7,0,’Serial:’,0  004017D1  aLabel2_0       db 6,0,’Label2’,0 004017DC  aName           db 5,0,’Name:’,0  004017FC  aLabel1         db 6,0,’Label1’,0  00401807  aWhoeverTriesTh db ’’,0,’Whoever tries...
 you can see all the components of the crackme, their data, etc etc. IconData is the raw data of the icon of the main dialog.
 
 If we look at the source we have:~
 
 [code=text:nmwo94j5]
 Begin VB.Form Form1  BorderStyle = 3 ’Fixed Dialog  Caption = "Leimcrackme"  ClientHeight = 2940  ClientLeft = 45  ClientTop = 330  ClientWidth = 4680  Icon = "andre.frx":0000  LinkTopic = "Form1"
 |  
 you can see the icon data is encoded in the .frx file, which usually have big data. So the Icon field links to andre.frx file and 0000 is the offset of the starting data. Infact also the last label is linked as follows:
 
 [code=text:nmwo94j5]
 Caption = $"andre.frx":030A
 |