Skip to content

Commit

Permalink
Gracefully exit, and show some nice banner
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Jan 6, 2011
1 parent 0660075 commit 8cacb9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
13 changes: 11 additions & 2 deletions bin/micetrap
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby -w
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'trollop' unless defined?(Trollop)
Expand Down Expand Up @@ -33,6 +33,11 @@ opts = Trollop::options do
Running it with sudo will allow you to use default, unsuspicious ports,
which may give you advantage at tricking a smart attacker.
If you don't want to use system ports, you can run micetrap without having
root privileges like this:
micetrap ftp --port 9999 (or whatever non-system port you like)
The available services are are:
#{SERVICES.join(', ')}
Expand All @@ -45,8 +50,12 @@ EOS
stop_on SERVICES
end

puts opts.inspect
service = ARGV.shift.to_sym
Trollop::die "You need to specify a service, which must be one of the following: #{SERVICES.join(', ')}\n\nMaybe you just feel a bit lost.." unless SERVICES.include?(service)

# Show a nice banner
ANSI = {:RESET=>"\e[0m", :BOLD=>"\e[1m", :UNDERLINE=>"\e[4m", :LGRAY=>"\e[0;37m", :GRAY=>"\e[1;30m", :RED=>"\e[31m", :GREEN=>"\e[32m", :YELLOW=>"\e[33m", :BLUE=>"\e[34m", :MAGENTA=>"\e[35m", :CYAN=>"\e[36m", :WHITE=>"\e[37m"}

puts "Starting #{ANSI[:BOLD]}Micetrap#{ANSI[:RESET]}..."
puts "Loading fake #{ANSI[:RED]}#{service}#{ANSI[:RESET]} server... (press Ctrl-C to exit)\n"
Micetrap::Server.new(opts.update(:service => service)).fire!
16 changes: 9 additions & 7 deletions lib/micetrap/logger.rb
Expand Up @@ -17,18 +17,20 @@ def file
end

def log_probe(line, remote_host, remote_port)
logged = "\n#{Time.now} Recorded a probe coming from #{remote_host}:#{remote_port} containing the following:\n\t\t#{line}"
puts "About to write there"
file.write logged
puts "wrote to #{file.inspect}"
content = line.strip.length > 0 ? line
: '(empty line)'
logged = "\n#{Time.now} Recorded a probe coming from #{remote_host}:#{remote_port} containing the following: #{content}"
File.open(@filename, 'a') do |f|
f.write logged
end
puts logged
end

def log_message(line)
logged = "\n#{Time.now} ::: #{line} :::"
puts "About to write there"
file.write logged
puts "wrote to #{file.inspect}"
File.open(@filename, 'a') do |f|
f.write logged
end
puts logged
end

Expand Down
6 changes: 3 additions & 3 deletions lib/micetrap/services/base.rb
Expand Up @@ -15,20 +15,20 @@ def fire port = nil
begin
server = TCPServer.open(port || default_ports.sample || 0)
rescue Errno::EACCES
puts "Seems that you are trying to use a system port, for which you need root privileges.\n\nRun micetrap with a custom port if you don't want to sudo!\n"
puts "Looks like you are trying to use a system port, for which you need root privileges.\nRun micetrap with another port if you don't want to sudo!\n"
exit(1)
end
@port = server.addr[1]
@addrs = server.addr[2..-1].uniq

logger.log_message "#{name} micetrap listening on #{@addrs.collect{|a|"#{a}:#{port}"}.join(' ')}"
logger.log_message "#{name} trap listening on #{@addrs.collect{|a|"#{a}:#{port}"}.join(' ')}"
listen(server)
end

def listen(server)
# Handle Ctrl-C to exit!
interrupted = false
trap("INT") { interrupted = true }
trap("INT") { puts "Gracefully exiting..."; exit(0) }

while not interrupted do
socket = server.accept
Expand Down

0 comments on commit 8cacb9c

Please sign in to comment.