Class: NetMap::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/netmap/cli.rb

Constant Summary collapse

<<-'EOF'.cyan
_   _      _   __  __             
   | \ | | ___| |_|  \/  | __ _ _ __  
   |  \| |/ _ \ __| |\/| |/ _` | '_ \ 
   | |\  |  __/ |_| |  | | (_| | |_) |
   |_| \_|\___|\__|_|  |_|\__,_| .__/ 
                           |_|    
  [ Network Scanner - By: IshikawaUta ]
EOF

Class Method Summary collapse

Class Method Details

.display_results(results, duration, active_hosts, scanner = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/netmap/cli.rb', line 155

def self.display_results(results, duration, active_hosts, scanner = nil)
  if active_hosts.empty?
    puts "\nTidak ada host aktif ditemukan.".red
  elsif results.empty? && active_hosts.any?
    # Jika pingscan saja (-sn), tampilkan tabel host yang ditemukan
    puts "\n" + "── HASIL PENEMUAN HOST (PING SCAN) ──".center(75, " ").cyan
    col_widths = { ip: 25, mac_vendor: 50 }
    
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:mac_vendor]+2)}"
    puts "#{'ALAMAT IP'.ljust(col_widths[:ip])}#{'MAC / VENDOR'.ljust(col_widths[:mac_vendor])}"
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:mac_vendor]+2)}"

    active_hosts.each do |ip|
      arp_info = scanner&.instance_variable_get(:@arp_table)&.[](ip)
      mac_vendor_text = if arp_info
        mac = arp_info[:mac]
        vendor = scanner.send(:get_vendor_by_mac, mac)
        "#{mac} (#{vendor})"
      else
        "N/A (Cek tabel ARP manual)"
      end
      
      puts "#{ip.ljust(col_widths[:ip]).green}#{mac_vendor_text.ljust(col_widths[:mac_vendor]).light_black}"
    end
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:mac_vendor]+2)}"
  else
    puts "\n"
    # Dimensi Kolom: IP(15), Port(10), Status(15), OS(15), MAC(30), Banner(45)
    col_widths = { ip: 15, port: 10, status: 15, os: 15, mac: 30, banner: 45 }
    
    # Header
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:port]+2)}#{'' * (col_widths[:status]+2)}#{'' * (col_widths[:os]+2)}#{'' * (col_widths[:mac]+2)}#{'' * (col_widths[:banner]+2)}"
    puts "#{'ALAMAT IP'.ljust(col_widths[:ip])}#{'PORT'.ljust(col_widths[:port])}#{'STATUS'.ljust(col_widths[:status])}#{'OS'.ljust(col_widths[:os])}#{'MAC / VENDOR'.ljust(col_widths[:mac])}#{'BANNER'.ljust(col_widths[:banner])}"
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:port]+2)}#{'' * (col_widths[:status]+2)}#{'' * (col_widths[:os]+2)}#{'' * (col_widths[:mac]+2)}#{'' * (col_widths[:banner]+2)}"

    results.each do |r|
      ip_val = r[:ip].ljust(col_widths[:ip]).green
      port_val = "#{r[:port]}/#{r[:type]}".ljust(col_widths[:port]).yellow
      status_color = r[:status] == :open ? :light_green : :yellow
      status_val = r[:status].to_s.ljust(col_widths[:status]).send(status_color)
      os_val = (r[:os] || "Unknown").ljust(col_widths[:os]).cyan
      
      mac_vendor = r[:mac] ? "#{r[:mac]} (#{r[:vendor]})" : "N/A (Local only)"
      mac_val = mac_vendor[0...(col_widths[:mac])].ljust(col_widths[:mac]).light_black
      
      banner_text = (r[:banner] || "").to_s.gsub(/[^[:print:]]/, ' ').strip
      banner_lines = wrap_text(banner_text, col_widths[:banner])
      
      # Baris pertama (dengan semua data)
      puts "#{ip_val}#{port_val}#{status_val}#{os_val}#{mac_val}#{banner_lines[0].ljust(col_widths[:banner]).italic}"
      
      # Baris tambahan jika banner dibungkus (wraped)
      if banner_lines.size > 1
        banner_lines[1..-1].each do |line|
          puts "#{' ' * col_widths[:ip]}#{' ' * col_widths[:port]}#{' ' * col_widths[:status]}#{' ' * col_widths[:os]}#{' ' * col_widths[:mac]}#{line.ljust(col_widths[:banner]).italic}"
        end
      end
    end
    puts "#{'' * (col_widths[:ip]+2)}#{'' * (col_widths[:port]+2)}#{'' * (col_widths[:status]+2)}#{'' * (col_widths[:os]+2)}#{'' * (col_widths[:mac]+2)}#{'' * (col_widths[:banner]+2)}"
  end
  
  puts "\nSelesai dalam #{duration} detik. Total host aktif: #{active_hosts.size}. Port terbuka: #{results.size}".cyan
end

.run_scan(targets, options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/netmap/cli.rb', line 118

def self.run_scan(targets, options)
  puts BANNER
  puts "\n" + "=".center(75, "=").yellow
  puts " MEMULAI PEMINDAIAN NETMAP ULTIMATE ".center(75, "=").yellow
  puts "=".center(75, "=").yellow
  puts " Waktu    : #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
  puts " Target   : #{targets.join(', ')}"
  puts " Mode     : #{options[:udp] ? 'UDP Scan' : 'TCP Scan'}"
  puts " OS Det   : #{options[:os_detect] ? 'Aktif' : 'Non-aktif'}"
  puts " Ping     : #{options[:noping] ? 'Dinonaktifkan (-Pn)' : 'Aktif'}"
  puts " Timing   : -T#{options[:timing]}"
  puts "=".center(75, "=").yellow + "\n"

  scanner = NetMap::Scanner.new(
    targets, 
    options[:ports], 
    threads: options[:threads], 
    timeout: options[:timeout],
    timing: options[:timing],
    discovery: options[:discovery],
    udp: options[:udp],
    os_detect: options[:os_detect],
    noping: options[:noping]
  )
  
  start_time = Time.now
  results = scanner.run
  duration = (Time.now - start_time).round(2)

  display_results(results, duration, scanner.active_hosts, scanner)
  
  save_results(results, options[:output]) if options[:output]
  save_json(results, options[:output_json]) if options[:output_json]
  save_grepable(results, options[:output_grep]) if options[:output_grep]
  save_html(results, options[:output_html]) if options[:output_html]
end

.save_grepable(results, filename) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/netmap/cli.rb', line 238

def self.save_grepable(results, filename)
  content = results.map do |r|
    "Host: #{r[:ip]}\tPorts: #{r[:port]}/#{r[:status]}/#{r[:type]}//#{r[:banner]}//\tOS: #{r[:os]}"
  end.join("\n")
  File.write(filename, content)
  puts "Hasil Grepable disimpan: #{filename}".green
end

.save_html(results, filename) ⇒ Object



246
247
248
249
250
# File 'lib/netmap/cli.rb', line 246

def self.save_html(results, filename)
  require_relative 'html_reporter'
  NetMap::HTMLReporter.generate(results, filename)
  puts "Laporan HTML disimpan: #{filename}".green
end

.save_json(results, filename) ⇒ Object



232
233
234
235
236
# File 'lib/netmap/cli.rb', line 232

def self.save_json(results, filename)
  require 'json'
  File.write(filename, JSON.pretty_generate(results))
  puts "Hasil JSON disimpan: #{filename}".green
end

.save_results(results, filename) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/netmap/cli.rb', line 252

def self.save_results(results, filename)
  File.open(filename, "w") do |f|
    f.puts "Hasil Pemindaian NetMap Ultimate - #{Time.now}"
    f.puts "=" * 50
    results.each { |r| f.puts "IP: #{r[:ip]} | Port: #{r[:port]}/#{r[:type]} | Status: #{r[:status]} | OS: #{r[:os]} | Banner: #{r[:banner]}" }
  end
  puts "Hasil Normal disimpan: #{filename}".green
end

.start(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/netmap/cli.rb', line 17

def self.start(args)
  options = {
    ports: "21,22,23,25,53,80,110,139,143,443,445,3000,3306,3389,5000,8080,8081,8096,8888,9000",
    threads: nil,
    timeout: nil,
    timing: 3,
    discovery: true,
    version_det: false,
    udp: false,
    os_detect: false,
    noping: false,
    output: nil,
    output_json: nil,
    output_grep: nil,
    output_html: nil
  }

  # Dukungan manual untuk -Pn dan -P (agar tidak bentrok dengan parser)
  if ARGV.include?('-Pn') || ARGV.include?('-P')
    options[:noping] = true
    ARGV.delete('-Pn')
    ARGV.delete('-P')
  end
  
  parser = OptionParser.new do |opts|
    opts.banner = "Penggunaan: netmap [opsi] <target>"
    opts.separator ""
    opts.separator "Opsi Pemindaian:"

    opts.on("-p", "--ports RANGE", "Rentang port (misal: 80,443 atau 1-1024)") do |v|
      options[:ports] = v
    end

    opts.on("-s TYPE", "Jenis Scan (U: UDP, V: Versi, n: Ping)") do |v|
      options[:udp] = true if v.include?('U')
      options[:version_det] = true if v.include?('V')
      options[:ports] = nil if v.include?('n')
    end

    opts.on("--udp", "Gunakan pemindaian UDP") { options[:udp] = true }
    opts.on("--version-all", "Gunakan deteksi versi") { options[:version_det] = true }
    opts.on("--ping", "Hanya host discovery") { options[:ports] = nil }
    opts.on("--noping", "Lewati host discovery, anggap host up") { options[:noping] = true }

    opts.on("-O", "--osscan", "Deteksi OS - Tebak sistem operasi target") do
      options[:os_detect] = true
    end

    opts.separator ""
    opts.separator "Opsi Kecepatan (Timing):"

    opts.on("-T", "--timing TEMPLATE", Integer, "Template waktu 0-5 (default: 3)") do |v|
      options[:timing] = v
    end

    opts.on("-t", "--threads JUMLAH", Integer, "Jumlah thread/concurrency") do |v|
      options[:threads] = v
    end

    opts.separator ""
    opts.separator "Opsi Output:"

    opts.on("-n", "--output-normal FILE", "Simpan hasil ke file teks (Normal)") do |v|
      options[:output] = v
    end

    opts.on("-j", "--json FILE", "Simpan hasil ke file JSON") do |v|
      options[:output_json] = v
    end

    opts.on("-g", "--grepable FILE", "Simpan hasil ke format Grepable") do |v|
      options[:output_grep] = v
    end

    opts.on("--html FILE", "Simpan hasil ke laporan HTML") do |v|
      options[:output_html] = v
    end

    opts.on("-v", "--version", "Tampilkan versi aplikasi") do
      puts BANNER
      puts " NetMap Ultimate Version: #{NetMap::VERSION}".yellow
      exit
    end

    opts.on("-h", "--help", "Tampilkan bantuan ini") do
      puts BANNER
      puts opts
      exit
    end
  end

  begin
    parser.parse!(args)
    exit 0 if args.empty? && (puts BANNER; puts parser; true)
    run_scan(args, options)
  rescue => e
    puts "Error: #{e.message}".red
    exit 1
  end
end

.wrap_text(text, width) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/netmap/cli.rb', line 219

def self.wrap_text(text, width)
  return [text] if text.size <= width
  lines = []
  while text.size > width
    # Cari spasi terdekat untuk pemotongan yang rapi
    chunk_end = text.rindex(' ', width) || width
    lines << text[0...chunk_end].strip
    text = text[chunk_end..-1].strip
  end
  lines << text unless text.empty?
  lines
end