Introduction:
There are many different tools, settings, configurations, etc. when it comes to sandboxing so I figured I would write about some that I've used and had success with. This tutorial will cover Sysmon event / network connection logging and PowerShell logging. In future blog posts I will write about many other sandboxing tools, techniques, and use cases.
What you will need :
- Virtualization software: VMware, VirtualBox, etc.
- Windows 10 ISO or virtual hard disk with Windows 10 installed
Notes:
- PowerShell logs and Sysmon logs are great tools to have in a sandbox environment. However, having them enabled on as many machines across your network as possible is where the real value is it. Having these logs (in addition standard Windows logs) centrally stored and readily accessible can drastically improve your ability to detect and respond.
- Please use extreme caution when working with live malware. I am not responsible for anything as a result of this tutorial
Tutorial
Installing Sysmon
1.)
Install your virtualization software (VMware,
VirtualBox, etc.)
2.)
Boot up Windows 10
3.)
Install Sysmon:
- Browse to https://technet.microsoft.com/en-us/sysinternals/sysmon and download the latest version
- Unzip the zip file it came in
- Inside of the zip file you will see three files:
- A EULA
- Sysmon.exe
- Sysmon64.exe
- In an administrative command prompt navigate to the unzipped Sysmon directory
- In the command line Type “Sysmon64.exe –i –n –l”
- -i = Install the Sysmon service and drivers
- -n = log network connections
- -l = Log loading of modules (DLLs loaded by processes, etc.)
- Note: By default Sysmon will automatically log the SHA1 hashes of created processes and libraries. You can change the hashing algorithm by throwing the -h flag
- Ex: “Sysmon64.exe –i –n –l -h md5”
- To check and see if Sysmon was installed correctly, type the following command:
- Sysmon.exe -c
- You should now see the following output:
- Locating the Sysmon output
- The Sysmon output is stored in Windows event logs under the following file path
- C:\Windows\System32\winevt\Logs\ Microsoft-Windows-Sysmon%4Operational.evtx
- I recommend increasing the max log size of this particular log. To do this, open event viewer and go to àApplications and Services Log à Microsoft à Windows à Sysmon à Operational. Right click on this log and set the “Max Log Size” field to your liking. One GB or more should be enough in most cases.
Introduction To PowerShell Logging
Investigating PowerShell based attacks is an essential skill to have in your forensics investigative skillset. Without some sort of logging, process monitoring, etc. it can be rather difficult to figure out what happened on a host based level when it comes to PowerShell based attacks. The next section of this tutorial will show you how to implement PowerShell logging in your sandbox. To begin, it is important to understand the capabilities of PowerShell logging and what each log type means. I highly recommend reading this article written by Matthew Dunwoody of FireEye that describes the capabilities of PowerShell logging in great detail.
Enabling PowerShell Logging
To turn on PowerShell logging perform the following:
- Press the Windows Key + R (to open run)
- type gpedit.msc
- This will open the local group policy editor
- Browse to Administrative templates ---> Windows Components ---> Windows PowerShell
- Double click on the logging option that you want to turn on and click "enable"
- When enabling transcript logging make sure you specify an output directory.
- The other PowerShell logs are written to an event log that can be found at C:\Windows\System32\winevt\Logs\Microsoft-Windows-PowerShell%4Operational.evtx
- I also recommend increasing the maximum size of these logs (just like we did for the Sysmon logs)
Examples
PowerShell Logging Example
For this example, I have created a macro enabled Word document that when opened, executes a fake piece of malware via PowerShell located at the root of the C drive. All this fake malware does is query the name of the machine, gets the current time, and will ping grubesecurity.blogspot.com. Shown below is a screenshot of my macro enabled word document and its corresponding macro code. Often this code will be obfuscated, but for the purpose of this tutorial it is not (part of it is only base64 encoded.)
Sub AutoOpen()
Dim exec As String
exec = "powershell.exe -nologo -executionpolicy bypass -noninteractive -windowstyle hidden -EncodedCommand QwA6AFwAZQB2AGkAbABfAG0AYQBsAHcAYQByAGUALgBwAHMAMQA="
Shell (exec)
End Sub
To recap, in the last example we looked at the output of the fake malicious code via PowerShell transcription logging. Now we will look at PowerShell script block logging. This means that we will now see the deobfuscated code of the evil_malware.ps1 file. To view the log file, open Event Viewer and go to Application and Service Logs --> Microsoft --> Widows --> PowerShell --> Operational. Shown below is the data for event ID 4104 which contains the source code of the evil_malware.ps1 file.
The last item we will look at is the output of Sysmon. To view the log file, open Event Viewer and go to Application and Service Logs --> Microsoft --> Widows --> Sysmon --> Operational. In the screenshot below you can see the malicious PowerShell command was run by a Microsoft Word document named "Grube Tax Document.docm."
Sub AutoOpen()
Dim exec As String
exec = "powershell.exe -nologo -executionpolicy bypass -noninteractive -windowstyle hidden -EncodedCommand QwA6AFwAZQB2AGkAbABfAG0AYQBsAHcAYQByAGUALgBwAHMAMQA="
Shell (exec)
End Sub
The first log we will look at is the output of the transcription log. Now remember that the results we be stored inside of the directory you specified via group policy. For this example I decided to place my output at "C:\PowerShell Transcription Logging Output." Notice in the screenshot below that the output folders are named based on the date.
When viewing the output you can see the command that the macro ran. When you base64 decode the EncodedCommand parameter it reads "C:\evil_malware.ps1" which tells PowerShell to run the fake malicious script. Notice that you can only see the output of the evil_malware.ps1 script. I am highlighting this because it's very important to know what you can expect to see when looking at any sort of log. In this case, you will see exactly what the attacker saw, which is the output of the fake malicious code. If this had been Mimikatz you would have seen which credentials, hashes, etc. that the attacker was able to obtain on the system. In the next example, we will look at the output from the PowerShell script block logging which will show us the source code of the evil_malware.ps1 file.
To recap, in the last example we looked at the output of the fake malicious code via PowerShell transcription logging. Now we will look at PowerShell script block logging. This means that we will now see the deobfuscated code of the evil_malware.ps1 file. To view the log file, open Event Viewer and go to Application and Service Logs --> Microsoft --> Widows --> PowerShell --> Operational. Shown below is the data for event ID 4104 which contains the source code of the evil_malware.ps1 file.
Sysmon Example
The last item we will look at is the output of Sysmon. To view the log file, open Event Viewer and go to Application and Service Logs --> Microsoft --> Widows --> Sysmon --> Operational. In the screenshot below you can see the malicious PowerShell command was run by a Microsoft Word document named "Grube Tax Document.docm."