Permalink
Browse files

Started base service

  • Loading branch information...
1 parent b1de0ee commit bd2a0de76b30530e647ee9d85b23c7ec25891fcc @txus committed Jan 4, 2011
View
1 lib/micetrap.rb
@@ -1,4 +1,5 @@
require 'micetrap/services/exceptions'
+require 'micetrap/services/base'
require 'micetrap/logger'
require 'micetrap/server'
View
8 lib/micetrap/logger.rb
@@ -16,8 +16,12 @@ def file
@file ||= File.open(@filename, 'w')
end
- def log(line, remote_host, remote_port)
- file.write "\n#{Time.now} #{@service_name.upcase} fake server recorded probes coming from #{remote_host}:#{remote_port} containing the following:\n\t\t#{line}"
+ def log_probe(line, addr, remote_host, remote_port)
+ file.write "\n#{Time.now} Recorded a probe coming from #{remote_host} - #{addr}:#{remote_port} containing the following:\n\t\t#{line}"
+ end
+
+ def log_message(line)
+ file.write "\n#{Time.now} ::: #{line} :::"
end
end
View
6 lib/micetrap/server.rb
@@ -4,13 +4,11 @@ module Micetrap
class Server
attr_reader :service
attr_reader :port
- attr_reader :logger
def initialize(options)
raise StandardError.new("Service cannot be empty!") unless options[:service]
@service =
eval("Micetrap::Services::#{options[:service].to_s.capitalize}").new
- @logger = Logger.new
@port = options[:port] # Optional
rescue NameError
raise Services::UnrecognizedServiceException.new("Service #{options[:service].to_s.capitalize} is not recognized")
@@ -20,9 +18,5 @@ def fire!
@service.fire port
end
- def log(*args)
- @logger.log(*args)
- end
-
end
end
View
65 lib/micetrap/services/base.rb
@@ -0,0 +1,65 @@
+module Micetrap
+ module Services
+ class Base
+
+ class ClientQuitError < RuntimeError; end
+
+ attr_reader :logger
+
+ def initialize
+ @logger = Logger.new self.class.name.downcase.to_sym
+ end
+
+ def fire port = nil
+ server = TCPServer.open(port || default_ports.sample || 0)
+ @port = server.addr[1]
+ @addrs = server.addr[2..-1].uniq
+
+ logger.log_message "#{@name} micetrap listening on #{@addrs.collect{|a|"#{a}:#{port}"}.join(' ')}"
+
+ # Handle Ctrl-C to exit!
+ interrupted = false
+ trap("INT") { interrupted = true }
+
+ while not interrupted do
+ Thread.start do
+ read_from(server.accept)
+ end
+ end
+ end
+
+ def read_from(socket)
+ s = socket
+
+ port = s.peeraddr[1]
+ name = s.peeraddr[2]
+ addr = s.peeraddr[3]
+
+ begin
+ while line = s.gets # read a line at a time
+ raise ClientQuitError if line =~ /^die\r?$/
+
+ logger.log_probe line, addr, name, port
+
+ if line.strip == ""
+ s.write response
+ logger.log_message "Responded misleadingly: let's drive those hackers nuts!"
+ s.close
+ exit(0)
+ end
+ end
+ rescue ClientQuitError
+ logger.log_message "#{name}:#{port} disconnected"
+ ensure
+ s.close # close socket on error
+ end
+ end
+
+ protected
+
+ def default_ports; []; end;
+ def response; ""; end;
+
+ end
+ end
+end
View
22 spec/micetrap/logger_spec.rb
@@ -25,19 +25,35 @@ module Micetrap
end
end
- describe "#log" do
+ describe "#log_probe" do
it 'logs a scanned service along with the scanner info' do
file = double('file')
now = Time.now
Time.stub(:now).and_return now
File.stub(:open).and_return file
- expected_string = "\n#{time} FTP fake server recorded probes coming from hackerz.com:5978 containing the following:\n\t\t###EVILPROBE###"
+ expected_string = "\n#{now} Recorded a probe coming from hackerz.com - 234.43.14.35:5978 containing the following:\n\t\t###EVILPROBE###"
subject.file.should_receive(:write)
.with expected_string
- subject.log "###EVILPROBE###", "hackerz.com", 5978
+ subject.log_probe "###EVILPROBE###", "234.43.14.35", "hackerz.com", 5978
+ end
+ end
+
+ describe "#log_message" do
+ it 'logs a misc messagescanned service along with the scanner info' do
+ file = double('file')
+ now = Time.now
+ Time.stub(:now).and_return now
+ File.stub(:open).and_return file
+
+ expected_string = "\n#{now} ::: Warning! :::"
+
+ subject.file.should_receive(:write)
+ .with expected_string
+
+ subject.log_message "Warning!"
end
end
end
View
15 spec/micetrap/server_spec.rb
@@ -18,10 +18,6 @@ class Test
:port => 80
server.port.should be(80)
end
- it 'assigns a new logger' do
- server = Server.new :service => :test
- server.logger.should be_a(Logger)
- end
context 'when given an invalid service' do
it 'raises' do
@@ -51,16 +47,5 @@ class Test
end
end
- describe "#log" do
- it 'delegates to its logger' do
- test_service = double('service')
- my_args = [1,2,3]
- Services::Test.should_receive(:new).and_return test_service
- server = Server.new :service => :test
- server.logger.should_receive(:log).with(*my_args)
-
- server.log(*my_args)
- end
- end
end
end
View
38 spec/micetrap/services/base_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+module Micetrap
+ module Services
+ describe Base do
+ describe "#fire" do
+ context 'when given a port' do
+ it 'fires up the service on that port' do
+
+ end
+ end
+ context 'when port is nil' do
+ it 'fires up the service on one of the default ports' do
+
+ end
+ context 'but when no default ports are specified' do
+ it 'uses the port 0' do
+
+ end
+ end
+ end
+ it 'logs a message telling the trap is listening' do
+
+ end
+ it 'calls read_from every time a connection is accepted' do
+ connection = double('connection')
+ server = double('server', :addr => [1,2,3], :accept => connection)
+ TCPServer.should_receive(:open).and_return server
+ subject.logger.stub(:log)
+
+ subject.should_receive(:read_from).with(connection).any_number_of_times
+ puts "Press Ctrl-C to resume specs! Don't worry, it's all under control :)"
+ subject.fire
+ end
+ end
+ end
+ end
+end

0 comments on commit bd2a0de

Please sign in to comment.