Skip to content

Commit

Permalink
Improved (a lot) target hostname resolution and coloring.
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Dec 23, 2016
1 parent 46c6890 commit 07251e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
24 changes: 24 additions & 0 deletions lib/bettercap/network/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ def get_hw_address( ctx, ip )
hw
end

def ip2name( address )
begin
names = Resolv.getnames(address)
hostname = names[0]
names.each do |name|
unless name.nil? or name.end_with?('.') or name.strip.empty?
hostname = name
end
end
unless hostname.empty?
return hostname
end
rescue; end

begin
hostname = Resolv.getname(address)
unless hostname.empty?
return hostname
end
rescue; end

address.to_s
end

private

# Start discovery agents and wait for +ctx.timeout+ seconds for them to
Expand Down
38 changes: 24 additions & 14 deletions lib/bettercap/network/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def to_s(padding=true)

# Return a compact string representation of this object.
def to_s_compact
return "#{@name}/#{@ip}" if @name
return "#{@name.light_blue}/#{@ip}" if @name
@ip
end

Expand All @@ -122,21 +122,31 @@ def self.normalized_mac(v)

# Attempt to perform a NBNS name resolution for this target.
def resolve!
resp, sock = nil, nil
begin
sock = UDPSocket.open
sock.send( NBNS_REQUEST, 0, @ip, NBNS_PORT )
resp = if select([sock], nil, nil, NBNS_TIMEOUT)
sock.recvfrom(NBNS_BUFSIZE)
hostname = Network.ip2name(@ip)
if hostname == @ip
resp, sock = nil, nil
begin
sock = UDPSocket.open
sock.send( NBNS_REQUEST, 0, @ip, NBNS_PORT )
resp = if select([sock], nil, nil, NBNS_TIMEOUT)
sock.recvfrom(NBNS_BUFSIZE)
end
if resp
@name = parse_nbns_response resp
Logger.info "Found NetBIOS name '#{@name}' for address #{@ip}"
end
rescue Exception => e
Logger.debug e
ensure
sock.close if sock
end
if resp
@name = parse_nbns_response resp
Logger.info "Found NetBIOS name '#{@name}' for address #{@ip}"
else
if hostname.include?('.')
@name = hostname.split('.')[0]
else
@name = hostname
end
rescue Exception => e
Logger.debug e
ensure
sock.close if sock
Logger.info "Found hostname #{@name.green} for address #{@ip}"
end
end

Expand Down
24 changes: 0 additions & 24 deletions lib/bettercap/sniffer/parsers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,6 @@ def match_port?( pkt )
false
end

def ip2name( address )
begin
names = Resolv.getnames(address)
hostname = names[0]
names.each do |name|
unless name.nil? or name.end_with?('.') or name.strip.empty?
hostname = name
end
end
unless hostname.empty?
return hostname
end
rescue; end

begin
hostname = Resolv.getname(address)
unless hostname.empty?
return hostname
end
rescue; end

address.to_s
end

# This method will be called from the BetterCap::Sniffer for each
# incoming packet ( +pkt ) and will apply the parser filter to it.
def on_packet( pkt )
Expand Down
2 changes: 1 addition & 1 deletion lib/bettercap/sniffer/parsers/https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def on_packet( pkt )
begin
if pkt.respond_to?(:tcp_dst) and pkt.tcp_dst == 443
Thread.new do
hostname = self.ip2name( pkt.ip_daddr )
hostname = BetterCap::Network.ip2name( pkt.ip_daddr )
if @@prev.nil? or @@prev != hostname
StreamLogger.log_raw( pkt, 'HTTPS', "https://#{hostname}/" )
@@prev = hostname
Expand Down

0 comments on commit 07251e1

Please sign in to comment.