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

  1. description = [[
  2. Gathers information from an IRC server.
  3.  
  4. It uses STATS, LUSERS, and other queries to obtain this information.
  5. ]]
  6.  
  7. ---
  8. -- @output
  9. -- 6665/tcp open     irc
  10. -- |  irc-info: Server: target.example.org
  11. -- |  Version: hyperion-1.0.2b(381). target.example.org
  12. -- |  Lservers/Lusers: 0/4204
  13. -- |  Uptime: 106 days, 2:46:30
  14. -- |  Source host: source.example.org
  15. -- |_ Source ident: OK n=nmap
  16.  
  17. author = "Doug Hoyte"
  18.  
  19. license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
  20.  
  21. categories = {"default", "discovery"}
  22.  
  23. require("stdnse")
  24. require "shortport"
  25.  
  26. portrule = shortport.port_or_service({6666,6667},"irc")
  27.  
  28. init = function()
  29.   -- Start of MOTD, we'll take the server name from here
  30.   nmap.registry.ircserverinfo_375 = nmap.registry.ircserverinfo_375
  31.     or pcre.new("^:([\\w-_.]+) 375", 0, "C")
  32.  
  33.   -- NICK already in use
  34.   nmap.registry.ircserverinfo_433 = nmap.registry.ircserverinfo_433
  35.     or pcre.new("^:[\\w-_.]+ 433", 0, "C")
  36.  
  37.   -- PING/PONG
  38.   nmap.registry.ircserverinfo_ping = nmap.registry.ircserverinfo_ping
  39.     or pcre.new("^PING :(.+)", 0, "C")
  40.  
  41.   -- Server version info
  42.   nmap.registry.ircserverinfo_351 = nmap.registry.ircserverinfo_351
  43.     or pcre.new("^:[\\w-_.]+ 351 \\w+ ([^:]+)", 0, "C")
  44.  
  45.   -- Various bits of info
  46.   nmap.registry.ircserverinfo_251_efnet = nmap.registry.ircserverinfo_251_efnet
  47.     or pcre.new("^:[\\w-_.]+ 251 \\w+ :There are (\\d+) users and (\\d+) invisible on (\\d+) servers", 0, "C")
  48.  
  49.   nmap.registry.ircserverinfo_251_ircnet = nmap.registry.ircserverinfo_251_ircnet
  50.     or pcre.new("^:[\\w-_.]+ 251 \\w+ :There are (\\d+) users and \\d+ services on (\\d+) servers", 0, "C")
  51.  
  52.   nmap.registry.ircserverinfo_252 = nmap.registry.ircserverinfo_252
  53.     or pcre.new("^:[\\w-_.]+ 252 \\w+ (\\d+) :", 0, "C")
  54.  
  55.   nmap.registry.ircserverinfo_254 = nmap.registry.ircserverinfo_254
  56.     or pcre.new("^:[\\w-_.]+ 254 \\w+ (\\d+) :", 0, "C")
  57.  
  58.   nmap.registry.ircserverinfo_255_efnet = nmap.registry.ircserverinfo_255_efnet
  59.     or pcre.new("^:[\\w-_.]+ 255 \\w+ :I have (\\d+) clients and (\\d+) server", 0, "C")
  60.  
  61.   nmap.registry.ircserverinfo_255_ircnet = nmap.registry.ircserverinfo_255_ircnet
  62.     or pcre.new("^:[\\w-_.]+ 255 \\w+ :I have (\\d+) users, \\d+ services and (\\d+) server", 0, "C")
  63.  
  64.   nmap.registry.ircserverinfo_242 = nmap.registry.ircserverinfo_242
  65.     or pcre.new("^:[\\w-_.]+ 242 \\w+ :Server Up (\\d+ days, [\\d:]+)", 0, "C")
  66.  
  67.   nmap.registry.ircserverinfo_352 = nmap.registry.ircserverinfo_352
  68.     or pcre.new("^:[\\w-_.]+ 352 \\w+ \\S+ (\\S+) ([\\w-_.]+)", 0, "C")
  69.  
  70.   nmap.registry.ircserverinfo_error = nmap.registry.ircserverinfo_error
  71.     or pcre.new("^ERROR :(.*)", 0, "C")
  72. end
  73.  
  74.  
  75.  
  76.  
  77.  
  78. action = function(host, port)
  79.   local sd = nmap.new_socket()
  80.   local curr_nick = random_nick()
  81.   local sver, shost, susers, sservers, schans, sircops, slusers, slservers, sup, serr
  82.   local myhost, myident
  83.   local s, e, t
  84.   local buf
  85.   local make_output = function()
  86.     local o = ""
  87.     if (not shost) then
  88.       if serr then
  89.         return "ERROR: " .. serr .. "\n"
  90.       else
  91.         return nil
  92.       end
  93.     end
  94.  
  95.     o = o .. "Server: " .. shost .. "\n"
  96.     if sver then
  97.       o = o .. "Version: " .. sver .. "\n"
  98.     end
  99.     if sircops and susers and sservers and schans then
  100.       o = o .. "Servers/Ops/Chans/Users: " .. sservers .. "/" .. sircops .. "/" .. schans .. "/" .. susers .. "\n"
  101.     end
  102.     if slusers and slservers then
  103.       o = o .. "Lservers/Lusers: " .. slservers .. "/" .. slusers .. "\n"
  104.     end
  105.     if sup then
  106.       o = o .. "Uptime: " .. sup .. "\n"
  107.     end
  108.     if myhost and myident then
  109.       o = o .. "Source host: " .. myhost .. "\n"
  110.       if string.find(myident, "^~") then
  111.         o = o .. "Source ident: NONE or BLOCKED\n"
  112.       else
  113.         o = o .. "Source ident: OK " .. myident .. "\n"
  114.       end
  115.     end
  116.  
  117.     return o
  118.   end
  119.  
  120.   init()
  121.  
  122.   sd:connect(host.ip, port.number)
  123.  
  124.   sd:send("USER nmap +iw nmap :Nmap Wuz Here\nNICK " .. curr_nick .. "\n")
  125.  
  126.   buf = stdnse.make_buffer(sd, "\r?\n")
  127.  
  128.   while true do
  129.     local line = buf()
  130.     if (not line) then break end
  131.  
  132.     -- This one lets us know we've connected, pre-PONGed, and got a NICK
  133.     s, e, t = nmap.registry.ircserverinfo_375:exec(line, 0, 0)
  134.     if (s) then
  135.       shost = string.sub(line, t[1], t[2])
  136.       sd:send("LUSERS\nVERSION\nSTATS u\nWHO " .. curr_nick .. "\nQUIT\n")
  137.     end
  138.  
  139.     s, e, t = nmap.registry.ircserverinfo_433:exec(line, 0, 0)
  140.     if (s) then
  141.       curr_nick = random_nick()
  142.       sd:send("NICK " .. curr_nick .. "\n")
  143.     end
  144.  
  145.     s, e, t = nmap.registry.ircserverinfo_ping:exec(line, 0, 0)
  146.     if (s) then
  147.       sd:send("PONG :" .. string.sub(line, t[1], t[2]) .. "\n")
  148.     end
  149.  
  150.     s, e, t = nmap.registry.ircserverinfo_351:exec(line, 0, 0)
  151.     if (s) then
  152.       sver = string.sub(line, t[1], t[2])
  153.     end
  154.  
  155.     s, e, t = nmap.registry.ircserverinfo_251_efnet:exec(line, 0, 0)
  156.     if (s) then
  157.       susers = (string.sub(line, t[1], t[2]) + string.sub(line, t[3], t[4]))
  158.       sservers = string.sub(line, t[5], t[6])
  159.     end
  160.  
  161.     s, e, t = nmap.registry.ircserverinfo_251_ircnet:exec(line, 0, 0)
  162.     if (s) then
  163.       susers = string.sub(line, t[1], t[2])
  164.       sservers = string.sub(line, t[3], t[4])
  165.     end
  166.  
  167.     s, e, t = nmap.registry.ircserverinfo_252:exec(line, 0, 0)
  168.     if (s) then
  169.       sircops = string.sub(line, t[1], t[2])
  170.     end
  171.  
  172.     s, e, t = nmap.registry.ircserverinfo_254:exec(line, 0, 0)
  173.     if (s) then
  174.       schans = string.sub(line, t[1], t[2])
  175.     end
  176.  
  177.     s, e, t = nmap.registry.ircserverinfo_255_efnet:exec(line, 0, 0)
  178.     if (s) then
  179.       slusers = string.sub(line, t[1], t[2])
  180.       slservers = string.sub(line, t[3], t[4])
  181.     end
  182.  
  183.     s, e, t = nmap.registry.ircserverinfo_255_ircnet:exec(line, 0, 0)
  184.     if (s) then
  185.       slusers = string.sub(line, t[1], t[2])
  186.       slservers = string.sub(line, t[3], t[4])
  187.     end
  188.  
  189.     s, e, t = nmap.registry.ircserverinfo_242:exec(line, 0, 0)
  190.     if (s) then
  191.       sup = string.sub(line, t[1], t[2])
  192.     end
  193.  
  194.     s, e, t = nmap.registry.ircserverinfo_352:exec(line, 0, 0)
  195.     if (s) then
  196.       myident = string.sub(line, t[1], t[2])
  197.       myhost = string.sub(line, t[3], t[4])
  198.     end
  199.  
  200.     s, e, t = nmap.registry.ircserverinfo_error:exec(line, 0, 0)
  201.     if (s) then
  202.       serr = string.sub(line, t[1], t[2])
  203.       return make_output()
  204.     end
  205.  
  206.   end
  207.  
  208.   return make_output()
  209.  
  210. end
  211.  
  212.  
  213.  
  214.  
  215. random_nick = function()
  216.   local nick = ""
  217.  
  218.   -- NICKLEN is at least 9
  219.   for i = 0, 8, 1 do
  220.     nick = nick .. string.char(math.random(97, 122)) -- lowercase ascii
  221.   end
  222.  
  223.   return nick
  224. end
  225.