diff --git a/RE101/RE101.md b/RE101/RE101.md new file mode 100644 index 0000000..e83781f --- /dev/null +++ b/RE101/RE101.md @@ -0,0 +1,26 @@ +--- +layout: default +permalink: /RE101/ +title: Reverse Engineering Malware 101 +--- +[Go Back to All WorkShops](https://securedorg.github.io) + +# Reverse Engineering Malware 101 # + +# Material # + +## [Introduction](https://securedorg.github.io/RE101/intro/) ## + +### Section 1) [Fundamentals](https://securedorg.github.io/RE101/section1/) ### + +### Section 2) [Malware Techniques](https://securedorg.github.io/RE101/section2/) ### + +### Section 3) [RE Tools](https://securedorg.github.io/RE101/section3/) ### + +### Section 4) [Triage Analysis](https://securedorg.github.io/RE101/section4/) ### + +### Section 5) [Static Analysis](https://securedorg.github.io/RE101/section5/) ### + +### Section 6) [Dynamic Analysis](https://securedorg.github.io/RE101/section6/) ### + + diff --git a/RE101/ReportForm.html b/RE101/ReportForm.html new file mode 100644 index 0000000..12d65a7 --- /dev/null +++ b/RE101/ReportForm.html @@ -0,0 +1,124 @@ + + +
+ + + + + + + + + + + + + + ++ + + + + + + \ No newline at end of file diff --git a/RE101/dynamic.md b/RE101/dynamic.md new file mode 100644 index 0000000..915b926 --- /dev/null +++ b/RE101/dynamic.md @@ -0,0 +1,144 @@ +--- +layout: default +permalink: /RE101/section6/ +title: Dynamic Analysis +--- +[Go Back to Reverse Engineering Malware 101](https://securedorg.github.io/RE101/) + +# Section 6: Dynamic Analysis # + + + +## LAB 3 +Dynamic analysis is a deeper analysis of the program to understand hidden functionality not understood statically. The static analysis will serve as a guide for stepping through the program in a debugger. + +Open the unpacked malware into the **x32dbg.exe** (referred as x64dbg) debugger and **IDAfree**. + +--- + +### Rebasing the disassembler + +Typically programs start at **004010000** but your debugger might start the program at a different address. You will need to rebase the program's address in the disassembler. In x64dbg, after you hit run or **F9**, it will stop you at the EntryPoint. Scroll up to find the very first address, this is the address that you will need to rebase. + +Edit->Segements->Rebase Program. + + + +--- + +### Finding the starting point + +You will need to sync the debugger and disassembler addresses so you can follow along in both. Let's start with the function offset **xxxx1530**. +* In IDA, open the functions tab and look for function xxxx1530. Where xxxx should match your rebase address ( If rebase is **0190**1000, then **0190**1530 ). +* In x64dbg, CTRL+G to jump to a specific address xxxx1530. + + + + +--- + +### XOR Decode Function + +Remember use the F2(breakpoint), F7(Step Into), F8(Step Over), F9(Run) keys to navigate through the debugger. If you accidentally run past the end the of the program you can always restart by clicking . + + + +In **IDA**, get the offset of the XorDecode function you saved prior. + + + +In **x64bdg** find that same offset and add a comment that it is the Xor Decode function. Set a breakpoint using **F2** on that function. Then run the program until the breakpoint using **F9**. Step into that function using **F7**. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn5.gif) + +Navigate down to the loop that does the Xor Encoding. Place a breakpoint on the same instructions shown below. Right click on the EBX register and select Follow in Dump. This location is where the decoded string will be stored. After you set your break points, press **F9** to get to the start of the loop, then step through the loops until you see the decoded string in the dump. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn9.png) + +--- + +### Navigating to the Internet Request + +We want to manipulate the control flow instructions so that we can get to the network connection API call. We know that the program will first **copy** and then **delete** itself after it checks if the file doesn't exists using GetFileAttributes API. Continue to step to the **jne** (jump if not equal) instruction. By double clicking the **ZF flag** we can manipulate the result 1 to 0. This means it will make the jump past the Copfile API. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn10.gif) + +Once you get past the delete API, there is that weird string you saw during static analysis. Step over (**F8**) the XorDecode function and notice the EAX register. It is the URL that was in the internet traffic from the triage analysis. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn11.png) + +--- + +### Manipulate the HTTP request outcome + +The VM was not connected to the internet but instead InetSim. What will happen when you manipulate the control flow to get past the internet connection failure? Go ahead and step past the internet connection and manipulate the control flow flag ZF to do so. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn12.gif) + +It must have been a very funny joke. **l** **m** **a** **o** + +--- + +### There is a message for you + +It seems that the malware was waiting for the word **lmao** to display a message. Navigate to the Messagebox api. Set a breakpoint on and after the function call, this will ensure that it will prevent you from skipping any hidden functionality. Go ahead and press **F9** to run the MessageBox function. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn13.gif) + +--- + +### Extracting the Resource + +The CFF explorer from the triage analysis revealed that there was a resource called **BIN**. Step through the program to get the location of the loaded resource after **LockResource**. Remember function return the output in register **EAX**. Notice `mov edi,eax` is where the output is stored in **EDI**. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn14.png) + +--- + +### Crypto Function + +We can assume that the malware is going to decrypt this string based on the function arguments for [CryptStringToBinary](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380285.aspx). + +```C++ +BOOL WINAPI CryptStringToBinary( + _In_ LPCTSTR pszString, //Arg 1 + _In_ DWORD cchString, + _In_ DWORD dwFlags, // Arg 3 Format of the string converted + _In_ BYTE *pbBinary, + _Inout_ DWORD *pcbBinary, + _Out_ DWORD *pdwSkip, + _Out_ DWORD *pdwFlags +); +``` + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn15.png) + +We know that Arg 1 is register **EDI** which is the resource we just loaded into memory and Arg 3 is 1. The CryptStringToBinary dwflag `0x00000001` means `CRYPT_STRING_BASE64`. Dump the address of EDI into one of the dump windows. This data definitely looks like base64 encoded strings. Step over these functions until past the second CryptStringToBinary call. The result will be placed in register **ESI**. Dump the address in the ESI register. Notice anything weird about the first 3 characters? + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn16.png) + +--- + +### CreateFile and ShellExecute + +Step over the create and write file functions to save the decrypted resource to the file system. Note that this file is saved as **icon.gif**. Next step until the start of the arguments for the ShellExecute call. It looks as if it's using the environment to open the newly created file. The program will finally be done. Open the image and record what you see. + +*Click to Enlarge* +[](https://securedorg.github.io/images/dyn17.gif) + +--- + +### Finale + +Go to the URL in the icon.gif. + +[Section 5 <- Back](https://securedorg.github.io/RE101/section5) | [Next -> Finale](https://securedorg.github.io/RE101/section6.1) \ No newline at end of file diff --git a/RE101/dynamic2.md b/RE101/dynamic2.md new file mode 100644 index 0000000..4cf93a5 --- /dev/null +++ b/RE101/dynamic2.md @@ -0,0 +1,96 @@ +--- +layout: default +permalink: /RE101/section6.1/ +title: Dynamic Analysis +--- +[Go Back to Reverse Engineering Malware 101](https://securedorg.github.io/RE101/) + +# Section 6: Finale # + +Congrats, you made it through the workshop. All of your notes and debugging should have gotten you to come up with a similar control flow like the diagram and report below. + +*Click to Enlarge* +[](https://securedorg.github.io/images/Diagram.png) + + +## Simple Report + +Filename: Unkown.exe + +Sha256: a635f37c16fc05e554a6c7b3f696e47e8eaf3531407cac27e357851cb710e615 + +### Summary + +This file creates a copy of itself in the %APPDATA% location, sets persistence mechanisms, and beacons to definitely-not-evil.com. If beacon is successful, it will open a messagebox, then decrypt the resource which will then spawn a shell window to open the resource. + +### General Characteristics + +The file is UPX packed + +Import Functions: +* GetEnvironmentVariable +* CopyFile +* DeleteFile +* InternetOpen +* InternetConnect +* HttpOpenRequest +* HttpSendRequest +* MessageBox +* FindResource +* CryptStringToBinary +* CreateFile +* ShellExecute +* CreateProcess + + +### File System IOC + +CreateFile C:\Users\victim\AppData\Roaming\dope.exe CreateFile icon.gif + +### Network IOC + +GET /ayy HTTP/1.1 + +Content-Type: text/html + +MySpecialHeader: whatever + +User-Agent: definitely-not-evil.com + +Host: definitely-not-evil.com + +Cache-Control: no-cache + +### Registry IOC + +RegQueryValue HKCU\Software\Microsoft\Windows\CurrentVersion\Run\dope + +### Behavior & Control Flow + +Processes Created dope.exe + +1) Starts by decoding xor strings + +2) Checks to see if dope.exe already exists in %APPDATA% + +3) If it doesn't exist create a copy of itself to %APPDATA% as dope.exe + +4) Set the startup registry key + +5) Start the newly copied dope.exe process + +6) Delete the original + +7) Dope.exe will check the registry key if set + +8) Call out to definitely-not-evil.com + +9) If the result is "lmao" it will open a messagebox and extract the resource + +10) Base64 decode the resource + +11) Save decoded resource as icon.gif + +12) Shellexecute to open icon.gif + +[Section 6 <- Back](https://securedorg.github.io/RE101/section6) \ No newline at end of file diff --git a/RE101/fundamentals.md b/RE101/fundamentals.md new file mode 100644 index 0000000..3e6f92f --- /dev/null +++ b/RE101/fundamentals.md @@ -0,0 +1,83 @@ +--- +layout: default +permalink: /RE101/section1/ +title: Fundamentals +--- +[Go Back to Reverse Engineering Malware 101](https://securedorg.github.io/RE101/) + +# Section 1: Fundamentals # + +## Environment Setup ## + +In this section you will be setting up a safe virtual malware analysis environment. The virtual machine (VM) that you will be running the malware on should not have internet access nor network share access to the host system. This VM will be designated as the **Victim VM**. On the other hand, the **Sniffer VM** will have a passive role in serving and monitoring the internet traffic of the Victim VM. This connection remains on a closed network within virtualbox. + +### Installing VirtualBox ### + +For windows and osx, follow the instructions in the install binary. + +| Windows | Mac OSX | Linux | +| --- | --- | --- | +| [](http://download.virtualbox.org/virtualbox/5.1.14/VirtualBox-5.1.14-112924-Win.exe) | [](http://download.virtualbox.org/virtualbox/5.1.14/VirtualBox-5.1.14-112924-OSX.dmg) | [](https://www.virtualbox.org/wiki/Linux_Downloads) | + +### Download Victim and Sniffer VMs ### + +Please use the utility [7zip](http://www.7-zip.org/download.html). Unzip the files with 7zip below and in VirtualBox **File->Import** Appliance targeting the .ova file. + + +[Victim VM](https://drive.google.com/open?id=0B_0DJl2kuzoNZkpveEtiMWJKWDA) + +* MD5sum: b84f0cdb7acc00aeb9effcee84b85f65 **Updated 9/11/2017** +* OS: Windows 7 Service Pack 1 +* Architecture: Intel 32bit +* Username: victim +* Password: re1012017 +* IP Address: 192.168.0.2 +* Gateway: 192.168.0.1 +* Zip size 3.96G, Final size required 10.1G + +**Note:** If the VM is rebooting on you, open a command prompt with admin privileges and run "slmgr /rearm", then reboot. It should reset the VM's trial version. + +[Sniffer VM](https://drive.google.com/open?id=0B_0DJl2kuzoNT3IwNElLV3VRdms) + +* MD5sum: fc69591b0ce1cdc84fc5c63d02d30d5f **Updated 9/11/2017** +* OS: Ubuntu 16.04.2 LTS Desktop +* Architecture: Intel 64bit +* Username: sniffer +* password re1012017 +* IP Address: 192.168.0.1 +* Gateway: 192.168.0.1 +* Zip size 2.08G, Final size required 6G + +--- + +### Post Install Instructions ### + +**Note:** If you are having problems getting the VM to run, revert to the AnalysisReady snapshot, then right-click on the VM and select discard the saved state. + +1. Install VirtualBox CD on both VMs: Devices->Insert Guest Additions CD Image + * If it doesn't auto appear, navigate to the CD Drive to install + * Follow install directions from the Guest Additions Dialog + * Note: it will require install privileges so insert passwords for each VM + * Shutdown Both VMs after you have installed the Guest Additions CD. +2. Victim VM: Devices->Drag and Drop->Bidrectional +3. Victim VM: Devices->Shared Clipboard->Bidirectional +4. Both VMs: Devices->Network->Network Settings + * Select Attached to `Internal Network` + * Name should mirror both VMs. Default is `intnet` +5. Run/Play both VMs to verify network connectivity + * **Important** While running, take a snapshot of each VM and name each "Clean". This will save a clean slate for you to revert the VM image back to. +6. Sniffer VM: Ensure `inetsim` is running + * Open terminal and run: `ps -ef | grep inetsim` + * If no output, run: `/etc/init.d/inetsim start` + * Run the ps command again to confirm it's running. + * Expected output:  +7. Victim VM: test connection to Sniffer VM + * In the search bar, type `cmd.exe` to open terminal + * Run command: `ping 192.168.0.1` + * Expected output:  +8. Sniffer VM: Devices->Shared Folders->Shared Folders Settings + * On your Host, create a folder called `sniffershare` + * In virtual box select Add New Shared Folder icon and navigate to the folder you just created (sniffershare) + * In Sniffer VM, open the terminal and run command:`mkdir ~/host; sudo mount -t vboxsf -o uid=$UID,gid=$(id -g) sniffershare ~/host` + +[Intro <- Back](https://securedorg.github.io/RE101/intro) | [Next -> Anatomy of PE](https://securedorg.github.io/RE101/section1.2) diff --git a/RE101/fundamentals2.md b/RE101/fundamentals2.md new file mode 100644 index 0000000..e5119a1 --- /dev/null +++ b/RE101/fundamentals2.md @@ -0,0 +1,82 @@ +--- +layout: default +permalink: /RE101/section1.2/ +title: Fundamentals +--- +[Go Back to Reverse Engineering Malware 101](https://securedorg.github.io/RE101/) + +# Section 1.2: Fundamentals # + +## Anatomy of a Windows PE C program ## + +Typical windows programs are in the Portable Executable (PE) Format. It’s portable because it contains information, resources, and references to dynamic-linked libraries (DLL) that allows windows to load and execute the machine code. + + + +--- + +## Windows Architecture ## + +In this workshop we will be focusing on user-mode applications. + +### User-mode vs. Kernel Mode [[1]][1] ### + +- In user-mode, an application starts a user-mode process which comes with its own private virtual address space and handle table + +- In kernel mode, applications share virtual address space. + +[1]: https://msdn.microsoft.com/en-us/windows/hardware/drivers/gettingstarted/user-mode-and-kernel-mode?f=255&MSPPError=-2147217396 + +This diagram shows the relationship of application components for user-mode and kernel-mode. + + +--- + +## PE Header ## + +The PE header provides information to operating system on how to map the file into memory. +The executable code has designated regions that require a different memory protection (RWX) +- Read +- Write +- Execute + +This diagram shows how this header is broken up. + +*Click to Enlarge* +[](https://securedorg.github.io/images/PE32.png) + +Here is a hexcode dump of a PE header we will be working with. + +*Click to Enlarge* +[](https://securedorg.github.io/images/PEHeader.gif) + +--- + +## Memory Layout ## + +- **Stack** - region of memory is added or removed using “last-in-first-out” (LIFO) procedure [[2]][2] +- **Heap** - region for dynamic memory allocation [[3]][3] +- **Program Image** - The PE executable code placed into memory +- **DLLs** - Loaded DLL images that are referenced by the PE +- **TEB** - Thread Environment Block stores information about the current running thread(s) [[4]][4] +- **PEB** - Process Environment Block stores information about loaded modules and processes. [[5]][5] + +[2]: https://en.wikipedia.org/wiki/Stack_(abstract_data_type) +[3]: https://en.wikipedia.org/wiki/Heap_(data_structure) +[4]: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block +[5]: https://en.wikipedia.org/wiki/Process_Environment_Block + +This diagram illustrates how the PE is placed into memory. + + +--- + +## The Stack ## + +- Data is either pushed onto or popped off of the stack data structure +- **EBP** - Base Pointer is the register that used to store the references in the stack frame + +This diagram represents a typical stack frame. + + +[Environment Setup <- Back](https://securedorg.github.io/RE101/section1) | [Next -> x86 Assembly](https://securedorg.github.io/RE101/section1.3) diff --git a/RE101/fundamentals3.md b/RE101/fundamentals3.md new file mode 100644 index 0000000..ddd12ca --- /dev/null +++ b/RE101/fundamentals3.md @@ -0,0 +1,133 @@ +--- +layout: default +permalink: /RE101/section1.3/ +title: Fundamentals +--- +[Go Back to Reverse Engineering Malware 101](https://securedorg.github.io/RE101/) + +# Section 1.3: Fundamentals # + +## x86 Assembly Language ## + +The C programming is a high level language interpreted by the compiler that converts code into machine instructions called assembly language. By using a disassembler tool we can get the assembly language of a compiled C program. + +The Intel 8086 and 8088 were the first CPUs to have an instruction set that is now commonly referred to as **x86**. Intel Architecture 32-bit (IA-32) sometimes also called i386 is the 32-bit version of the x86 instruction set architecture. + +The x86 architecture is **little-endian**, meaning that multi-byte values are written least significant byte first. + +#### How we see it: +| A0 | A1 | A2 | A3 | + +#### Stored as Little Endian +| A3 | A2 | A1 | A0 | + +--- + +## Opcodes and Instructions ### + +Each Instruction represents opcodes (hex code) that tell the machine what to do next. + +Three categories of instructions: +* Data Movement/Access +* Arithmetic / Logic +* Control-Flow + +Common Instructions +* **mov, lea** (data movement, data access) +* **add, sub** (arithmetic) +* **or, and, xor** (Logic) +* **shr, shl** (Logic) +* **ror, rol** (Logic) +* **jmp, jne, jnz, jnb** (Control Flow) +* **push, pop, call, leave, enter, ret** (Control Flow) + +Example below is moving value at 0xaaaaaaaa into ecx. + +| Instruction | Opcode | +| --- | --- | +| `mov ecx,[0xaaaaaaaa];` | `8B 0D AA AA AA AA` | + +Use the search page below or open the [Search Instructions](https://securedorg.github.io/x86.html) page to search for functions discussed above + + + +--- + +## Registers ### + +The image below is what registers will look like in a debugger. + + +#### General-Purpose Registers [[1]][1] + + +| Register | Description | +| --- | --- | +| **EAX** | Accumulator Register | +| **EBX** | Base Register | +| **ECX** | Counter Register | +| **EDX** | Data Register | +| **ESI** | Source Index | +| **EDI** | Destination Index | +| **EBP** | Base Pointer | +| **ESP** | Stack Pointer | + +#### Segment Registers + +| Register | Description | +| --- | --- | +| SS | Stack Segment, Pointer to the stack | +| CS | Code Segment, Pointer to the code | +| DS | Data Segment, Pointer to the data | +| ES | Extra Segment, Pointer to extra data | +| FS | F Segment, Pointer to more extra data | +| GS | G Segment, Pointer to still more extra data | + +#### EFLAGS Register + +| ID | Name | Description | +| --- | --- | --- | +| CF | Carry Flag | Set if the last arithmetic operation carried (addition) or borrowed (subtraction) a bit beyond the size of the register. This is then checked when the operation is followed with an add-with-carry or subtract-with-borrow to deal with values too large for just one register to contain | +| PF | Parity Flag | Set if the number of set bits in the least significant byte is a multiple of 2 | +| AF | Adjust Flag | Carry of Binary Code Decimal (BCD) numbers arithmetic operations | +| ZF | Zero Flag | Set if the result of an operation is Zero (0) | +| SF | Sign Flag | Set if the result of an operation is negative | +| TF | Trap Flag | Set if step by step debugging | +| IF | Interruption Flag | Set if interrupts are enabled | +| DF | Direction Flag | Stream direction. If set, string operations will decrement their pointer rather than incrementing it, reading memory backwards | +| OF | Overflow Flag | Set if signed arithmetic operations result in a value too large for the register to contain | +| IOPL | I/O Privilege Level field (2 bits) | I/O Privilege Level of the current process | +| NT | Nested Task flag | Controls chaining of interrupts. Set if the current process is linked to the next process | +| RF | Resume Flag | Response to debug exceptions | +| VM | Virtual-8086 Mode | Set if in 8086 compatibility mode | +| AC | Alignment Check | Set if alignment checking of memory references is done | +| VIF | Virtual Interrupt Flag | Virtual image of IF | +| VIP | Virtual Interrupt Pending flag | Set if an interrupt is pending | +| ID | Identification Flag | Support for CPUID instruction if can be set | + +#### Instruction Pointer + +The **EIP** register contains the address of the next instruction to be executed. + +--- + +## Hello World ## + +*Click Image to Enlarge* +[](https://securedorg.github.io/images/helloworld.gif) + +## Calling a Function ## + +### Arguments on the Stack ### + +*Click Image to Enlarge* +[](https://securedorg.github.io/images/FunctionCall.gif) + +### Local Variables on the Stack ### + +*Click Image to Enlarge* +[](https://securedorg.github.io/images/FunctionCall2.gif) + +[1]: https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture + +[Anatomy of PE <- Back](https://securedorg.github.io/RE101/section1.2) | [Next -> Section 2](https://securedorg.github.io/RE101/section2) diff --git a/RE101/idacheatsheet.html b/RE101/idacheatsheet.html new file mode 100644 index 0000000..eea3900 --- /dev/null +++ b/RE101/idacheatsheet.html @@ -0,0 +1,190 @@ + + + + + + + + + + +
+IDAProCheat Sheet+
|
+
+
|
+
+
|
+
+