Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Merge branch 'jinankjain-Bug74'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Claudius committed Aug 30, 2016
2 parents bf92ec7 + 7c2edb0 commit f7c14f9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
11 changes: 2 additions & 9 deletions lib/ssh_scan/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ def initialize(target, port, timeout = 3)
@target = target
@timeout = timeout

if @target.ip_addr?
@ip = @target
else
@ip = @target.resolve_fqdn()
end

@port = port
@client_banner = SSHScan::Constants::DEFAULT_CLIENT_BANNER
@server_banner = nil
Expand All @@ -24,7 +18,7 @@ def initialize(target, port, timeout = 3)

def connect()
begin
@sock = Socket.tcp(@ip, @port, connect_timeout: @timeout)
@sock = Socket.tcp(@target, @port, connect_timeout: @timeout)
rescue Errno::ETIMEDOUT => e
@error = SSHScan::Error::ConnectTimeout.new(e.message)
@sock = nil
Expand All @@ -42,8 +36,7 @@ def get_kex_result(kex_init_raw = @kex_init_raw)
# Common options for all cases
result = {}
result[:ssh_scan_version] = SSHScan::VERSION
result[:hostname] = @target.fqdn? ? @target : ""
result[:ip] = @ip
result[:ip] = @target
result[:port] = @port

if !@sock
Expand Down
31 changes: 27 additions & 4 deletions lib/ssh_scan/scan_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,34 @@ def scan_target(socket, opts)
target, port = socket.chomp.split(':')
policy = opts[:policy_file]
timeout = opts[:timeout]
result = []

client = SSHScan::Client.new(target, port, timeout)
client.connect()
result = client.get_kex_result()
return result if result.include?(:error)
if target.fqdn?
if target.resolve_fqdn_as_ipv6.nil?
client = SSHScan::Client.new(target.resolve_fqdn_as_ipv4.to_s, port, timeout)
client.connect()
result = client.get_kex_result()
result[:hostname] = target
return result if result.include?(:error)
else
client = SSHScan::Client.new(target.resolve_fqdn_as_ipv6.to_s, port, timeout)
client.connect()
result = client.get_kex_result()
if result.include?(:error)
client = SSHScan::Client.new(target.resolve_fqdn_as_ipv4.to_s, port, timeout)
client.connect()
result = client.get_kex_result()
result[:hostname] = target
return result if result.include?(:error)
end
end
else
client = SSHScan::Client.new(target, port, timeout)
client.connect()
result = client.get_kex_result()
result[:hostname] = ""
return result if result.include?(:error)
end

# Connect and get results (Net-SSH)
begin
Expand Down
17 changes: 17 additions & 0 deletions lib/string_ext.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ipaddr'
require 'resolv'

# Extend string to include some helpful stuff
class String
Expand All @@ -22,6 +23,22 @@ def ip_addr?
return true
end

def resolve_fqdn_as_ipv6
Resolv::DNS.open do |dns|
ress = dns.getresources self, Resolv::DNS::Resource::IN::AAAA
temp = ress.map { |r| r.address }
return temp[0]
end
end

def resolve_fqdn_as_ipv4
Resolv::DNS.open do |dns|
ress = dns.getresources self, Resolv::DNS::Resource::IN::A
temp = ress.map { |r| r.address }
return temp[0]
end
end

def resolve_fqdn
@fqdn ||= TCPSocket.gethostbyname(self)[3]
end
Expand Down

0 comments on commit f7c14f9

Please sign in to comment.