Module: Rex::Powershell::Payload

Includes:
Templates
Defined in:
lib/rex/powershell/payload.rb

Constant Summary

Constants included from Templates

Templates::DEFAULT_RIG_OPTS, Templates::TEMPLATE_DIR, Templates::TO_MEM_DOTNET, Templates::TO_MEM_MSIL, Templates::TO_MEM_OLD, Templates::TO_MEM_REFLECTION

Class Method Summary collapse

Class Method Details

.read_replace_script_template(template_path, filename, hash_sub) ⇒ Object



11
12
13
14
15
16
# File 'lib/rex/powershell/payload.rb', line 11

def self.read_replace_script_template(template_path, filename, hash_sub)
  template = ''
  template_pathname = File.join(template_path, filename)
  File.open(template_pathname, "rb") {|f| template = f.read}
  template % hash_sub
end

.to_win32pe_psh(template_path = TEMPLATE_DIR, code) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rex/powershell/payload.rb', line 37

def self.to_win32pe_psh(template_path = TEMPLATE_DIR, code)
  hash_sub = {}
  hash_sub[:var_code] 		= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_win32_func]		= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_payload] 		= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_size] 		= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_rwx]			= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_iter] 		= Rex::Text.rand_text_alpha(rand(8)+8)
  hash_sub[:var_syscode] 		= Rex::Text.rand_text_alpha(rand(8)+8)

  hash_sub[:shellcode] = Rex::Powershell.to_powershell(code, hash_sub[:var_code])

  read_replace_script_template(template_path, "to_mem_old.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
end

.to_win32pe_psh_msil(template_path = TEMPLATE_DIR, code) ⇒ Object

MSIL JIT approach as demonstrated by Matt Graeber www.exploit-monday.com/2013/04/MSILbasedShellcodeExec.html Referencing PowerShell Empire data/module_source/code_execution/Invoke-ShellcodeMSIL.ps1



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
# File 'lib/rex/powershell/payload.rb', line 83

def self.to_win32pe_psh_msil(template_path = TEMPLATE_DIR, code)
  rig = Rex::RandomIdentifier::Generator.new(DEFAULT_RIG_OPTS)
  rig.init_var(:func_build_dyn_type)
  rig.init_var(:func_get_meth_addr)
  rig.init_var(:var_type_name)
  rig.init_var(:var_dyn_asm)
  rig.init_var(:var_dyn_mod)
  rig.init_var(:var_tgt_meth)
  rig.init_var(:var_dyn_type)
  rig.init_var(:var_dyn_meth)
  rig.init_var(:var_args)
  rig.init_var(:var_xor)
  rig.init_var(:var_sc_addr)
  rig.init_var(:var_sc)
  rig.init_var(:var_src_meth)
  rig.init_var(:str_addr_loc)
  rig.init_var(:str_tgt_meth)
  rig.init_var(:str_src_type)
  rig.init_var(:str_tgt_type)

  hash_sub = rig.to_h
  hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)

  read_replace_script_template(template_path, "to_mem_msil.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
end

.to_win32pe_psh_net(template_path = TEMPLATE_DIR, code) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rex/powershell/payload.rb', line 18

def self.to_win32pe_psh_net(template_path = TEMPLATE_DIR, code)
  rig = Rex::RandomIdentifier::Generator.new(DEFAULT_RIG_OPTS)
  rig.init_var(:var_code)
  rig.init_var(:var_kernel32)
  rig.init_var(:var_baseaddr)
  rig.init_var(:var_threadHandle)
  rig.init_var(:var_output)
  rig.init_var(:var_codeProvider)
  rig.init_var(:var_compileParams)
  rig.init_var(:var_syscode)
  rig.init_var(:var_temp)
  rig.init_var(:var_opf)

  hash_sub = rig.to_h
  hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)

  read_replace_script_template(template_path, "to_mem_dotnet.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
end

.to_win32pe_psh_rc4(template_path = TEMPLATE_DIR, code) ⇒ Object

PSH script that executes an RC4 encrypted payload with Invoke-Expression by Adrian Vollmer (SySS GmbH, www.syss.de)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rex/powershell/payload.rb', line 113

def self.to_win32pe_psh_rc4(template_path = TEMPLATE_DIR, code)
  rig = Rex::RandomIdentifier::Generator.new(DEFAULT_RIG_OPTS)
  rig.init_var(:func_rc4_decrypt)
  rig.init_var(:var_rc4buffer)
  rig.init_var(:var_key)

  key = Rex::Text.rand_text_alpha(rand(8)+8)
  rc4 = RC4.new(key)
  enc_code = rc4.encrypt(code)

  hash_sub = rig.to_h
  hash_sub[:random_key] = key
  hash_sub[:b64payload] = Rex::Text.encode_base64(enc_code)

  read_replace_script_template(template_path, "to_mem_rc4.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
end

.to_win32pe_psh_reflection(template_path = TEMPLATE_DIR, code) ⇒ Object

Reflection technique prevents the temporary .cs file being created for the .NET compiler Tweaked by shellster Originally from PowerSploit



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rex/powershell/payload.rb', line 57

def self.to_win32pe_psh_reflection(template_path = TEMPLATE_DIR, code)
  rig = Rex::RandomIdentifier::Generator.new(DEFAULT_RIG_OPTS)
  rig.init_var(:func_get_proc_address)
  rig.init_var(:func_get_delegate_type)
  rig.init_var(:var_code)
  rig.init_var(:var_module)
  rig.init_var(:var_procedure)
  rig.init_var(:var_unsafe_native_methods)
  rig.init_var(:var_parameters)
  rig.init_var(:var_return_type)
  rig.init_var(:var_type_builder)
  rig.init_var(:var_buffer)
  rig.init_var(:var_hthread)
  rig.init_var(:var_opf)

  hash_sub = rig.to_h
  hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)

  read_replace_script_template(template_path, "to_mem_pshreflection.ps1.template",hash_sub).gsub(/(?<!\r)\n/, "\r\n")
end