makeiplist.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env ruby
  2. ##
  3. # This module requires Metasploit: https://metasploit.com/download
  4. # Current source: https://github.com/rapid7/metasploit-framework
  5. ##
  6. #
  7. # This script takes a list of ranges and converts it to a per line IP list.
  8. # Demonstration:
  9. # echo 192.168.100.0-50 >> rangelist.txt
  10. # echo 192.155-156.0.1 >> rangelist.txt
  11. # echo 192.168.200.0/25 >> rangelist.txt
  12. # ruby tools/recon/makeiplist.rb
  13. #
  14. # Author:
  15. # mubix
  16. #
  17. msfbase = __FILE__
  18. while File.symlink?(msfbase)
  19. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  20. end
  21. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
  22. require 'rex'
  23. require 'optparse'
  24. class OptsConsole
  25. def self.parse(args)
  26. options = {}
  27. opts = OptionParser.new do |opts|
  28. opts.banner = %Q|This script takes a list of ranges and converts it to a per line IP list.
  29. Usage: #{__FILE__} [options]|
  30. opts.separator ""
  31. opts.separator "Specific options:"
  32. opts.on("-i", '-i <filename>', "Input file") do |v|
  33. options['input'] = v.to_s
  34. end
  35. opts.on("-o", '-o <filename>', "(Optional) Output file. Default: iplist.txt") do |v|
  36. options['output'] = v.to_s
  37. end
  38. opts.separator ""
  39. opts.separator "Common options:"
  40. opts.on_tail("-h", "--help", "Show this message") do
  41. puts opts
  42. exit
  43. end
  44. end
  45. opts.parse!(args)
  46. if options.empty?
  47. puts "[*] No options specified, try -h for usage"
  48. exit
  49. end
  50. begin
  51. if options['input'] == nil
  52. puts opts
  53. raise OptionParser::MissingArgument, '-i is a required argument'
  54. end
  55. unless ::File.exist?(options['input'])
  56. raise OptionParser::InvalidArgument, "Not found: #{options['input']}"
  57. end
  58. if options['output'] == nil
  59. options['output'] = 'iplist.txt'
  60. end
  61. rescue OptionParser::InvalidOption
  62. puts "[*] Invalid option, try -h for usage"
  63. exit
  64. rescue OptionParser::InvalidArgument => e
  65. puts "[*] #{e.message}"
  66. exit
  67. end
  68. options
  69. end
  70. end
  71. #
  72. # Prints IPs
  73. #
  74. def make_list(in_f, out_f)
  75. in_f.each_line do |range|
  76. ips = Rex::Socket::RangeWalker.new(range)
  77. ips.each do |ip|
  78. out_f.puts ip
  79. end
  80. end
  81. end
  82. #
  83. # Returns file handles
  84. #
  85. def load_files(in_f, out_f)
  86. handle_in = ::File.open(in_f, 'r')
  87. # Output file not found, assuming we should create one automatically
  88. ::File.open(out_f, 'w') {} unless ::File.exist?(out_f)
  89. handle_out = ::File.open(out_f, 'a')
  90. return handle_in, handle_out
  91. end
  92. options = OptsConsole.parse(ARGV)
  93. in_f, out_f = load_files(options['input'], options['output'])
  94. begin
  95. puts "[*] Generating list at #{options['output']}"
  96. make_list(in_f, out_f)
  97. ensure
  98. # Always makes sure the file descriptors are closed
  99. in_f.close
  100. out_f.close
  101. end
  102. puts "[*] Done."