Merge from /nmap-exp/patrick/nse-nsock-maintenance.

This is a maintenance fix for the NSE Nsock library binding. The patch focuses
on code correctness and simplicity. The patch also brings some initial updates
with an eye towards the upcoming Lua 5.2 release. See [1] for a post concerning
this branch.

[1] http://seclists.org/nmap-dev/2010/q3/710
This commit is contained in:
batrick 2010-09-18 20:35:09 +00:00
parent 5f13514d46
commit de4ba536de
20 changed files with 1446 additions and 2167 deletions

View file

@ -84,13 +84,6 @@ local checkpkt = function(reply, orig)
return true
end
--- pcap callback
-- @return destination ip address, the ip protocol and icmp type
local callback = function(size, layer2, layer3)
local ip = packet.Packet:new(layer3, layer3:len())
return bin.pack('ACC', ip.ip_bin_dst, ip.ip_p, ip.icmp_type)
end
--- set destination port and ip ttl to a generic tcp packet
-- @param ip the ip object
-- @param dport the layer 4 destination port
@ -274,6 +267,13 @@ local portrange = function(ports)
return stdnse.strjoin(",", strrange)
end
--- pcap check function
-- @return destination ip address, the ip protocol and icmp type
local function check (size, layer2, layer3)
local ip = packet.Packet:new(layer3, layer3:len())
return bin.pack('ACC', ip.ip_bin_dst, ip.ip_p, ip.icmp_type)
end
-- main firewalking logic
action = function(host)
local sock = nmap.new_dnet()
@ -290,7 +290,7 @@ action = function(host)
end
-- filter for incoming icmp time exceeded replies
pcap:pcap_open(host.interface, 104, 0, callback, "icmp and dst host " .. saddr)
pcap:pcap_open(host.interface, 104, false, "icmp and dst host " .. saddr)
try(sock:ip_open())
@ -309,11 +309,14 @@ action = function(host)
while retry < MAX_RETRIES do
try(sock:ip_send(pkt.buf))
pcap:pcap_register(bin.pack('ACC', pkt.ip_bin_src, packet.IPPROTO_ICMP, ICMP_TIME_EXCEEDED))
local status, _, _, rep = pcap:pcap_receive()
local status, length, layer2, layer3 = pcap:pcap_receive();
local test = bin.pack('ACC', pkt.ip_bin_src, packet.IPPROTO_ICMP, ICMP_TIME_EXCEEDED);
while status and test ~= check(length, layer2, layer3) do
status, length, layer2, layer3 = pcap:pcap_receive();
end
if status then
if checkpkt(rep, pkt) then
if checkpkt(layer3, pkt) then
stdnse.print_debug(1, "Firewalk: discovered fwd port " .. port)
table.insert(fwdports, port)
break

View file

@ -33,9 +33,9 @@ require 'packet'
local NUMPROBES = 6
--- Pcap callback
--- Pcap check function
-- @return Destination and source IP addresses and TCP ports
local callback = function(size, layer2, layer3)
local function check (size, layer2, layer3)
local ip = packet.Packet:new(layer3, layer3:len())
return bin.pack('AA=S=S', ip.ip_bin_dst, ip.ip_bin_src, ip.tcp_dport, ip.tcp_sport)
end
@ -222,7 +222,7 @@ action = function(host)
try = nmap.new_try(function() sock:ip_close() end)
pcap:pcap_open(host.interface, 104, 0, callback, "tcp and dst host " .. saddr .. " and src host " .. daddr .. " and src port " .. port)
pcap:pcap_open(host.interface, 104, false, "tcp and dst host " .. saddr .. " and src host " .. daddr .. " and src port " .. port)
pcap:set_timeout(host.times.timeout * 1000)
@ -231,12 +231,14 @@ action = function(host)
while i <= NUMPROBES do
try(sock:ip_send(tcp.buf))
pcap:pcap_register(bin.pack('AA=S=S', tcp.ip_bin_src, tcp.ip_bin_dst, tcp.tcp_sport, tcp.tcp_dport))
local status, len, _, pkt = pcap:pcap_receive()
local status, len, layer2, layer3 = pcap:pcap_receive()
local test = bin.pack('AA=S=S', tcp.ip_bin_src, tcp.ip_bin_dst, tcp.tcp_sport, tcp.tcp_dport)
while status and test ~= check(len, layer2, layer3) do
status, len, layer2, layer3 = pcap:pcap_receive()
end
if status then
table.insert(ipids, packet.u16(pkt, 4))
table.insert(ipids, packet.u16(layer3, 4))
end
updatepkt(tcp)

View file

