Skip to content
Browse files

Gracefully exit, and show some nice banner

  • Loading branch information...
1 parent 0660075 commit 8cacb9cd899c07602d19b08476c423ddb670960c @txus committed Jan 6, 2011
Showing with 23 additions and 12 deletions.
  1. +11 −2 bin/micetrap
  2. +9 −7 lib/micetrap/logger.rb
  3. +3 −3 lib/micetrap/services/base.rb
View
13 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)
@@ -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(', ')}
@@ -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!
View
16 lib/micetrap/logger.rb
@@ -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
View
6 lib/micetrap/services/base.rb
@@ -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

0 comments on commit 8cacb9c

Please sign in to comment.
Something went wrong with that request. Please try again.