Class: TurboRex::Windows::ALPC::Transport
- Inherits:
-
Object
- Object
- TurboRex::Windows::ALPC::Transport
- Defined in:
- lib/turborex/windows/alpc.rb
Instance Method Summary collapse
- #accept(opts = {}) ⇒ Object
- #close ⇒ Object
- #connect(opts = {}, &block) ⇒ Object
-
#initialize(opts = {}) ⇒ Transport
constructor
A new instance of Transport.
- #listen(conn_handle, opts = {}, &block) ⇒ Object
- #recv(handle, opts = {}) ⇒ Object
- #refuse_connect(opts = {}) ⇒ Object
- #send(handle, port_message, message_attr, opts = {}) ⇒ Object
- #send_recv(handle, send_message, send_message_attr, recv_message_attr, opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Transport
Returns a new instance of Transport.
119 120 121 122 |
# File 'lib/turborex/windows/alpc.rb', line 119 def initialize(opts = {}) @conn_handle = nil @communication_handle = [] end |
Instance Method Details
#accept(opts = {}) ⇒ Object
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 |
# File 'lib/turborex/windows/alpc.rb', line 324 def accept(opts = {}) communication_handle = APIProxy.alloc_c_type('HANDLE') alpc_port_attr = APIProxy.alloc_c_struct('ALPC_PORT_ATTRIBUTES') alpc_port_attr.Flags = 0 alpc_port_attr.MaxMessageLength = 0x1000 alpc_port_attr.MemoryBandwidth = 0 alpc_port_attr.MaxPoolUsage = 0xFFFFFFFF alpc_port_attr.MaxSectionSize = 0xFFFFFFFF alpc_port_attr.MaxViewSize = 0xFFFFFFFF alpc_port_attr.MaxTotalSectionSize = 0xFFFFFFFF alpc_port_attr.DupObjectTypes = 0xFFFFFFFF port_context = opts[:port_context] || 0 flags = opts[:flags] || 0 uniq_process = opts[:uniq_process] uniq_thread = opts[:uniq_thread] = opts[:message_id] accept = 1 = opts[:port_message] if .nil? raise TurboRex::Exception::ALPC::ReplyMessageMismatch if uniq_process.nil? || uniq_thread.nil? || .nil? = PortMessage.new(alloc_size: 1) .client_id = [uniq_process, uniq_thread] . = end accept = 0 if opts[:refuse] ntstatus = APIProxy.ntalpcacceptconnectport(communication_handle, @conn_handle, flags, 0, alpc_port_attr, port_context, ., 0, accept) unless opts[:refuse] @communication_handle << communication_handle[0] [communication_handle[0], TinySDK.format_hex_ntstatus(ntstatus)] else TinySDK.format_hex_ntstatus(ntstatus) end end |
#close ⇒ Object
376 377 378 379 380 381 |
# File 'lib/turborex/windows/alpc.rb', line 376 def close APIProxy.ntalpcdisconnectport(@conn_handle, 0) Metasm::WinAPI.closehandle @conn_handle @conn_handle = nil @communication_handle = [] end |
#connect(opts = {}, &block) ⇒ Object
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 |
# File 'lib/turborex/windows/alpc.rb', line 257 def connect(opts = {}, &block) unless wport_name = TurboRex::Windows::Utils.multibyte_to_widechar(opts[:port_name]) raise "Unable to convert characters to utf-16le encoding." end dest_str = APIProxy.alloc_c_ptr('UNICODE_STRING') APIProxy.rtlinitunicodestring(dest_str, wport_name) handle = APIProxy.alloc_c_type('HANDLE') alpc_port_attr = APIProxy.alloc_c_struct('ALPC_PORT_ATTRIBUTES') alpc_port_attr.Flags = TurboRex::Windows::ALPC::ALPC_PORFLG_ALLOW_LPC_REQUESTS alpc_port_attr.MaxMessageLength = 0x1000 alpc_port_attr.MemoryBandwidth = 0 alpc_port_attr.MaxPoolUsage = 0xFFFFFFFF alpc_port_attr.MaxSectionSize = 0xFFFFFFFF alpc_port_attr.MaxViewSize = 0xFFFFFFFF alpc_port_attr.MaxTotalSectionSize = 0xFFFFFFFF alpc_port_attr.DupObjectTypes = 0xFFFFFFFF alpc_port_attr.SecurityQos.Length = alpc_port_attr.SecurityQos.sizeof alpc_port_attr.SecurityQos.ImpersonationLevel = opts[:impersonation_level] || TurboRex::Windows::Constants::SecurityIdentification # timeout #large_integer = APIProxy.alloc_c_struct('LARGE_INTEGER') #large_integer.HighPart #large_integer.LowPart = PortMessage.new(payload: opts[:payload], alloc_size: (opts[:alloc_size]||3800)) obj_attr = opts[:obj_attr] || 0 flags = opts[:flags] || TurboRex::Windows::ALPC::ALPC_MSGFLG_SYNC_REQUEST # Don't use the ALPC_MSGFLG_SYNC_REQUEST flag when specific attributes timeout = opts[:timeout] || 0 retry_count = opts[:retry_count] || 0 buf_len = nil if = . buf_len = APIProxy.alloc_c_type('SIZE_T') buf_len[0] = end out_msg_attr = 0 in_msg_attr = 0 if opts[:client_obj_attr] # perform to call NtAlpcConnectPortEx raise NotImplementedError else ntstatus = APIProxy.ntalpcconnectport(handle, dest_str, obj_attr, alpc_port_attr, flags, 0, ., buf_len, out_msg_attr, in_msg_attr, timeout) . formatted_status = TinySDK.format_hex_ntstatus(ntstatus) if formatted_status == 0xC0000041 puts "[-] The server refused the connection.(STATUS_PORT_CONNECTION_REFUSED)" retry_count.times do |i| puts "[*] Retrying..." ntstatus = APIProxy.ntalpcconnectport(handle, dest_str, obj_attr, alpc_port_attr, flags, 0, ., buf_len, out_msg_attr, in_msg_attr, timeout) break unless TinySDK.format_hex_ntstatus(ntstatus) == 0xC0000041 end elsif !TinySDK.nt_success?(ntstatus) raise TurboRex::Exception::NotNTSuccess.new("0x#{formatted_status.to_s(16).upcase}") end end @communication_handle << handle[0] return handle[0], end |
#listen(conn_handle, opts = {}, &block) ⇒ Object
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 |
# File 'lib/turborex/windows/alpc.rb', line 124 def listen(conn_handle, opts = {}, &block) @conn_handle = conn_handle #port_message = APIProxy.alloc_c_struct('PORT_MESSAGE') = APIProxy.alloc_c_ary('BYTE', 0x1000) = MessageAttribute.new.struct buf_len = APIProxy.alloc_c_type('SIZE_T') buf_len[0] = .sizeof retry_count = 0 while true begin # call NtAlpcSendWaitReceivePort will cause interpreter blocks, until you kill it. ntstatus = APIProxy.ntalpcsendwaitreceiveport(@conn_handle, opts[:flag] || 0, 0, 0, , buf_len, , 0) yield(, buf_len, , TinySDK.format_hex_ntstatus(ntstatus, hex_str: true)) if block_given? unless TinySDK.nt_success? ntstatus unless buf_len[0] == .sizeof = APIProxy.alloc_c_ary('BYTE', buf_len[0]) raise TurboRex::Exception::ALPC::BufferTooSmall else raise end end rescue => e if e.is_a? TurboRex::Exception::ALPC::BufferTooSmall raise TurboRex::Exception::ALPC::TooManyRetries if retry_count >= 2 retry_count += 1 retry else raise TurboRex::Exception::UnknownError end end = PortMessage.new(raw_message: ) break if (.type & 0xFFF) == TurboRex::Windows::ALPC::LPC_CONNECTION_REQUEST end return end |
#recv(handle, opts = {}) ⇒ Object
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 |
# File 'lib/turborex/windows/alpc.rb', line 185 def recv(handle, opts = {}) = opts.fetch(:port_message) { APIProxy.alloc_c_ary('BYTE', 0x1000) } buf_len = opts.fetch(:buf_len) { APIProxy.alloc_c_type('SIZE_T') } buf_len[0] = .sizeof #message_attr = opts.fetch(:message_attr) { APIProxy.alloc_c_struct('ALPC_MESSAGE_ATTRIBUTES') } = MessageAttribute.new.struct flag = opts[:flag] || 0 retry_count = opts[:retry_count] || 0 timeout = opts[:timeout] || 0 begin ntstatus = APIProxy.ntalpcsendwaitreceiveport(handle, flag, 0, 0, , buf_len, , timeout) unless TinySDK.nt_success? ntstatus unless buf_len[0] == .sizeof = APIProxy.alloc_c_ary('BYTE', buf_len[0]) raise TurboRex::Exception::ALPC::BufferTooSmall else raise end end rescue => e if e.is_a? TurboRex::Exception::ALPC::BufferTooSmall raise TurboRex::Exception::ALPC::TooManyRetries if retry_count >= 2 retry_count += 1 retry else raise TurboRex::Exception::NotNTSuccess.new(TinySDK.format_hex_ntstatus(ntstatus, hex_str: true)) end end PortMessage.new(raw_message: ) end |
#refuse_connect(opts = {}) ⇒ Object
371 372 373 374 |
# File 'lib/turborex/windows/alpc.rb', line 371 def refuse_connect(opts = {}) opts[:refuse] = true accept(opts) end |
#send(handle, port_message, message_attr, opts = {}) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/turborex/windows/alpc.rb', line 170 def send(handle, , , opts = {}) flag = opts[:flag] || 0 timeout = opts[:timeout] || 0 #buf_len = opts.fetch(:buf_len) { APIProxy.alloc_c_type('SIZE_T') } #buf_len = port_message.sizeof ntstatus = APIProxy.ntalpcsendwaitreceiveport(handle, flag, , , 0, 0, 0, timeout) unless TinySDK.nt_success?(ntstatus) raise TurboRex::Exception::NotNTSuccess.new TinySDK.format_hex_ntstatus(ntstatus, hex_str: true) end TinySDK.format_hex_ntstatus ntstatus end |
#send_recv(handle, send_message, send_message_attr, recv_message_attr, opts = {}) ⇒ Object
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 |
# File 'lib/turborex/windows/alpc.rb', line 218 def send_recv(handle, , , , opts = {}) = opts.fetch(:port_message) { APIProxy.alloc_c_ary('BYTE', 0x1000) } buf_len = opts.fetch(:buf_len) { APIProxy.alloc_c_type('SIZE_T') } buf_len[0] = .sizeof = flag = opts[:flag] || TurboRex::Windows::ALPC::ALPC_MSGFLG_SYNC_REQUEST retry_count = opts[:retry_count] || 0 timeout = opts[:timeout] || 0 begin ntstatus = APIProxy.ntalpcsendwaitreceiveport(handle, flag, , , , buf_len, , timeout) unless TinySDK.nt_success? ntstatus unless buf_len[0] == .sizeof = APIProxy.alloc_c_ary('BYTE', buf_len[0]) raise TurboRex::Exception::ALPC::BufferTooSmall else raise end end rescue => e if e.is_a? TurboRex::Exception::ALPC::BufferTooSmall raise TurboRex::Exception::ALPC::TooManyRetries if retry_count >= 2 retry_count += 1 retry else raise TurboRex::Exception::NotNTSuccess.new(TinySDK.format_hex_ntstatus(ntstatus, hex_str: true)) end end PortMessage.new(raw_message: ) end |