Ruby实现端口扫描

安全
本文讲述了Ruby实现端口扫描的方法,希望对您有所帮助.

#!/usr/bin/ruby -w

=begin

quite simple connect scanner in ruby

Shows open (reliable), close and filtered. But the reliablity

of the last two states depends of course on firewalling and the timeout

interval you can set down. Uncomment or comment states you want or don't

want to get printed on the screen.

*supports scanning mutiple hosts/IPs (like www.heise.de,www.gulli.com)

*supports multiple ports(like 12,45,53,165),ranges(like 20..85) or one single

port(like 80)

*support a ports file like http://insecure.org/nmap/data/nmap-services (just

clean it with | grep /tcp ..)

ZGlnaXRhbGJ5dGU=

=end

require 'socket'

require 'timeout'

class Scanner

def initialize

@hosts,@ports = Array($*)

end

def portarrange

case @ports

when /^.+[..]/

@ports = @ports.split("..")

@ports = @ports[0].to_i..@ports[1].to_i

when /^.+[,]/

@ports = @ports.split(",")

else

@ports = Array(@ports)

end

end

def hostarrange

case @hosts

when /^.+[,]/

@hosts = @hosts.split(",")

else

@hosts = Array(@hosts)

end

end

def output(state,port)

printed = false

portsfile = File.new("ports", "r")

scanpat = "^.+ #{port}/tcp"

begin

portsfile.each_line do |line|

if line =~ Regexp.new(scanpat)

puts "#{state} : #{line}"

printed = true

end

end

puts "#{state} : #{port}" if printed == false

ensure

portsfile.close

end

end

def scanning(hosts,ports)

hosts.each do |host|

begin

puts "scanning #{host}:"

ports.each do |port|

begin

Timeout::timeout(10){TCPSocket.new(host, port)}

rescue Timeout::Error

output("filtered",port)

rescue

# output("closed",port)

else

output("open",port)

end

end

end

end

end

end

##################### code start #####################

puts "no arguments past,correct usage:\nruby #{$0} [hosts] [ports]\n" if

!ARGV[1]

my_scanner = Scanner.new

hosts = my_scanner.hostarrange

ports = my_scanner.portarrange

my_scanner.scanning(hosts,ports)

##################### eof #####################

【编辑推荐】

  1. SNIFFER嗅探器检测工具和对策
  2. 敏感信息检测系统--UnisSISS 操作探秘
  3. 无ARP欺骗的嗅探技术
责任编辑:赵宁宁 来源: hackbase
相关推荐

2022-06-15 13:40:38

端口扫描工具开源工具

2020-12-14 10:32:28

Web安全工具多线程

2024-06-19 19:07:53

2013-01-10 09:19:53

2010-09-17 09:40:16

2024-06-28 08:00:00

端口扫描安全

2009-01-15 09:52:00

2009-11-18 09:59:41

2023-10-16 18:51:04

Masscan网络安全

2010-09-13 15:55:43

制作端口扫描器

2021-05-31 08:54:30

RustScanRust端口扫描器

2010-12-22 21:57:00

Angry IP Sc

2023-04-04 12:24:10

2015-12-14 16:03:45

LinuxUnix端口扫描

2014-03-18 15:42:46

2010-09-15 15:12:49

2013-03-22 10:07:38

2021-01-10 08:14:01

Go语言TCP扫描器

2018-11-12 08:04:15

2015-04-24 11:03:53

点赞
收藏

51CTO技术栈公众号