summaryrefslogtreecommitdiffstats
path: root/scripts/nfs-ls.nse
blob: d6185a0a7a11e95183f85d8adc18299a25728782 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
local rpc = require "rpc"
local shortport = require "shortport"
local stdnse = require "stdnse"
local string = require "string"
local ls = require "ls"
local table = require "table"
local nmap = require "nmap"

description = [[
Attempts to get useful information about files from NFS exports.
The output is intended to resemble the output of <code>ls</code>.

The script starts by enumerating and mounting the remote NFS exports. After
that it performs an NFS GETATTR procedure call for each mounted point
in order to get its ACLs.
For each mounted directory the script will try to list its file entries
with their attributes.

Since the file attributes shown in the results are the result of
GETATTR, READDIRPLUS, and similar procedures, the attributes
are the attributes of the local filesystem.

These access permissions are shown only with NFSv3:
* Read:     Read data from file or read a directory.
* Lookup:   Look up a name in a directory
            (no meaning for non-directory objects).
* Modify:   Rewrite existing file data or modify existing
            directory entries.
* Extend:   Write new data or add directory entries.
* Delete:   Delete an existing directory entry.
* Execute:  Execute file (no meaning for a directory).

Recursive listing is not implemented.
]]

---
-- @usage
-- nmap -p 111 --script=nfs-ls <target>
-- nmap -sV --script=nfs-ls <target>
--
-- @args nfs-ls.time Specifies which one of the last mac times to use in
--       the files attributes output. Possible values are:
-- * <code>m</code>: last modification time (mtime)
-- * <code>a</code>: last access time (atime)
-- * <code>c</code>: last change time (ctime)
-- The default value is <code>m</code> (mtime).
-- @args nfs.version The NFS protocol version to use
--
-- @output
-- PORT    STATE SERVICE
-- 111/tcp open  rpcbind
-- | nfs-ls:
-- |   Volume /mnt/nfs/files
-- |   access: Read Lookup NoModify NoExtend NoDelete NoExecute
-- |   PERMISSION  UID   GID   SIZE     MODIFICATION TIME  FILENAME
-- |   drwxr-xr-x  1000  100   4096     2010-06-17 12:28   /mnt/nfs/files
-- |   drwxr--r--  1000  1002  4096     2010-05-14 12:58   sources
-- |   -rw-------  1000  1002  23606    2010-06-17 12:28   notes
-- |
-- |   Volume /home/storage/backup
-- |   access: Read Lookup Modify Extend Delete NoExecute
-- |   PERMISSION  UID   GID   SIZE     MODIFICATION TIME  FILENAME
-- |   drwxr-xr-x  1000  100   4096     2010-06-11 22:31   /home/storage/backup
-- |   -rw-r--r--  1000  1002  0        2010-06-10 08:34   filetest
-- |   drwx------  1000  100   16384    2010-02-05 17:05   lost+found
-- |   -rw-r--r--  0     0     5        2010-06-10 11:32   rootfile
-- |   lrwxrwxrwx  1000  1002  8        2010-06-10 08:34   symlink
-- |_
--
-- @xmloutput
-- <table key="volumes">
--   <table>
--     <elem key="volume">/mnt/nfs/files</elem>
--     <table key="files">
--       <table>
--         <elem key="permission">drwxr-xr-x</elem>
--         <elem key="uid">1000</elem>
--         <elem key="gid">100</elem>
--         <elem key="size">4096</elem>
--         <elem key="time">2010-06-11 22:31</elem>
--         <elem key="filename">/mnt/nfs/files</elem>
--       </table>
--       <table>
--         <elem key="permission">-rw-r-&#45;r-&#45;</elem>
--         <elem key="uid">1000</elem>
--         <elem key="gid">1002</elem>
--         <elem key="size">0</elem>
--         <elem key="time">2010-06-10 08:34</elem>
--         <elem key="filename">filetest</elem>
--       </table>
--       <table>
--         <elem key="permission">drwx-&#45;&#45;&#45;&#45;&#45;</elem>
--         <elem key="uid">0</elem>
--         <elem key="gid">0</elem>
--         <elem key="size">16384</elem>
--         <elem key="time">2010-02-05 17:05</elem>
--         <elem key="filename">lost+found</elem>
--       </table>
--       <table>
--         <elem key="permission">-rw-r-&#45;r-&#45;</elem>
--         <elem key="uid">0</elem>
--         <elem key="gid">0</elem>
--         <elem key="size">5</elem>
--         <elem key="time">2010-06-10 11:32</elem>
--         <elem key="filename">rootfile</elem>
--       </table>
--       <table>
--         <elem key="permission">lrwxrwxrwx</elem>
--         <elem key="uid">1000</elem>
--         <elem key="gid">1002</elem>
--         <elem key="size">8</elem>
--         <elem key="time">2010-06-10 08:34</elem>
--         <elem key="filename">symlink</elem>
--       </table>
--     </table>
--     <table key="info">
--       <elem>access: Read Lookup NoModify NoExtend NoDelete NoExecute</elem>
--     </table>
--   </table>
-- </table>
-- <table key="total">
--   <elem key="files">5</elem>
--   <elem key="bytes">20493</elem>
-- </table>

