1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-02 04:49:02 +00:00

o [NSE] Added a new library for ASN.1 parsing and adapted the SNMP library to

make use of it. Added 5 scripts that use the new libraries:
  - snmp-netstat shows listening and connected sockets
  - snmp-processes shows process information including name, pid, path and 
    parameters
  - snmp-win32-services shows the names of running Windows services
  - snmp-win32-shares shows the names and path of Windows shares
  - snmp-win32-software shows a list of installed Windows software
  - snmp-win32-users shows a list of local Windows users
This commit is contained in:
patrik
2010-02-16 09:15:38 +00:00
parent 3f36981440
commit d2e54f0bf2
9 changed files with 1456 additions and 301 deletions

145
scripts/snmp-netstat.nse Normal file
View File

@@ -0,0 +1,145 @@
description = [[
Attempts to query SNMP for a netstat like output
]]
---
-- @output
-- | snmp-netstat:
-- | TCP 0.0.0.0:21 0.0.0.0:2256
-- | TCP 0.0.0.0:80 0.0.0.0:8218
-- | TCP 0.0.0.0:135 0.0.0.0:53285
-- | TCP 0.0.0.0:389 0.0.0.0:38990
-- | TCP 0.0.0.0:445 0.0.0.0:49158
-- | TCP 127.0.0.1:389 127.0.0.1:1045
-- | TCP 127.0.0.1:389 127.0.0.1:1048
-- | UDP 192.168.56.3:137 *:*
-- | UDP 192.168.56.3:138 *:*
-- | UDP 192.168.56.3:389 *:*
-- |_ UDP 192.168.56.3:464 *:*
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.1
-- Created 01/19/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
local lip = oid:match( "^" .. base_oid .. "%.(%d+%.%d+%.%d+%.%d+)") or ""
local lport = oid:match( "^" .. base_oid .. "%.%d+%.%d+%.%d+%.%d+%.(%d+)")
local fip = oid:match( "^" .. base_oid .. "%.%d+%.%d+%.%d+%.%d+%.%d+%.(%d+%.%d+%.%d+%.%d+)") or "*:*"
local fport = oid:match( "^" .. base_oid .. "%.%d+%.%d+%.%d+%.%d+%.%d+%.%d+%.%d+%.%d+%.%d+%.(%d+)")
if lport and lport ~= "0" then
lip = lip .. ":" .. lport
end
if fport and fport ~= "0" then
fip = fip .. ":" .. fport
end
value = string.format("%-20s %s", lip, fip )
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
snmp_table.baseoid = base_oid
return snmp_table
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl, prefix )
local new_tab = {}
for _, v in ipairs( tbl ) do
table.insert( new_tab, string.format( "%-4s %s", prefix, v.value ) )
end
return new_tab
end
function table_merge( t1, t2 )
for _, v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local tcp_oid = "1.3.6.1.2.1.6.13.1.1"
local udp_oid = "1.3.6.1.2.1.7.5.1.1"
local netstat = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
local tcp = snmp_walk( socket, tcp_oid )
local udp = snmp_walk( socket, udp_oid )
if ( tcp == nil ) or ( #tcp == 0 ) or ( udp==nil ) or ( #udp == 0 ) then
return
end
tcp = process_answer(tcp, "TCP")
udp = process_answer(udp, "UDP")
netstat = table_merge( tcp, udp )
nmap.set_port_state(host, port, "open")
socket:close()
return stdnse.format_output( true, netstat )
end

176
scripts/snmp-processes.nse Normal file
View File

@@ -0,0 +1,176 @@
description = [[
Attempts to enumerate running processes through SNMP
]]
---
-- @output
-- | snmp-processes:
-- | System Idle Process
-- | PID: 1
-- | System
-- | PID: 4
-- | smss.exe
-- | Path: \SystemRoot\System32\
-- | PID: 256
-- | csrss.exe
-- | Path: C:\WINDOWS\system32\
-- | Params: ObjectDirectory=\Windows SharedSection=1024,3072,512 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserS
-- | PID: 308
-- | winlogon.exe
-- | PID: 332
-- | services.exe
-- | Path: C:\WINDOWS\system32\
-- | PID: 380
-- | lsass.exe
-- | Path: C:\WINDOWS\system32\
-- |_ PID: 392
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.3
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occure if a mib did not exist
-- Revised 01/19/2010 - v0.2 - removed debugging output and renamed file
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
socket:close()
snmp_table.baseoid = base_oid
return snmp_table
end
--- Gets a value for the specified oid
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @param oid string containing the object id for which the value should be extracted
-- @return value of relevant type or nil if oid was not found
function get_value_from_table( tbl, oid )
for _, v in ipairs( tbl ) do
if v.oid == oid then
return v.value
end
end
return nil
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl )
local swrun_name = "1.3.6.1.2.1.25.4.2.1.2"
local swrun_pid = "1.3.6.1.2.1.25.4.2.1.1"
local swrun_path = "1.3.6.1.2.1.25.4.2.1.4"
local swrun_params = "1.3.6.1.2.1.25.4.2.1.5"
local new_tbl = {}
for _, v in ipairs( tbl ) do
if ( v.oid:match("^" .. swrun_name) ) then
local item = {}
local objid = v.oid:gsub( "^" .. swrun_name, swrun_path)
local value = get_value_from_table( tbl, objid )
if value and value:len() > 0 then
table.insert( item, ("Path: %s"):format( value ) )
end
objid = v.oid:gsub( "^" .. swrun_name, swrun_params)
value = get_value_from_table( tbl, objid )
if value and value:len() > 0 then
table.insert( item, ("Params: %s"):format( value ) )
end
objid = v.oid:gsub( "^" .. swrun_name, swrun_pid)
value = get_value_from_table( tbl, objid )
if value then
table.insert( item, ("PID: %s"):format( value ) )
end
item.name = v.value
table.insert( item, value )
table.insert( new_tbl, item )
end
end
return new_tbl
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local data, snmpoid = nil, "1.3.6.1.2.1.25.4.2"
local shares = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
shares = snmp_walk( socket, snmpoid )
if ( shares == nil ) or ( #shares == 0 ) then
return
end
shares = process_answer( shares )
nmap.set_port_state(host, port, "open")
return stdnse.format_output( true, shares )
end

View File

@@ -0,0 +1,121 @@
description = [[
Attempts to enumerate Windows Services through SNMP
]]
---
-- @output
-- | snmp-win32-services:
-- | Apache Tomcat
-- | Application Experience Lookup Service
-- | Application Layer Gateway Service
-- | Automatic Updates
-- | COM+ Event System
-- | COM+ System Application
-- | Computer Browser
-- | Cryptographic Services
-- | DB2 - DB2COPY1 - DB2
-- | DB2 Management Service (DB2COPY1)
-- | DB2 Remote Command Server (DB2COPY1)
-- | DB2DAS - DB2DAS00
-- |_ DCOM Server Process Launcher
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.2
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occure if a mib did not exist
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
socket:close()
snmp_table.baseoid = base_oid
return snmp_table
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl )
local new_tab = {}
for _, v in ipairs( tbl ) do
table.insert( new_tab, v.value )
end
table.sort( new_tab )
return new_tab
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmpoid = "1.3.6.1.4.1.77.1.2.3.1.1"
local services = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
services = snmp_walk( socket, snmpoid )
if ( services == nil ) or ( #services == 0 ) then
return
end
services = process_answer(services)
nmap.set_port_state(host, port, "open")
return stdnse.format_output( true, services )
end

View File

@@ -0,0 +1,142 @@
description = [[
Attempts to enumerate Windows Shares through SNMP
]]
---
-- @output
-- | snmp-win32-shares:
-- | SYSVOL
-- | C:\WINDOWS\sysvol\sysvol
-- | NETLOGON
-- | C:\WINDOWS\sysvol\sysvol\inspectit-labb.local\SCRIPTS
-- | Webapps
-- |_ C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.2
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occure if a mib did not exist
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
socket:close()
snmp_table.baseoid = base_oid
return snmp_table
end
--- Gets a value for the specified oid
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @param oid string containing the object id for which the value should be extracted
-- @return value of relevant type or nil if oid was not found
function get_value_from_table( tbl, oid )
for _, v in ipairs( tbl ) do
if v.oid == oid then
return v.value
end
end
return nil
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl )
local share_name = "1.3.6.1.4.1.77.1.2.27.1.1"
local share_path = "1.3.6.1.4.1.77.1.2.27.1.2"
local new_tbl = {}
for _, v in ipairs( tbl ) do
if ( v.oid:match("^" .. share_name) ) then
local item = {}
local objid = v.oid:gsub( "^" .. share_name, share_path)
local path = get_value_from_table( tbl, objid )
item.name = v.value
table.insert( item, path )
table.insert( new_tbl, item )
end
end
return new_tbl
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local data, snmpoid = nil, "1.3.6.1.4.1.77.1.2.27"
local shares = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
shares = snmp_walk( socket, snmpoid )
if ( shares == nil ) or ( #shares == 0 ) then
return
end
shares = process_answer( shares )
nmap.set_port_state(host, port, "open")
return stdnse.format_output( true, shares )
end

View File

@@ -0,0 +1,147 @@
description = [[
Attempts to enumerate installed software through SNMP
]]
---
-- @output
-- | snmp-win32-software:
-- | Apache Tomcat 5.5 (remove only); 2007-09-15 15:13:18
-- | Microsoft Internationalized Domain Names Mitigation APIs; 2007-09-15 15:13:18
-- | Security Update for Windows Media Player (KB911564); 2007-09-15 15:13:18
-- | Security Update for Windows Server 2003 (KB924667-v2); 2007-09-15 15:13:18
-- | Security Update for Windows Media Player 6.4 (KB925398); 2007-09-15 15:13:18
-- | Security Update for Windows Server 2003 (KB925902); 2007-09-15 15:13:18
-- |_ Windows Internet Explorer 7; 2007-09-15 15:13:18
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.2
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occure if a mib did not exist
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
socket:close()
snmp_table.baseoid = base_oid
return snmp_table
end
--- Gets a value for the specified oid
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @param oid string containing the object id for which the value should be extracted
-- @return value of relevant type or nil if oid was not found
function get_value_from_table( tbl, oid )
for _, v in ipairs( tbl ) do
if v.oid == oid then
return v.value
end
end
return nil
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl )
local sw_name = "1.3.6.1.2.1.25.6.3.1.2"
local sw_date = "1.3.6.1.2.1.25.6.3.1.5"
local new_tbl = {}
for _, v in ipairs( tbl ) do
if ( v.oid:match("^" .. sw_name) ) then
local objid = v.oid:gsub( "^" .. sw_name, sw_date)
local install_date = get_value_from_table( tbl, objid )
local sw_item
local _, year, month, day, hour, min, sec = bin.unpack( ">SCCCCC", install_date )
install_date = ("%02d-%02d-%02d %02d:%02d:%02d"):format( year, month, day, hour, min, sec )
sw_item = ("%s; %s"):format(v.value ,install_date)
table.insert( new_tbl, sw_item )
end
end
table.sort( new_tbl )
return new_tbl
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local data, snmpoid = nil, "1.3.6.1.2.1.25.6.3.1"
local sw = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
sw = snmp_walk( socket, snmpoid )
if ( sw == nil ) or ( #sw == 0 ) then
return
end
sw = process_answer( sw )
nmap.set_port_state(host, port, "open")
return stdnse.format_output( true, sw )
end

View File

@@ -0,0 +1,118 @@
description = [[
Attempts to enumerate User Accounts through SNMP
]]
---
-- @output
-- | snmp-win32-users:
-- | Administrator
-- | Guest
-- | IUSR_EDUSRV011
-- | IWAM_EDUSRV011
-- | SUPPORT_388945a0
-- | Tomcat
-- | db2admin
-- | ldaptest
-- |_ patrik
author = "Patrik Karlsson"
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "safe"}
dependencies = {"snmp-brute"}
-- Version 0.2
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occure if a mib did not exist
require "shortport"
require "snmp"
portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
--- Walks the MIB Tree
--
-- @param socket socket already connected to the server
-- @base_oid string containing the base object ID to walk
-- @return table containing <code>oid</code> and <code>value</code>
function snmp_walk( socket, base_oid )
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmp_table = {}
local oid = base_oid
while ( true ) do
local value, response, snmpdata, options, item = nil, nil, nil, {}, {}
options.reqId = 28428 -- unnecessary?
payload = snmp.encode( snmp.buildPacket( snmp.buildGetNextRequest(options, oid) ) )
try(socket:send(payload))
response = try( socket:receive_bytes(1) )
snmpdata = snmp.fetchResponseValues( response )
value = snmpdata[1][1]
oid = snmpdata[1][2]
if not oid:match( base_oid ) or base_oid == oid then
break
end
item.oid = oid
item.value = value
table.insert( snmp_table, item )
end
socket:close()
snmp_table.baseoid = base_oid
return snmp_table
end
--- Processes the table and creates the script output
--
-- @param tbl table containing <code>oid</code> and <code>value</code>
-- @return table suitable for <code>stdnse.format_output</code>
function process_answer( tbl )
local new_tab = {}
for _, v in ipairs( tbl ) do
table.insert( new_tab, v.value )
end
table.sort( new_tab )
return new_tab
end
action = function(host, port)
local socket = nmap.new_socket()
local catch = function() socket:close() end
local try = nmap.new_try(catch)
local snmpoid = "1.3.6.1.4.1.77.1.2.25"
local users = {}
socket:set_timeout(5000)
try(socket:connect(host.ip, port.number, "udp"))
users = snmp_walk( socket, snmpoid )
users = process_answer( users )
if ( users == nil ) or ( #users == 0 ) then
return
end
nmap.set_port_state(host, port, "open")
return stdnse.format_output( true, users )
end