File: snmp-processes.nse

package info (click to toggle)
nmap 6.47-3%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 44,788 kB
  • ctags: 25,108
  • sloc: ansic: 89,741; cpp: 62,412; sh: 19,492; python: 17,323; xml: 11,413; perl: 2,529; makefile: 2,503; yacc: 608; lex: 469; asm: 372; java: 45
file content (140 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local nmap = require "nmap"
local shortport = require "shortport"
local snmp = require "snmp"
local stdnse = require "stdnse"
local table = require "table"

description = [[
Attempts to enumerate running processes through SNMP.
]]

---
-- @usage
-- nmap -sU -p 161 --script=snmp-processes <target>
-- @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.4
-- Created 01/15/2010 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
-- Revised 01/19/2010 - v0.2 - fixed loop that would occur if a mib did not exist
-- Revised 01/19/2010 - v0.3 - removed debugging output and renamed file
-- Revised 04/11/2010 - v0.4 - moved snmp_walk to snmp library <patrik@cqure.net>


portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})

--- 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 = {}
  local status

  socket:set_timeout(5000)
  try(socket:connect(host, port))

  status, shares = snmp.snmpWalk( socket, snmpoid )
  socket:close()

  if (not(status)) or ( 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