home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / scripts / smb-enum-processes.nse < prev    next >
Text File  |  2009-07-06  |  8KB  |  272 lines

  1. description = [[
  2. Pulls a list of processes from the remote server over SMB. This will determine
  3. all running processes, their process IDs, and their parent processes. It is done
  4. by querying the remote registry service, which is disabled by default on Vista; on 
  5. all other Windows versions, it requires Administrator privilges. 
  6.  
  7. Since this requires administrator privileges, it isn't especially useful for a 
  8. penetration tester, since they can effectively do the same thing with metasploit
  9. or other tools. It does, however, provide for a quick way to get process lists 
  10. for a bunch of systems at the same time. 
  11.  
  12. WARNING: I have experienced crashes in regsvc.exe while making registry calls 
  13. against a fully patched Windows 2000 system; I've fixed the issue that caused it,
  14. but there's no guarantee that it (or a similar vuln in the same code) won't show 
  15. up again. Since the process automatically restarts, it doesn't negatively impact 
  16. the system, besides showing a message box to the user. 
  17. ]]
  18.  
  19. ---
  20. -- @usage
  21. -- nmap --script smb-enum-processes.nse -p445 <host>
  22. -- sudo nmap -sU -sS --script smb-enum-processes.nse -p U:137,T:139 <host>
  23. --
  24. ---
  25. -- @output
  26. -- Host script results:
  27. -- |  smb-enum-processes:  
  28. -- |  -+-Idle(0)---System(8)---SMSS(140)-+-WINLOGON(160)-+-SERVICES(212)-+-spoolsv(432)
  29. -- |   |                                 |               |               +-mstask(536)
  30. -- |   |                                 |               |               +-WinMgmt(592)
  31. -- |   |                                 |               |               +-svchost(620)
  32. -- |   |                                 |               |               `-regsvc(1136)
  33. -- |   |                                 |               `-LSASS(224)
  34. -- |   |                                 `-CSRSS(164)
  35. -- |   +-Unknown(296)---explorer(344)-+-firefox(636)---WinRAR(736)
  36. -- |   |                              +-keyfinder(848)
  37. -- |   |                              `-CMD(956)
  38. -- |   +-Unknown(400)---IEXPLORE(1036)
  39. -- |_  `-Unknown(840)---DRWTSN32(1192)
  40. -- 
  41. -- --
  42. -- Host script results:
  43. -- |  smb-enum-processes:  
  44. -- |  Idle [0] (parent: 0, priority: 0, threads: 1, handles: 0)
  45. -- |  System [8] (parent: 0, priority: 8, threads: 34, handles: 190)
  46. -- |  smss [140] (parent: 8, priority: 11, threads: 6, handles: 33)
  47. -- |  winlogon [160] (parent: 140, priority: 13, threads: 14, handles: 335)
  48. -- |  csrss [164] (parent: 140, priority: 13, threads: 10, handles: 229)
  49. -- |  services [212] (parent: 160, priority: 9, threads: 33, handles: 462)
  50. -- |  lsass [224] (parent: 160, priority: 9, threads: 13, handles: 267)
  51. -- |  SPOOLSV [412] (parent: 212, priority: 8, threads: 10, handles: 95)
  52. -- |  svchost [448] (parent: 212, priority: 8, threads: 24, handles: 369)
  53. -- |  mstask [516] (parent: 212, priority: 8, threads: 6, handles: 89)
  54. -- |  VMwareService.e [572] (parent: 212, priority: 13, threads: 4, handles: 95)
  55. -- |  winmgmt [648] (parent: 212, priority: 8, threads: 3, handles: 89)
  56. -- |  cmd [700] (parent: 212, priority: 8, threads: 1, handles: 28)
  57. -- |  explorer [720] (parent: 620, priority: 8, threads: 10, handles: 239)
  58. -- |  VMwareUser [748] (parent: 720, priority: 8, threads: 1, handles: 30)
  59. -- |  VMwareTray [764] (parent: 720, priority: 8, threads: 1, handles: 30)
  60. -- |_ regsvc [868] (parent: 212, priority: 8, threads: 4, handles: 76)
  61. -----------------------------------------------------------------------
  62.  
  63. author = "Ron Bowes"
  64. copyright = "Ron Bowes"
  65. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  66. categories = {"discovery","intrusive"}
  67.  
  68. require "bin"
  69. require 'msrpc'
  70. require 'msrpcperformance'
  71. require 'smb'
  72. require 'stdnse'
  73.  
  74. -- Strings used to separate processes from one another.
  75. local separators = {
  76.     first    = "-+-";
  77.     last    = " `-";
  78.     middle    = " +-";
  79.     only     = "---";
  80. }
  81.  
  82. function psl_add (psl, ps)
  83.     -- Add process.
  84.     psl[ps.pid] = ps
  85.  
  86.     -- Add dummy parent if no real one exists.
  87.     if psl[ps.ppid] == nil then
  88.         psl[ps.ppid] = {
  89.             name    = 'Unknown';
  90.             pid    = ps.ppid;
  91.             ppid    = ps.ppid;
  92.         }
  93.     end
  94. end
  95.  
  96. function psl_mode (list, i)
  97.     local mode
  98.  
  99.     -- Decide connector for process.
  100.     if table.maxn(list) == 1 then
  101.         mode = "only"
  102.     elseif i == 1 then
  103.         mode = "first"
  104.     elseif i == table.maxn(list) then
  105.         mode = "last"
  106.     else
  107.         mode = "middle"
  108.     end
  109.  
  110.     return mode
  111. end
  112.  
  113. function psl_print (psl)
  114.     local result = ""
  115.  
  116.     -- Find how many root processes there are.
  117.     local roots = {}
  118.     for i,ps in pairs(psl) do
  119.         if psl[ps.ppid] == nil or ps.ppid == ps.pid then
  120.             table.insert(roots, i)
  121.         end
  122.     end
  123.     table.sort(roots)
  124.  
  125.     -- Create vertical sibling link.
  126.     local bars = {}
  127.     if table.maxn(roots) ~= 1 then
  128.         table.insert(bars, 2)
  129.     end
  130.  
  131.     -- Print out each root of the tree.
  132.     for i,root in ipairs(roots) do
  133.         local mode = psl_mode(roots, i)
  134.         result = result .. psl_tree(psl, root, 0, bars, mode)
  135.     end
  136.  
  137.     return result
  138. end
  139.  
  140. function psl_tree (psl, pid, column, bars, mode)
  141.     local ps = psl[pid]
  142.  
  143.     -- Delete vertical sibling link.
  144.     if mode == 'last' then
  145.         table.remove(bars)
  146.     end
  147.  
  148.     -- Print lead-in.
  149.     local prefix = ''
  150.     if mode == 'middle' or mode == 'last' then
  151.         prefix = '\n'
  152.  
  153.         local i = 1
  154.         for j = 1, column do
  155.             if table.maxn(bars) >= i and
  156.                 bars[i] == j then
  157.                 prefix = prefix .. '|'
  158.                 i = i + 1
  159.             else
  160.                 prefix = prefix .. ' '
  161.             end
  162.         end
  163.     end
  164.  
  165.     -- Format process itself.
  166.     output = separators[mode] .. ps.name .. '(' .. ps.pid .. ')'
  167.     column = column + #output
  168.     local result = prefix .. output
  169.  
  170.     -- Find process' children.
  171.     local children = {}
  172.     for child_pid,child in pairs(psl) do
  173.         if child_pid ~= pid and child.ppid == pid then
  174.             table.insert(children, child_pid)
  175.         end
  176.     end
  177.     table.sort(children)
  178.  
  179.     -- Create vertical sibling link between children.
  180.     if table.maxn(children) > 1 then
  181.         table.insert(bars, column + 2)
  182.     end
  183.  
  184.     -- Format process' children.
  185.     for i,pid in ipairs(children) do
  186.         local mode = psl_mode(children, i)
  187.         result = result .. psl_tree(psl, pid, column, bars, mode)
  188.     end
  189.  
  190.     return result
  191. end
  192.  
  193. hostrule = function(host)
  194.     return smb.get_port(host) ~= nil
  195. end
  196.  
  197. action = function(host)
  198.  
  199.     local status, result
  200.     local process
  201.     local response = " \n"
  202.  
  203.     -- Get the process list
  204.     status, result = msrpcperformance.get_performance_data(host, "230")
  205.     if(status == false) then
  206.         if(nmap.debugging() > 0) then
  207.             return "ERROR: " .. result
  208.         else
  209.             return nil
  210.         end
  211.     end
  212.  
  213.     -- Get the process table
  214.     process = result['Process']
  215.  
  216. --    for i, v in pairs(result['Processor']['_Total']) do
  217. --        io.write(string.format("i = %s\n", i))
  218. --    end
  219.  
  220.     -- Put the processes into an array, and sort them by process id
  221.     local names = {}
  222.     for i, v in pairs(process) do
  223.         if(i ~= '_Total') then
  224.             names[#names + 1] = i
  225.         end
  226.     end
  227.     table.sort(names, function (a, b) return process[a]['ID Process'] < process[b]['ID Process'] end)
  228.  
  229.     -- Put the processes into an array indexed by process id and with a value equal to the name (so we can look it up 
  230.     -- easily when we need to)
  231.     local process_id = {}
  232.     for i, v in pairs(process) do
  233.         process_id[v['ID Process']] = i
  234.     end
  235.  
  236.  
  237.     if(nmap.verbosity() == 1) then
  238.         local psl = {}
  239.         for i,name in ipairs(names) do
  240.             if(name ~= '_Total') then
  241.                 psl_add(psl, {
  242.                     name    = name;
  243.                     pid    = process[name]['ID Process'];
  244.                     ppid    = process[name]['Creating Process ID'];
  245.                 })
  246.             end
  247.         end
  248.         response = ' \n' .. psl_print(psl)
  249.     elseif(nmap.verbosity() > 1) then
  250.         for i = 1, #names, 1 do
  251.             local name = names[i]
  252.             if(name ~= '_Total') then
  253.                 local parent = process_id[process[name]['Creating Process ID']]
  254.                 if(parent == nil) then
  255.                     parent = "n/a"
  256.                 end
  257.  
  258. --                response = response .. string.format("%6d %24s (Parent: %24s, Priority: %4d, Threads: %4d, Handles: %4d)\n", process[name]['ID Process'], name, parent, process[name]['Priority Base'], process[name]['Thread Count'], process[name]['Handle Count'])
  259.  
  260.                 response = response .. string.format("%s [%d] (parent: %s, priority: %s, threads: %s, handles: %s)\n", name, process[name]['ID Process'], process[name]['Creating Process ID'], process[name]['Priority Base'], process[name]['Thread Count'], process[name]['Handle Count'])
  261.             end
  262.             
  263.         end
  264.     else
  265.         response = stdnse.strjoin(", ", names)
  266.     end
  267.  
  268.     return response
  269. end
  270.  
  271.  
  272.