-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPythonController.cs
More file actions
129 lines (120 loc) · 5.23 KB
/
Copy pathPythonController.cs
File metadata and controls
129 lines (120 loc) · 5.23 KB
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
using SerializedPayloadGenerator.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SerializedPayloadGenerator.Controllers
{
public class PythonController : Controller
{
private static string strAppDataPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource");
private static string strPythonPath = String.Format(@"{0}\python\python.exe", strAppDataPath);
private static string strPyPickleFile = String.Format(@"{0}\python_support\pickle_deser.py", strAppDataPath);
private static string strPyPickleDictFile = String.Format(@"{0}\python_support\pickle_deser_dict.py", strAppDataPath);
// GET: Python
public ActionResult Index(Python python)
{
if (TempData.ContainsKey("pickle"))
{
python = (Python)TempData["pickle"];
}
return View(python);
}
public bool ValidateParams(Python python)
{
if (MvcApplication.ValidateInput(python.command) &&
MvcApplication.ValidateInput(python.payload) &&
MvcApplication.ValidateInput(python.output.ToString()) &&
MvcApplication.ValidateInput(python.type.ToString()))
{
return true;
}
return false;
}
private ActionResult SendYAMLFile(Python python)
{
string strFileName = "exploit.yaml";
string srtFilePath = String.Format(@"{0}\python\{1}", strAppDataPath, strFileName);
string srtTempFilePath = String.Format(@"{0}\TempFiles\{1}_{2}", strAppDataPath, strFileName, (new Random()).Next(1, 1000));
string text = System.IO.File.ReadAllText(srtFilePath);
text = text.Replace("$COMMAND$", python.command);
System.IO.File.WriteAllText(srtTempFilePath, text);
byte[] byteFileData = System.IO.File.ReadAllBytes(srtTempFilePath);
string strContentType = MimeMapping.GetMimeMapping(srtFilePath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = strFileName,
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
System.IO.File.Delete(srtTempFilePath);
return File(byteFileData, strContentType);
}
private ActionResult SendDictinoryFile(Python python, string srtFilePath)
{
byte[] byteFileData = System.IO.File.ReadAllBytes(srtFilePath);
string strContentType = MimeMapping.GetMimeMapping(srtFilePath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "PickleDict.bin",
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
System.IO.File.Delete(srtFilePath);
return File(byteFileData, strContentType);
}
[HttpPost]
public ActionResult Generate(Python python)
{
if (python.type == PyDeserType.Pickle)
{
if (ValidateParams(python))
{
if (python.output == PythonOutput.Base64)
{
string strPickleCommand = string.Format("{0} \"{1}\"", strPyPickleFile, python.command);
string strCommandOutput = MvcApplication.executeCommand(strPythonPath, strPickleCommand);
try
{
python.payload = strCommandOutput.Substring(2, strCommandOutput.Length - 5);
}
catch (Exception e)
{
python.payload = e.Message;
}
TempData["pickle"] = python;
return RedirectToAction("Index");
}
else
{
string strTempFilePath = String.Format(@"{0}\TempFiles\PythonDict_{1}.bin", strAppDataPath, (new Random()).Next(1, 1000));
string strPickleCommand = string.Format("{0} \"{1}\" \"{2}\"", strPyPickleDictFile, python.command, strTempFilePath);
MvcApplication.executeCommand(strPythonPath, strPickleCommand);
if (System.IO.File.Exists(strTempFilePath))
{
return SendDictinoryFile(python, strTempFilePath);
}
else
{
python.payload = "Failed to Generate Dictionary payload";
TempData["pickle"] = python;
return RedirectToAction("Index");
}
}
}
else
{
python.payload = "Invalid options: '&' and '|' not allowed";
TempData["pickle"] = python;
return RedirectToAction("Index");
}
}
else
{
return SendYAMLFile(python);
}
}
}
}