summaryrefslogtreecommitdiffstats
path: root/scripts/smb-enum-processes.nse
blob: cb81df52e96b6eb72b5a8b8565c2bd9b0c1d5260 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local msrpcperformance = require "msrpcperformance"
local nmap = require "nmap"
local smb = require "smb"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"

description = [[
Pulls a list of processes from the remote server over SMB. This will determine
all running processes, their process IDs, and their parent processes. It is done
by querying the remote registry service, which is disabled by default on Vista;
on all other Windows versions, it requires Administrator privileges.

Since this requires administrator privileges, it isn't especially useful for a
penetration tester, since they can effectively do the same thing with metasploit
or other tools. It does, however, provide for a quick way to get process lists
for a bunch of systems at the same time.

WARNING: I have experienced crashes in <code>regsvc.exe</code> while making registry calls
against a fully patched Windows 2000 system; I've fixed the issue that caused
it, but there's no guarantee that it (or a similar vulnerability in the same code) won't
show up again. Since the process automatically restarts, it doesn't negatively
impact the system, besides showing a message box to the user.
]]

---
-- @usage
-- nmap --script smb-enum-processes.nse -p445 <host>
-- sudo nmap -sU -sS --script smb-enum-processes.nse -p U:137,T:139 <host>
--
---
-- @output
-- Host script results:
-- |  smb-enum-processes:
-- |_ |_ Idle, System, smss, csrss, winlogon, services, logon.scr, lsass, spoolsv, msdtc, VMwareService, svchost, alg, explorer, VMwareTray, VMwareUser, wmiprvse
--
-- --
-- Host script results:
-- |  smb-enum-processes:
-- |  `+-Idle
-- |   | `-System
-- |   |   `-smss
-- |   |     `+-csrss
-- |   |      `-winlogon
-- |   |        `+-services
-- |   |         | `+-spoolsv
-- |   |         |  +-msdtc
-- |   |         |  +-VMwareService
-- |   |         |  +-svchost
-- |   |         |  `-alg
-- |   |         +-logon.scr
-- |   |         `-lsass
-- |   +-explorer
-- |   | `+-VMwareTray
-- |   |  `-VMwareUser
-- |_  `-wmiprvse
--
-- --
-- Host script results:
-- |  smb-enum-processes:
-- |   PID  PPID  Priority Threads Handles
-- |  ----- ----- -------- ------- -------
-- |      0     0        0       1       0 `+-Idle
-- |      4     0        8      49     395  | `-System
-- |    252     4       11       3      19  |   `-smss
-- |    300   252       13      10     338  |     `+-csrss
-- |    324   252       13      18     513  |      `-winlogon
-- |    372   324        9      16     272  |        `+-services
-- |    872   372        8      12     121  |         | `+-spoolsv
-- |    896   372        8      13     151  |         |  +-msdtc
-- |   1172   372       13       3      53  |         |  +-VMwareService
-- |   1336   372        8      20     158  |         |  +-svchost
-- |   1476   372        8       6      90  |         |  `-alg
-- |    376   324        4       1      22  |         +-logon.scr
-- |    384   324        9      23     394  |         `-lsass
-- |   1720  1684        8       9     259  +-explorer
-- |   1796  1720        8       1      42  | `+-VMwareTray
-- |   1808  1720        8       1      44  |  `-VMwareUser
-- |_  1992   580        8       7     179  `-wmiprvse
-----------------------------------------------------------------------

author = "Ron Bowes"
copyright = "Ron Bowes"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"discovery", "intrusive"}
dependencies = {"smb-brute"}


function psl_mode (list, i)
  local mode

  -- Decide connector for process.
  if #list == 1 then
    mode = "only"
  elseif i == 1 then
    mode = "first"
  elseif i < #list then
    mode = "middle"
  else
    mode = "last"
  end

  return mode
end

