Module: Mailpot::Mail

Defined in:
lib/mailpot/mail.rb

Class Method Summary collapse

Class Method Details

.add_message(message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mailpot/mail.rb', line 26

def add_message(message)
 if !@initialized
  initialize
 end
 msg = Hash.new
 msg[:source] = message[:source]
 msg[:sender] = message[:sender]
 msg[:source_ip] = message[:ip]
 msg[:recipients] = message[:recipients]
 msg[:probe] = detect_probe(message)
 # we must at this point detect probes because we need them to be sent
 encoded_message = Base64.encode64(msg.to_json)
 deflate_encoded_message = gzdeflate(encoded_message)
 digest = Digest::MD5.hexdigest(deflate_encoded_message)
 store_message(digest, deflate_encoded_message)
end

.detect_probe(msg) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/mailpot/mail.rb', line 43

def detect_probe(msg)
 config = Mailpot.get_config
 mail = Mail.new(msg[:source])
 # First rule we want to detect is when the ip of the honeypot is in the subject
 if mail.subject.include? config[:smtp_ip]
  return [true, forward_probe(msg)]
 end
 return [false, false]
end

.forward_probe(msg) ⇒ Object



53
54
55
56
57
# File 'lib/mailpot/mail.rb', line 53

def forward_probe(msg)
 Net::SMTP.start('localhost') do | smtp|
  smtp.send_message msg[:source], msg[:sender], msg[:recipients]
 end
end

.gzdeflate(s) ⇒ Object



68
69
70
# File 'lib/mailpot/mail.rb', line 68

def gzdeflate(s)
 Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(s, Zlib::FINISH)
end

.initializeObject

setup connections etc



16
17
18
19
20
21
22
23
24
# File 'lib/mailpot/mail.rb', line 16

def initialize
 config = Mailpot.get_config
 yml = YAML.load_file config[:key_file]
 @bucket = yml['bucket']
 @queue = yml['queue']
 @s3 = AWS::S3.new(yml)
 @sqs = AWS::SQS.new(yml)
 @initialized = true
end

.store_message(key, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/mailpot/mail.rb', line 59

def store_message(key, value)
 Thread.new {
  mail = @s3.buckets[@bucket].objects[key]
  mail.write(value)
  q = @sqs.queues[@queue]
  msg = q.send_message(key)
 }
end