-- Created 05/28/2010 - v0.1 - combined nfs-dirlist and nfs-acls scripts
-- Revised 06/04/2010 - v0.2 - make NFS exports listing with their acls
--                             default action.
-- Revised 06/07/2010 - v0.3 - added mactimes output.
-- Revised 06/10/2010 - v0.4 - use the new library functions and list
--                             entries with their attributes.
-- Revised 06/11/2010 - v0.5 - make the mtime the default time to show.
-- Revised 06/12/2010 - v0.6 - reworked the output to use the tab
--                             library.
-- Revised 06/27/2010 - v0.7 - added NFSv3 ACCESS support.
-- Revised 06/28/2010 - v0.8 - added NFSv2 support.
--

author = {"Patrik Karlsson", "Djalal Harouni"}
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"discovery", "safe"}
dependencies = {"rpc-grind"}


portrule = shortport.port_or_service(111, "rpcbind", {"tcp", "udp"} )

hostrule = function(host)
  local mountport, nfsport
  if host.registry.nfs then
    mountport = host.registry.nfs.mountport
    nfsport = host.registry.nfs.nfsport
  else
    host.registry.nfs = {}
  end
  for _,proto in ipairs({"tcp","udp"}) do
    local port = nmap.get_ports(host, nil, proto, "open")
    while port do
      if port.version then
        if port.service == "mountd" then
          mountport = port
        elseif port.service == "nfs" then
          nfsport = port
        end
      end
      if mountport and nfsport then break end
      port = nmap.get_ports(host, port, proto, "open")
    end
    if mountport and nfsport then break end
  end
  -- Run when nfs and mount ports were scanned and their versions numbers known
  if not (nfsport and (host.registry.nfs.nfsver or nfsport.version.version)) then
    return false
  end
  if not (mountport and (host.registry.nfs.mountver or mountport.version.version)) then
    return false
  end
  if host.registry.nfs.nfsver == nil then
    local low, high = string.match(nfsport.version.version, "(%d)%-(%d)")
    if high == nil then
      high = tonumber(nfsport.version.version)
      if high == 4 then
        return false --Can't support version 4
      else
        host.registry.nfs.nfsver = high
      end
    else
      if high == "4" then
        host.registry.nfs.nfsver = 3
      else
        host.registry.nfs.nfsver = tonumber(low)
      end
    end
  end
  if host.registry.nfs.mountver == nil then
    local low, high = string.match(mountport.version.version, "(%d)%-(%d)")
    if high == nil then
      host.registry.nfs.mountver = tonumber(mountport.version.version)
    else
      host.registry.nfs.mountver = tonumber(high)
    end
  end
  host.registry.nfs.mountport = mountport
  host.registry.nfs.nfsport = nfsport
  return (mountport and nfsport)
end

local procedures = { }