@ -145,7 +145,7 @@ end
-- This is all we can use since we can get various protocols back from
-- different hosts
local callback = function(size, layer2, layer3)
local function check (size, layer2, layer3)
local ip = packet.Packet:new(layer3, layer3:len())
return bin.pack('A', ip.ip_bin_dst)
end
@ -306,7 +306,7 @@ action = function(host)
try = nmap.new_try(function() sock:ip_close() end)
pcap:pcap_open(host.interface, 104, 0, callback, "dst host " .. saddr .. " and (icmp or (" .. proto .. " and src host " .. daddr .. " and src port " .. port .. "))")
pcap:pcap_open(host.interface, 104, false, "dst host " .. saddr .. " and (icmp or (" .. proto .. " and src host " .. daddr .. " and src port " .. port .. "))")
-- Since we're sending potentially large amounts of data per packet,
-- simply bump up the host's calculated timeout value. Most replies
@ -337,12 +337,14 @@ action = function(host)
end
end
pcap:pcap_register(bin.pack('A', pkt.ip_bin_src))
status, _, _, ip = pcap:pcap_receive()
local test = bin.pack('A', pkt.ip_bin_src)
local status, length, layer2, layer3 = pcap:pcap_receive()
while status and test ~= check(length, layer2, layer3) do
status, length, layer2, layer3 = pcap:pcap_receive()
end
if status then
local t, v = checkpkt(ip, pkt)
local t, v = checkpkt(layer3, pkt)
if t == "gotreply" then
gotit = true
break

View file

@ -157,9 +157,9 @@ local tstat = function(n1, n2, u1, u2, v1, v2)
return math.abs(u1 - u2) / math.sqrt(a * (b / dof))
end
--- Pcap callback
--- Pcap check
-- @return Destination and source IP addresses and TCP ports
local callback = function(size, layer2, layer3)
local function check (size, layer2, layer3)
local ip = packet.Packet:new(layer3, layer3:len())
return bin.pack('AA=S=S', ip.ip_bin_dst, ip.ip_bin_src, ip.tcp_dport, ip.tcp_sport)
end
@ -418,7 +418,7 @@ action = function(host)
local conf, delay, numtrips = try(getopts())
pcap:pcap_open(host.interface, 104, 0, callback, "tcp and dst host " .. saddr .. " and src host " .. daddr)
pcap:pcap_open(host.interface, 104, false, "tcp and dst host " .. saddr .. " and src host " .. daddr)
try(sock:ip_open())
@ -452,15 +452,17 @@ action = function(host)
stats[j].fam = -1
end
pcap:pcap_register(bin.pack('AA=S=S', tcp.ip_bin_src, tcp.ip_bin_dst, tcp.tcp_sport, tcp.tcp_dport))
start = stdnse.clock_us()
try(sock:ip_send(tcp.buf))
stats[j].sent = stats[j].sent + 1
local status, len, _, pkt, stop = pcap:pcap_receive()
local test = bin.pack('AA=S=S', tcp.ip_bin_src, tcp.ip_bin_dst, tcp.tcp_sport, tcp.tcp_dport)
local status, length, layer2, layer3, stop = pcap:pcap_receive()
while status and test ~= check(length, layer2, layer3) do
status, length, layer2, layer3, stop = pcap:pcap_receive()
end
if not stop then
-- probably a timeout, just grab current time

View file

@ -24,14 +24,13 @@ hostrule = function(host)
nmap.get_interface_link(host.interface) == 'ethernet'
end
callback = function(packetsz, layer2, layer3)
local function check (packetsz, layer2, layer3)
return string.sub(layer2, 0, 12)
end
do_test = function(dnet, pcap, host, test)
local _
local status
local status, length, layer2, layer3
local i = 0
-- ARP requests are send with timeouts: 10ms, 40ms, 90ms
@ -41,15 +40,21 @@ do_test = function(dnet, pcap, host, test)
-- flush buffers :), wait quite long.
repeat
pcap:set_timeout(100)
pcap:pcap_register(host.mac_addr_src .. host.mac_addr)
status ,_,_,_ = pcap:pcap_receive()
local test = host.mac_addr_src .. host.mac_addr
status, length, layer2, layer3 = pcap:pcap_receive()
while status and test ~= check(length, layer2, layer3) do
status, length, layer2, layer3 = pcap:pcap_receive()
end
until status ~= true
pcap:set_timeout(10 * i*i)
pcap:pcap_register(host.mac_addr_src .. host.mac_addr)
dnet:ethernet_send(test)
status ,_,_,_ = pcap:pcap_receive()
local test = host.mac_addr_src .. host.mac_addr
status, length, layer2, layer3 = pcap:pcap_receive()
while status and test ~= check(length, layer2, layer3) do
status, length, layer2, layer3 = pcap:pcap_receive()
end
if status == true then
-- the basic idea, was to inform user about time, when we got packet
-- so that 1 would mean (0-10ms), 2=(10-40ms) and 3=(40ms-90ms)
@ -79,7 +84,7 @@ action = function(host)
}
dnet:ethernet_open(host.interface)
pcap:pcap_open(host.interface, 64, 0, callback, "arp")
pcap:pcap_open(host.interface, 64, false, "arp")
local test_static = host.mac_addr_src ..
string.char(0x08,0x06, 0x00,0x01, 0x08,0x00, 0x06,0x04, 0x00,0x01) ..