@@ -28,48 +28,6 @@ def initialize
28
28
] , self . class )
29
29
end
30
30
31
- def run_host ( host )
32
- begin
33
-
34
- udp_sock = Rex ::Socket ::Udp . create ( {
35
- 'LocalHost' => datastore [ 'CHOST' ] || nil ,
36
- 'Context' => { 'Msf' => framework , 'MsfExploit' => self }
37
- } )
38
- add_socket ( udp_sock )
39
-
40
- # new
41
- external_address = get_external_address ( udp_sock , host , datastore [ 'RPORT' ] ) || host
42
- actual_ext_port = map_port ( udp_sock , host , datastore [ 'RPORT' ] , datastore [ 'INTERNAL_PORT' ] , datastore [ 'EXTERNAL_PORT' ] , Rex ::Proto ::NATPMP . const_get ( datastore [ 'PROTOCOL' ] ) , datastore [ 'LIFETIME' ] )
43
-
44
- if actual_ext_port
45
- map_target = Rex ::Socket . source_address ( host )
46
- if ( datastore [ 'EXTERNAL_PORT' ] != actual_ext_port )
47
- print_status ( "#{ external_address } " +
48
- "#{ datastore [ 'EXTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
49
- "#{ datastore [ 'INTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } couldn't be forwarded" )
50
- end
51
- print_status ( "#{ external_address } " +
52
- "#{ actual_ext_port } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
53
- "#{ datastore [ 'INTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } forwarded" )
54
-
55
- # report NAT-PMP as being open
56
- report_service (
57
- :host => host ,
58
- :port => datastore [ 'RPORT' ] ,
59
- :proto => 'udp' ,
60
- :name => 'natpmp' ,
61
- :state => Msf ::ServiceState ::Open
62
- )
63
- end
64
- rescue ::Interrupt
65
- raise $!
66
- rescue ::Rex ::HostUnreachable , ::Rex ::ConnectionTimeout , ::Rex ::ConnectionRefused
67
- nil
68
- rescue ::Exception => e
69
- print_error ( "Unknown error: #{ e . class } #{ e . backtrace } " )
70
- end
71
- end
72
-
73
31
def run_host ( host )
74
32
begin
75
33
udp_sock = Rex ::Socket ::Udp . create ( {
@@ -78,34 +36,24 @@ def run_host(host)
78
36
)
79
37
add_socket ( udp_sock )
80
38
peer = "#{ host } :#{ datastore [ 'RPORT' ] } "
81
- vprint_status ( "#{ peer } Scanning #{ datastore [ 'PROTOCOL' ] } ports #{ datastore [ 'PORTS' ] } using NATPMP" )
82
-
83
- # first, send a request to get the external address
84
- udp_sock . sendto ( external_address_request , host , datastore [ 'RPORT' ] , 0 )
85
- external_address = nil
86
- while ( r = udp_sock . recvfrom ( 12 , 0.25 ) and r [ 1 ] )
87
- ( ver , op , result , epoch , external_address ) = parse_external_address_response ( r [ 0 ] )
88
- end
39
+ vprint_status ( "#{ peer } Scanning #{ protocol } ports #{ datastore [ 'PORTS' ] } using NATPMP" )
89
40
41
+ external_address = get_external_address ( udp_sock , host , datastore [ 'RPORT' ] )
90
42
if ( external_address )
91
43
print_good ( "#{ peer } responded with external address of #{ external_address } " )
92
44
else
93
45
vprint_status ( "#{ peer } didn't respond with an external address" )
94
46
return
95
47
end
96
48
97
- Rex ::Socket . portspec_crack ( datastore [ 'PORTS' ] ) . each do |port |
98
- # send one request to clear the mapping if *we've* created it before
99
- clear_req = map_port_request ( port , port , Rex ::Proto ::NATPMP . const_get ( datastore [ 'PROTOCOL' ] ) , 0 )
100
- udp_sock . sendto ( clear_req , host , datastore [ 'RPORT' ] , 0 )
101
- while ( r = udp_sock . recvfrom ( 16 , 1.0 ) and r [ 1 ] )
102
- end
49
+ # clear all mappings
50
+ map_port ( udp_sock , host , datastore [ 'RPORT' ] , 0 , 0 , Rex ::Proto ::NATPMP . const_get ( protocol ) , lifetime )
103
51
104
- # now try the real mapping
52
+ Rex :: Socket . portspec_crack ( datastore [ 'PORTS' ] ) . each do | port |
105
53
map_req = map_port_request ( port , port , Rex ::Proto ::NATPMP . const_get ( datastore [ 'PROTOCOL' ] ) , 1 )
106
54
udp_sock . sendto ( map_req , host , datastore [ 'RPORT' ] , 0 )
107
55
while ( r = udp_sock . recvfrom ( 16 , 1.0 ) and r [ 1 ] )
108
- handle_reply ( host , external_address , r )
56
+ break if handle_reply ( host , external_address , r )
109
57
end
110
58
end
111
59
@@ -136,6 +84,14 @@ def handle_reply(host, external_addr, pkt)
136
84
if ( int != ext )
137
85
state = Msf ::ServiceState ::Open
138
86
print_good ( "#{ peer } #{ external_addr } - #{ int } /#{ protocol } #{ state } because of successful mapping with unmatched ports" )
87
+ if inside_workspace_boundary? ( external_addr )
88
+ report_service (
89
+ :host => external_addr ,
90
+ :port => int ,
91
+ :proto => protocol ,
92
+ :state => state
93
+ )
94
+ end
139
95
else
140
96
state = Msf ::ServiceState ::Closed
141
97
print_status ( "#{ peer } #{ external_addr } - #{ int } /#{ protocol } #{ state } because of successful mapping with matched ports" ) if ( datastore [ 'DEBUG' ] )
@@ -145,21 +101,13 @@ def handle_reply(host, external_addr, pkt)
145
101
print_status ( "#{ peer } #{ external_addr } - #{ int } /#{ protocol } #{ state } because of code #{ result } response" ) if ( datastore [ 'DEBUG' ] )
146
102
end
147
103
148
- if inside_workspace_boundary? ( external_addr )
149
- report_service (
150
- :host => external_addr ,
151
- :port => int ,
152
- :proto => protocol ,
153
- :state => state
154
- )
155
- end
156
-
157
104
report_service (
158
105
:host => host ,
159
106
:port => pkt [ 2 ] ,
160
107
:name => 'natpmp' ,
161
108
:proto => 'udp' ,
162
109
:state => Msf ::ServiceState ::Open
163
110
)
111
+ true
164
112
end
165
113
end
0 commit comments