@@ -40,25 +40,29 @@ def run_host(host)
40
40
} )
41
41
add_socket ( udp_sock )
42
42
43
- # get the external address first
44
- vprint_status "#{ host } - NATPMP - Probing for external address"
45
- udp_sock . sendto ( external_address_request , host , datastore [ 'RPORT' ] , 0 )
46
- external_address = nil
47
- while ( r = udp_sock . recvfrom ( 12 , 1 ) and r [ 1 ] )
48
- ( ver , op , result , epoch , external_address ) = parse_external_address_response ( r [ 0 ] )
49
- end
43
+ # new
44
+ external_address = get_external_address ( udp_sock , host , datastore [ 'RPORT' ] ) || host
45
+ 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' ] )
46
+
47
+ if actual_ext_port
48
+ map_target = Rex ::Socket . source_address ( host )
49
+ if ( datastore [ 'EXTERNAL_PORT' ] != actual_ext_port )
50
+ print_status ( "#{ external_address } " +
51
+ "#{ datastore [ 'EXTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
52
+ "#{ datastore [ 'INTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } couldn't be forwarded" )
53
+ end
54
+ print_status ( "#{ external_address } " +
55
+ "#{ actual_ext_port } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
56
+ "#{ datastore [ 'INTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } forwarded" )
50
57
51
- vprint_status "#{ host } - NATPMP - Sending mapping request"
52
- # build the mapping request
53
- req = map_port_request (
54
- datastore [ 'INTERNAL_PORT' ] , datastore [ 'EXTERNAL_PORT' ] ,
55
- Rex ::Proto ::NATPMP . const_get ( datastore [ 'PROTOCOL' ] ) , datastore [ 'LIFETIME' ]
56
- )
57
- # send it
58
- udp_sock . sendto ( req , host , datastore [ 'RPORT' ] , 0 )
59
- # handle the reply
60
- while ( r = udp_sock . recvfrom ( 16 , 1 ) and r [ 1 ] )
61
- handle_reply ( Rex ::Socket . source_address ( host ) , host , external_address , r )
58
+ # report NAT-PMP as being open
59
+ report_service (
60
+ :host => host ,
61
+ :port => datastore [ 'RPORT' ] ,
62
+ :proto => 'udp' ,
63
+ :name => 'natpmp' ,
64
+ :state => Msf ::ServiceState ::Open
65
+ )
62
66
end
63
67
rescue ::Interrupt
64
68
raise $!
@@ -69,43 +73,4 @@ def run_host(host)
69
73
end
70
74
end
71
75
72
- def handle_reply ( map_target , host , external_address , pkt )
73
- return if not pkt [ 1 ]
74
-
75
- if ( pkt [ 1 ] =~ /^::ffff:/ )
76
- pkt [ 1 ] = pkt [ 1 ] . sub ( /^::ffff:/ , '' )
77
- end
78
-
79
- ( ver , op , result , epoch , internal_port , external_port , lifetime ) = parse_map_port_response ( pkt [ 0 ] )
80
-
81
- if ( result == 0 )
82
- if ( datastore [ 'EXTERNAL_PORT' ] != external_port )
83
- print_status ( "#{ external_address } " +
84
- "#{ datastore [ 'EXTERNAL_PORT' ] } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
85
- "#{ internal_port } /#{ datastore [ 'PROTOCOL' ] } couldn't be forwarded" )
86
- end
87
- print_status ( "#{ external_address } " +
88
- "#{ external_port } /#{ datastore [ 'PROTOCOL' ] } -> #{ map_target } " +
89
- "#{ internal_port } /#{ datastore [ 'PROTOCOL' ] } forwarded" )
90
- end
91
-
92
- # report NAT-PMP as being open
93
- report_service (
94
- :host => host ,
95
- :port => pkt [ 2 ] ,
96
- :proto => 'udp' ,
97
- :name => 'natpmp' ,
98
- :state => Msf ::ServiceState ::Open
99
- )
100
-
101
- # report the external port as being open
102
- if inside_workspace_boundary? ( external_address )
103
- report_service (
104
- :host => external_address ,
105
- :port => external_port ,
106
- :proto => datastore [ 'PROTOCOL' ] . to_s . downcase ,
107
- :state => Msf ::ServiceState ::Open
108
- )
109
- end
110
- end
111
76
end
0 commit comments