2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2012-01-24 16:16:56 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# NAT-PMP protocol support
|
|
|
|
#
|
|
|
|
# by Jon Hart <jhart@spoofed.org>
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module NATPMP
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Return a NAT-PMP request to get the external address.
|
2014-08-22 17:34:05 +00:00
|
|
|
def external_address_request
|
2013-08-30 21:28:33 +00:00
|
|
|
[ 0, 0 ].pack('nn')
|
|
|
|
end
|
2012-01-24 16:16:56 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Parse a NAT-PMP external address response +resp+.
|
|
|
|
# Returns the decoded parts of the response as an array.
|
2014-08-22 17:34:05 +00:00
|
|
|
def parse_external_address_response(resp)
|
|
|
|
(ver, op, result, epoch, addr) = resp.unpack("CCnNN")
|
2013-08-30 21:28:33 +00:00
|
|
|
[ ver, op, result, epoch, Rex::Socket::addr_itoa(addr) ]
|
|
|
|
end
|
2012-01-24 16:16:56 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Return a NAT-PMP request to map remote port +rport+/+protocol+ to local port +lport+ for +lifetime+ ms
|
2014-08-22 17:34:05 +00:00
|
|
|
def map_port_request(lport, rport, protocol, lifetime)
|
|
|
|
[ Rex::Proto::NATPMP::Version, # version
|
2013-08-30 21:28:33 +00:00
|
|
|
protocol, # opcode, which is now the protocol we are asking to forward
|
|
|
|
0, # reserved
|
|
|
|
lport,
|
|
|
|
rport,
|
|
|
|
lifetime
|
2014-06-30 07:48:18 +00:00
|
|
|
].pack("CCnnnN")
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2012-01-24 16:16:56 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Parse a NAT-PMP mapping response +resp+.
|
|
|
|
# Returns the decoded parts as an array.
|
2014-08-22 17:34:05 +00:00
|
|
|
def parse_map_port_response(resp)
|
2014-08-22 19:41:35 +00:00
|
|
|
resp.unpack("CCnNnnN")
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2012-01-24 16:16:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|