function psl_print (psl, lvl)
  -- Print out table header.
  local result = {}
  if lvl == 2 then
    result[#result+1] = " PID  PPID  Priority Threads Handles\n"
    result[#result+1] = "----- ----- -------- ------- -------\n"
  end

  -- Find how many root processes there are.
  local roots = {}
  for i, ps in pairs(psl) do
    if psl[ps.ppid] == nil or ps.ppid == ps.pid then
      table.insert(roots, i)
    end
  end
  table.sort(roots)

  -- Create vertical sibling bars.
  local bars = {}
  if #roots ~= 1 then
    table.insert(bars, 2)
  end

  -- Print out each root of the tree.
  for i, root in ipairs(roots) do
    local mode = psl_mode(roots, i)
    psl_tree(psl, root, 0, bars, mode, lvl, result)
  end

  return table.concat(result)
end

function psl_tree (psl, pid, column, bars, mode, lvl, result)
  local ps = psl[pid]

  -- Delete vertical sibling link.
  if mode == "last" then
    table.remove(bars)
  end

  -- Print information table.
  local info = ""
  if lvl == 2 then
    info = string.format("% 5d % 5d % 8d % 7d % 7d ", ps.pid, ps.ppid, ps.prio, ps.thrd, ps.hndl)
  end

  -- Print vertical sibling bars.
  local prefix = ""
  for i=1, #bars do
    prefix = prefix .. string.rep(" ", bars[i] - 1) .. "|"
  end

  -- Strings used to separate processes from one another.
  local separators = {
    first  = "`+-";
    last  = " `-";
    middle  = " +-";
    only  = "`-";
  }

  -- Format process itself.
  result[#result+1] = "\n" .. info .. prefix .. separators[mode] .. ps.name

  -- Find children of the process.
  local children = {}
  for child_pid, child in pairs(psl) do
    if child_pid ~= pid and child.ppid == pid then
      table.insert(children, child_pid)
    end
  end
  table.sort(children)

  -- Add vertical sibling link between children.
  column = column + #separators[mode]
  if #children > 1 then
    table.insert(bars, column + 2)
  end

  -- Format process's children.
  for i, pid in ipairs(children) do
    local mode = psl_mode(children, i)
    psl_tree(psl, pid, column, bars, mode, lvl, result)
  end

  return result
end

hostrule = function(host)
  return smb.get_port(host) ~= nil
end

action = function(host)
  -- Get the process list
  local status, result = msrpcperformance.get_performance_data(host, "230")
  if status == false then
    return stdnse.format_output(false, result)
  end

  -- Get the process table
  local process = result["Process"]

  -- Put the processes into an array, and sort them by pid.
  local names = {}
  for i, v in pairs(process) do
    if i ~= "_Total" then
      names[#names + 1] = i
    end
  end
  table.sort(names, function (a, b) return process[a]["ID Process"] < process[b]["ID Process"] end)

  -- Put the processes into an array indexed by pid and with a value equal
  -- to the name (so we can look it up easily when we need to).
  local process_id = {}
  for i, v in pairs(process) do
    process_id[v["ID Process"]] = i
  end

  -- Fill the process list table.
  --
  -- Used fields:
  --   Creating Process ID
  --   Handle Count
  --   ID Process
  --   Priority Base
  --   Thread Count
  --
  -- Unused fields:
  --   % Privileged Time
  --   % Processor Time
  --   % User Time
  --   Elapsed Time
  --   IO Data Bytes/sec
  --   IO Data Operations/sec
  --   IO Other Bytes/sec
  --   IO Other Operations/sec
  --   IO Read Bytes/sec
  --   IO Read Operations/sec
  --   IO Write Bytes/sec
  --   IO Write Operations/sec
  --   Page Faults/sec
  --   Page File Bytes
  --   Page File Bytes Peak
  --   Pool Nonpaged Bytes
  --   Pool Paged Bytes
  --   Private Bytes
  --   Virtual Bytes
  --   Virtual Bytes Peak
  --   Working Set
  --   Working Set Peak
  local psl = {}
  for i, name in ipairs(names) do
    if name ~= "_Total" then
      psl[process[name]["ID Process"]] = {
        name  = name;
        pid  = process[name]["ID Process"];
        ppid  = process[name]["Creating Process ID"];
        prio  = process[name]["Priority Base"];
        thrd  = process[name]["Thread Count"];
        hndl  = process[name]["Handle Count"];
      }
    end
  end

  -- Produce final output.
  local response
  if nmap.verbosity() == 0 then
    response = "|_ " .. table.concat(names, ", ")
  else
    response = "\n" .. psl_print(psl, nmap.verbosity())
  end

  return response
end