local function table_attributes(nfs, mount, attr)
  local file = {}

  if attr.mode then
    file.type = rpc.Util.FtypeToChar(attr.mode)
    file.mode = rpc.Util.FpermToString(attr.mode)
    file.uid = tostring(attr.uid)
    file.gid = tostring(attr.gid)
    if nfs.human then
      file.size = rpc.Util.SizeToHuman(attr.size)
    else
      file.size = tostring(attr.size)
    end
    file.time = rpc.Util.TimeToString(attr[nfs.time].seconds)
  else
    file.type = '?'
    file.mode = '?????????'
    file.uid = '?'
    file.gid = '?'
    file.size = '?'
    file.time = '?'
  end
  file.filename = mount

  return file
end

local function table_dirlist(nfs, mount, dirlist)
  local ret, files, attrs = {}, {}, {}
  local idx = 1

  for _, v in ipairs(dirlist) do
    if ((0 < nfs.maxfiles) and (#files >= nfs.maxfiles)) then
      break
    end

    if v.attributes then
      table.insert(files, v.name)
      attrs[files[idx]] = table_attributes(nfs, v.name, v.attributes)
      idx = idx + 1
    else
      stdnse.debug1("ERROR attributes:  %s", v.name)
    end
  end

  table.sort(files)
  for _, v in pairs(files) do
    table.insert(ret, attrs[v])
  end

  return ret
end

-- Unmount the NFS file system and close the connections
local function unmount_nfs(mount, mnt_obj, nfs_obj)
  rpc.Helper.NfsClose(nfs_obj)
  rpc.Helper.UnmountPath(mnt_obj, mount)
end

local function nfs_ls(nfs, mount, output)
  local dirs, attr, acs = {}, {}, {}
  local nfsobj = rpc.NFS:new()
  local mnt_comm, nfs_comm, fhandle

  mnt_comm, fhandle = procedures.MountPath(nfs.host, mount)
  if mnt_comm == nil then
    ls.report_error(output, fhandle)
    return false
  end

  local nfs_comm, status = procedures.NfsOpen(nfs.host)
  if nfs_comm == nil then
    rpc.Helper.UnmountPath(mnt_comm, mount)
    ls.report_error(output, status)
    return false
  end

  -- check if NFS and Mount versions are compatible
  -- RPC library will check if the Mount and NFS versions are supported
  if (nfs_comm.version == 1) then
    unmount_nfs(mount, mnt_comm, nfs_comm)
    ls.report_error(output,
      string.format("NFS v%d not supported", nfs_comm.version))
    return false
  elseif ((nfs_comm.version == 2 and mnt_comm.version > 2) or
      (nfs_comm.version == 3 and mnt_comm.version ~= 3)) then
    unmount_nfs(mount, mnt_comm, nfs_comm)
    ls.report_error(output,
      string.format("versions mismatch, NFS v%d - Mount v%d",
      nfs_comm.version, mnt_comm.version))
    return false
  end

  status, attr = nfsobj:GetAttr(nfs_comm, fhandle)
  if not status then
    unmount_nfs(mount, mnt_comm, nfs_comm)
    ls.report_error(output, attr)
    return status
  end

  if nfs_comm.version == 3 then
    status, acs = nfsobj:Access(nfs_comm, fhandle, 0x0000003F)
    if status then
      acs.str = rpc.Util.format_access(acs.mask, nfs_comm.version)
      ls.report_info(output, string.format("access: %s", acs.str))
    end

    status, dirs = nfsobj:ReadDirPlus(nfs_comm, fhandle)
    if status then
      for _,v in ipairs(table_dirlist(nfs, mount, dirs.entries)) do
        ls.add_file(output, {v.type .. v.mode, v.uid, v.gid, v.size,
          v.time, v.filename})
      end
    end
  elseif nfs_comm.version == 2 then
    status, dirs = nfsobj:ReadDir(nfs_comm, fhandle)
    if status then
      local lookup = {}
      for _, v in ipairs(dirs.entries) do
        if ((0 < nfs.maxfiles) and (#lookup >= nfs.maxfiles)) then
          break
        end

        local f = {}
        status, f = nfsobj:LookUp(nfs_comm, fhandle, v.name)
        f.name = v.name
        table.insert(lookup, f)
      end

      for _, v in ipairs(table_dirlist(nfs, mount, lookup)) do
        ls.add_file(output, {v.type .. v.mode, v.uid, v.gid, v.size,
          v.time, v.filename})
      end
    end
  end

  unmount_nfs(mount, mnt_comm, nfs_comm)
  return status
end

local mainaction = function(host)
  local results, mounts, status = {}, {}
  local nfs_info =
  {
    host      = host,
    --recurs    = tonumber(nmap.registry.args['nfs-ls.recurs']) or 1,
  }
  local output = ls.new_listing()

  nfs_info.version, nfs_info.time = stdnse.get_script_args('nfs.version',
    'nfs-ls.time')
  nfs_info.maxfiles = ls.config('maxfiles')
  nfs_info.human = ls.config('human')

  if nfs_info.time == "a" or nfs_info.time == "A" then
    nfs_info.time = "atime"
  elseif nfs_info.time == "c" or nfs_info.time == "C" then
    nfs_info.time = "ctime"
  else
    nfs_info.time = "mtime"
  end

  status, mounts = procedures.ShowMounts(nfs_info.host)
  if not status or mounts == nil then
    if mounts then
      return stdnse.format_output(false, mounts)
    else
      return stdnse.format_output(false, "Mount error")
    end
  end

  for _, v in ipairs(mounts) do
    local err
    ls.new_vol(output, v.name, true)
    status = nfs_ls(nfs_info, v.name, output)
    ls.end_vol(output)
  end

  return ls.end_listing(output)
end

hostaction = function(host)
  procedures = {
    ShowMounts = function(ahost)
      local mnt_comm, status, result, mounts
      local mnt = rpc.Mount:new()
      mnt_comm = rpc.Comm:new('mountd', host.registry.nfs.mountver)
      status, result = mnt_comm:Connect(ahost, host.registry.nfs.mountport)
      if ( not(status) ) then
        stdnse.debug1("ShowMounts: %s", result)
        return false, result
      end
      status, mounts = mnt:Export(mnt_comm)
      mnt_comm:Disconnect()
      if ( not(status) ) then
        stdnse.debug1("ShowMounts: %s", mounts)
      end
      return status, mounts
    end,

    MountPath = function(ahost, path)
      local fhandle, status, err
      local mountd, mnt_comm
      local mnt = rpc.Mount:new()

      mnt_comm = rpc.Comm:new("mountd", host.registry.nfs.mountver)

      status, err = mnt_comm:Connect(host, host.registry.nfs.mountport)
      if not status then
        stdnse.debug1("MountPath: %s", err)
        return nil, err
      end

      status, fhandle = mnt:Mount(mnt_comm, path)
      if not status then
        mnt_comm:Disconnect()
        stdnse.debug1("MountPath: %s", fhandle)
        return nil, fhandle
      end

      return mnt_comm, fhandle
    end,

    NfsOpen = function(ahost)
      local nfs_comm, status, err

      nfs_comm = rpc.Comm:new('nfs', host.registry.nfs.nfsver)
      status, err = nfs_comm:Connect(host, host.registry.nfs.nfsport)
      if not status then
        stdnse.debug1("NfsOpen: %s", err)
        return nil, err
      end

      return nfs_comm, nil
    end,
  }
  return mainaction(host)
end

portaction = function(host, port)
  procedures = {
    ShowMounts = function(ahost)
      return rpc.Helper.ShowMounts(ahost, port)
    end,
    MountPath = function(ahost, path)
      return rpc.Helper.MountPath(ahost, port, path)
    end,
    NfsOpen = function(ahost)
      return rpc.Helper.NfsOpen(ahost, port)
    end,
  }
  return mainaction(host)
end

local ActionsTable = {
  -- portrule: use rpcbind service
  portrule = portaction,
  -- hostrule: Talk to services directly
  hostrule = hostaction
}

action = function(...) return ActionsTable[SCRIPT_TYPE](...) end