Ufak ufak temelleriyle başlayarak burada bir üst düzey port tarama uygulaması kodlayacağız beraber bu konu sürekli olarak güncellenecektir ve github hesabımda paylaşılacaktır herhangi bir programa eklencek bir şey, anlaşılmayan veya kötü anlattığım kısım olursa konu üzerinden iletişime geçebilirsiniz. Benimde bu süreçte öğrendiğim yeni şeyler olacaktır. Ukala mesajlar atmayınız HER ŞEY EĞİTİM AMAÇLIDIR, en azından Ruby dilinin söz dizimini (synatx) öğrenmiş olursunuz uzatmadan başlayalım.
1- İleride ve şu anda lazım olacak kütüphaneleri ekleyelim.
2- Fonksiyonu parsere ekleyince çıktı böyle olacaktır.
3- Kullanıcının girecek olduğu "-h" "-tcp" gibi hangi porttları taramak gibi isteklerini tanımlamış olduğumuz "options" sözlüğüne(hashine) alalım.
4- Argüman kontrolü
5- TCP port tarama fonksiyonumuzu ekleyip içine bağlantıları ve değişkenleri gönderelim.
8- UDP tarama fonksiyonu
7- Ana fonksiyonumuzu tanımlayalım ve içine kontrolleri koyalım.
Örnek kullanım:
Not: yakın zamanda UDP port tarama desteği, paralel threading ve hız desteği eklenecektir. Şuan amacım socket kullanımlarını anlatmak.
Yenilikler:
- 13.11.2023 tarihinde saat 16.13'de "Timeout" desteği eklenmiştir.
- 13.11.2023 tarihinde saat 19.12'de "Timeout" desteği ve kod düzeni iyileştirilip, threading ve udp desteği eklenmiştir.
- 14.11.2023 tarihinde saat 12.33'te "TCP" desteği güncellenmiştir ve hızı iyileştirilmiştir.
1- İleride ve şu anda lazım olacak kütüphaneleri ekleyelim.
Ruby:
def print_usage_text
puts "Usage: ruby your_script_name.rb [options]"
puts "\nOptions:"
puts " --help, -h\t\tPrints this help text"
puts " --tcp PORTS\t\tTCP ports input"
puts " --udp PORTS\t\tUDP ports input"
puts " --host HOST\t\tTarget host to port scan"
puts " --threads THREADS\tThreads to speed up the scan"
puts " --verbose, -v\t\tSet verbose output"
puts " --output FILE\t\tSet scan output file"
puts " --timeout TIMEOUT\tSet timeout"
exit(0)
end
2- Fonksiyonu parsere ekleyince çıktı böyle olacaktır.
3- Kullanıcının girecek olduğu "-h" "-tcp" gibi hangi porttları taramak gibi isteklerini tanımlamış olduğumuz "options" sözlüğüne(hashine) alalım.
Ruby:
$options = {
:timeout => 1 #Define default timeout = 1
}
OptionParser.new do |opts|
opts.on("--help", "-h", "Help parameter") do |help|
$options[:help] = help
end
opts.on("--tcp PORTS", "TCP ports input") do |tcp_ports_input|
$options[:tcp_ports_input] = tcp_ports_input
end
opts.on("--udp PORTS", "UDP ports input") do |udp_ports_input|
$options[:udp_ports_input] = udp_ports_input
end
opts.on("--host HOST", "Target host to port scan") do |target_host|
$options[:target_host] = target_host
end
opts.on("--threads THREADS", "Threads to speed for scan") do |threads_to_speed|
$options[:threads_to_speed] = threads_to_speed.to_i
end
opts.on("--verbose", "-v", "Set verbose output") do
$options[:verbose] = true
end
opts.on("--output FILE", "Set scan output file name") do |output|
$options[:output] = output
end
opts.on("--timeout TIMEOUT", "Set timeout") do |timeout|
$options[:timeout] = timeout.to_i
end
end.parse!
4- Argüman kontrolü
Ruby:
def validate_arguments
unless $options[:target_host]
puts "Error: Target host is required. Use --host HOST."
print_usage_text
end
end
5- TCP port tarama fonksiyonumuzu ekleyip içine bağlantıları ve değişkenleri gönderelim.
Ruby:
def tcp_scan(host, port, timeout_seconds)
$start_time = Time.now
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(port, host)
begin
socket.connect_nonblock(sockaddr)
puts "#{port} Open."
rescue IO::WaitWritable
IO.select(nil, [socket], nil, timeout_seconds)
retry
rescue Errno::EISCONN
puts "#{port}/TCP Open"
rescue Errno::ECONNREFUSED
puts "#{port}/TCP Closed (Error: Connection refused)."
rescue Errno::ETIMEDOUT
puts "#{port}/TCP Closed (Error: Connection timed out)."
rescue Errno::EHOSTUNREACH
puts "#{port}/TCP Closed (Error: Host unreachable)."
rescue Errno::ENETUNREACH
puts "#{port}/TCP Closed (Error: Network unreachable)."
rescue Errno::EINVAL
puts "#{port}/TCP Closed (Error: Invalid argument)."
rescue Exception => e
puts "#{port}/TCP Closed #{e.message}"
ensure
socket.close if socket
end
end
8- UDP tarama fonksiyonu
Ruby:
def udp_scan(host, port)
timeout_occurred = false
start_time = Time.now
begin
Timeout.timeout($options[:timeout]) do
udp_socket = UDPSocket.new
udp_socket.send("ping", 0, host, port)
response, _ = udp_socket.recvfrom(1)
puts "Port #{port}/UDP is open"
udp_socket.close
end
rescue Timeout::Error
elapsed_time = Time.now - start_time
if elapsed_time >= $options[:timeout]
timeout_occurred = true
puts "Port #{port}/UDP timed out (elapsed time: #{elapsed_time} seconds)"
else
raise MyCustomTimeoutError, "Timeout error (elapsed time: #{elapsed_time} seconds)"
end
rescue StandardError => e
puts "An error occurred: #{e.class} - #{e.message}"
ensure
return timeout_occurred
end
end
7- Ana fonksiyonumuzu tanımlayalım ve içine kontrolleri koyalım.
Ruby:
def main
validate_arguments
threads = []
if $options[:tcp_ports_input]
puts "Starting TCP scan on #{$options[:target_host]}..."
$options[:tcp_ports_input].split(',').each do |tcp_port|
threads << Thread.new { tcp_scan($options[:target_host], tcp_port.to_i) }
end
end
if $options[:udp_ports_input]
puts "Starting UDP scan on #{$options[:target_host]}..."
$options[:udp_ports_input].split(',').each do |udp_port|
threads << Thread.new { udp_scan($options[:target_host], udp_port.to_i) }
end
end
threads.each(&:join)
end
main
Örnek kullanım:
Not: yakın zamanda UDP port tarama desteği, paralel threading ve hız desteği eklenecektir. Şuan amacım socket kullanımlarını anlatmak.
Yenilikler:
- 13.11.2023 tarihinde saat 16.13'de "Timeout" desteği eklenmiştir.
- 13.11.2023 tarihinde saat 19.12'de "Timeout" desteği ve kod düzeni iyileştirilip, threading ve udp desteği eklenmiştir.
- 14.11.2023 tarihinde saat 12.33'te "TCP" desteği güncellenmiştir ve hızı iyileştirilmiştir.
Son düzenleme: