port_scan.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require 'json'
  2. require 'nmap/program'
  3. require 'nmap/xml'
  4. # such a file can be found at
  5. # http://[316:c51a:62a3:8b9::2]/result.json
  6. # or
  7. # http://[300:7232:2b0e:d6e9:216:3eff:feb6:65a3]/nodeinfo.json
  8. filename = "yggnodes.json"
  9. threads_n = 16
  10. $ports = [80, 443, 8008, 8448]
  11. puts "Do a port scan on the yggdrasil network. Scan ports [#{$ports.join ', '}]. Reading nodes from #{filename}. Using #{threads_n} for scanning."
  12. puts "Read #{filename}"
  13. json = File.read 'yggnodes.json'
  14. puts "Parse json "
  15. data = JSON.parse json
  16. puts "Collecting addresses"
  17. addrs = []
  18. data['yggnodes'].each_value { |node|
  19. addrs << node['address']
  20. }
  21. #
  22. # yggnodes
  23. # public keys
  24. # address
  25. # coords
  26. # dht
  27. # nodeinfo
  28. # peers
  29. # time
  30. per_thread = addrs.length / threads_n
  31. rest = addrs.length % threads_n
  32. puts "Split addresses for threads"
  33. parts = []
  34. for i in 0...threads_n
  35. parts << addrs[i * per_thread ... (i+1) * per_thread]
  36. end
  37. rest_part = addrs[-rest..-1]
  38. threads = []
  39. def generate_code(number)
  40. charset = Array('A'..'Z') + Array('a'..'z')
  41. Array.new(number) { charset.sample }.join
  42. end
  43. def work_on_address addr, ident
  44. Nmap::Program.scan do |nmap|
  45. nmap.service_scan = true
  46. nmap.xml = "scan_#{ident}-#{generate_code(5)}.xml"
  47. nmap.ipv6 = true
  48. nmap.ports = $ports
  49. nmap.targets = addr
  50. end
  51. end
  52. threads << Thread.new {
  53. puts "Start extra thread"
  54. rest_part.each_with_index { |addr, index|
  55. work_on_address addr, "r_#{index}"
  56. }
  57. puts "Extra thread complete"
  58. }
  59. for i in 0...threads_n
  60. threads << Thread.new(i) { |num|
  61. puts "Start thread #{num + 1}"
  62. parts[num].each_with_index { |addr, index|
  63. work_on_address addr, "#{num}_#{index}"
  64. }
  65. puts "Stop thread #{num + 1}"
  66. }
  67. end
  68. puts "Waiting for threads to finish"
  69. threads.each { |th|
  70. th.join
  71. }