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

  1. description = [[
  2. Attempts to extract system information from an SNMP version 1 service.
  3. ]]
  4.  
  5. ---
  6. -- @output
  7. -- |  snmp-sysdescr: HP ETHERNET MULTI-ENVIRONMENT,ROM A.25.80,JETDIRECT,JD117,EEPROM V.28.22,CIDATE 08/09/2006
  8. -- |_   System uptime: 28 days, 17:18:59 (248153900 timeticks)
  9.  
  10. author = "Thomas Buchanan <tbuchanan@thecompassgrp.net>"
  11.  
  12. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  13.  
  14. categories = {"default", "discovery", "safe"}
  15.  
  16. require "shortport"
  17. require "snmp"
  18.  
  19. -- runs after snmp-brute.nse
  20. runlevel = 2
  21.  
  22. portrule = shortport.portnumber(161, "udp", {"open", "open|filtered"})
  23.  
  24. ---
  25. -- Sends SNMP packets to host and reads responses
  26. action = function(host, port)
  27.  
  28.            -- create the socket used for our connection
  29.     local socket = nmap.new_socket()
  30.     
  31.     -- set a reasonable timeout value
  32.     socket:set_timeout(5000)
  33.     
  34.     -- do some exception handling / cleanup
  35.     local catch = function()
  36.         socket:close()
  37.     end
  38.     
  39.     local try = nmap.new_try(catch)
  40.     
  41.     -- connect to the potential SNMP system
  42.     try(socket:connect(host.ip, port.number, "udp"))
  43.     
  44.     local payload
  45.       
  46.     -- build a SNMP v1 packet
  47.     -- copied from packet capture of snmpget exchange
  48.     -- get value: 1.3.6.1.2.1.1.1.0 (SNMPv2-MIB::sysDescr.0)
  49.     local options = {}
  50.     options.reqId = 28428 -- unnecessary?
  51.     payload = snmp.encode(snmp.buildPacket(snmp.buildGetRequest(options, "1.3.6.1.2.1.1.1.0")))
  52.  
  53.     try(socket:send(payload))
  54.     
  55.     local status
  56.     local response
  57.     
  58.     -- read in any response we might get
  59.     status, response = socket:receive_bytes(1)
  60.  
  61.     if (not status) or (response == "TIMEOUT") then 
  62.         return
  63.     end
  64.     
  65.     -- since we got something back, the port is definitely open
  66.     nmap.set_port_state(host, port, "open")
  67.     
  68.     local result
  69.     result = snmp.fetchFirst(response)
  70.     
  71.     -- build a SNMP v1 packet
  72.     -- copied from packet capture of snmpget exchange
  73.     -- get value: 1.3.6.1.2.1.1.3.0 (SNMPv2-MIB::sysUpTime.0)
  74.     local options = {}
  75.     options.reqId = 28428
  76.     payload = snmp.encode(snmp.buildPacket(snmp.buildGetRequest(options, "1.3.6.1.2.1.1.3.0")))
  77.     
  78.     try(socket:send(payload))
  79.     
  80.     -- read in any response we might get
  81.     status, response = socket:receive_bytes(1)
  82.  
  83.     if (not status) or (response == "TIMEOUT") then
  84.         return result
  85.     end
  86.     
  87.     try(socket:close())
  88.  
  89.     local uptime = snmp.fetchFirst(response)
  90.  
  91.     local days, hours, minutes, seconds, htime, mtime, stime
  92.     days = math.floor(uptime / 8640000)
  93.     htime = math.fmod(uptime, 8640000)
  94.     hours = math.floor(htime / 360000)
  95.     mtime = math.fmod(htime, 360000)
  96.     minutes = math.floor(mtime / 6000)
  97.     stime = math.fmod(mtime, 6000)
  98.     seconds = stime / 100
  99.     
  100.     local dayLabel
  101.     
  102.     if days == 1 then
  103.         dayLabel = "day"
  104.     else
  105.         dayLabel = "days"
  106.     end
  107.     
  108.     result = result .. "\n" .. string.format("  System uptime: %d %s, %d:%02d:%05.2f (%s timeticks)", days, dayLabel, hours, minutes, seconds, tostring(uptime))
  109.     
  110.     return result
  111. end
  112.  
  113.