CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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