Class: Dorothy::DorothyFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/dorothy2/BFM.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_arr) ⇒ DorothyFetcher

Source_arr is an array e.g.: [“webgui”, “localdir”=>“/Users/akira/Downloads/doroth2_1.9.3_mail/opt/bins/webgui”, “typeid”=>1]



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dorothy2/BFM.rb', line 23

def initialize(source_arr)
  ndownloaded = 0

  @added = Hash.new
  source = source_arr[1]      #source_arr[1] is a hash

  source["priority"] ||= 0
  source["profile"] ||= "default"


  case source["type"]

    when "ssh" then
      #file = "/opt/dionaea/var/dionaea/binaries/"
      #puts "Start to download malware"

      files = []

      begin
        Net::SSH.start(source["ip"], source["username"], :password => source["pass"], :port => source["port"]) do |ssh|
          ssh.scp.download!(source["remotedir"],source["localdir"], :recursive => true) do |ch, name, sent, total|
            unless files.include? "#{source["localdir"]}/" + File.basename(name)
              ndownloaded += 1
              files.push "#{source["localdir"]}/" + File.basename(name)
            end
            #		print "#{File.basename(name)}: #{sent}/#{total}\r"
            #		$stdout.flush
          end
          LOGGER.info "BFM", "#{ndownloaded} files downloaded"
        end

      rescue => e
        LOGGER.error "BFM", "An error occurred while downloading malwares from honeypot sensor: " + $!
        LOGGER.error "BFM", "Error: #{$!}, #{e.inspect}, #{e.backtrace}"
      end

      #DIRTY WORKAROUND for scp-ing only files without directory
      FileUtils.mv(Dir.glob(source["localdir"] + "/binaries/*"), source["localdir"])
      Dir.rmdir(source["localdir"] + "/binaries")


      begin
        unless DoroSettings.env[:testmode]
          Net::SSH.start(source["host"], source["user"], :password => source["pass"], :port => source["port"]) do |ssh|
            ssh.exec "mv #{source["remotedir"]}/* #{source["remotedir"]}/../analyzed "
          end
        end
      rescue
        LOGGER.error "BFM", "An error occurred while erasing parsed malwares in the honeypot sensor: " + $!
      end

      files.each do |f|
        begin
          @added = QueueManager.add(f, source_arr[0], source["profile"], source["priority"])
        rescue
          LOGGER.error "BFM", "Error while adding the bin to the queue, skipping."
          LOGGER.debug "BFM", $!
          next
        end
      end


    #Thanks to Salvatore Gervino who made the first PoC of this source-module: http://www.honeynet.it/wp-content/uploads/Mentored_Projects/salvatore_gervino-dorothy2_email.pdf
    when "mail" then

      @db = Insertdb.new

       = {:address=>source["host"],  :username=>source["username"],
                 :password=>source["password"], :port=>source["port"], :ssl=>source["enable_ssl"], :n_emails => source["n_emails"] , :delete_once_downloaded => source["delete_once_downloaded"]}


      mailer = Dorothy::Mailer.new()
      begin
        emails = mailer.get_emails

        emails.each do |email|

          LOGGER.debug "BFM", "Analyzing email: #{email.date} - #{email.from_addrs[0]} - #{email.subject} - #{email.to_addrs[0]}"


          unless email.attachments.empty?
            mail_id = @db.push_email_data(email)

            attachment_content_type = Mail::ContentTypeElement.new(email.attachments.first.content_type)
            #if the attachment is a forwarded email, treat the attachment as the original email
            if attachment_content_type.main_type == 'message'

              LOGGER.info "BFM", "Forwarded email from #{email.from.first} found"
              email = mailer.read_from_string email.attachments.first.body.decoded
              mail_id = @db.push_email_data(email, mail_id)

            end


            email.attachments.each do | attachment |
              LOGGER.info "BFM", "Attachment found: #{attachment.filename} "
              bin = source["localdir"] + "/" + Digest::MD5.hexdigest(attachment.body.decoded) + "_" + attachment.filename
              Util.write( bin, attachment.body.decoded)
              id = QueueManager.add(bin, source_arr[0],source["profile"], source["priority"], mail_id)
              @added.store(id,[bin, source["priority"], source["profile"], source_arr[0]])
            end
          end

        end  #end for
        @db.close
        LOGGER.debug "BFM", "Analyzing email: End "
      rescue => e
        LOGGER.error "BFM", "Error while adding the bin to the queue, skipping. #{$!}"
        LOGGER.debug "DB", e.backtrace
      end

    when "system" then
      empty = true
      Dir.foreach(source["localdir"]) do |file|
        bin = source["localdir"] + "/" + file
        next if File.directory?(bin)

        begin
          id = QueueManager.add(bin, source_arr[0], source["profile"], source["priority"])
          empty = false
          @added.store(id,[bin, source["priority"], source["profile"], source_arr[0]])

        rescue
          LOGGER.error "BFM", "Error while adding the bin to the queue, skipping."
          LOGGER.debug "BFM", $!
          next
        end

      end
      LOGGER.debug "BFM", "No binaries were found in the selected source" if empty
    else
      LOGGER.fatal "BFM", "Source type #{source["type"]} is not yet configured"
  end
end

Instance Attribute Details

#addedObject (readonly)

Returns the value of attribute added.



20
21
22
# File 'lib/dorothy2/BFM.rb', line 20

def added
  @added
end

Class Method Details

.loader(sources, daemon = false) ⇒ Object

Expects an Hash as input



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/dorothy2/BFM.rb', line 159

def self.loader(sources, daemon=false)

  infinite = true


  begin
    while infinite  #infinite loop
      sources.each do |sname|
        #skip if it is webgui
        next if sname.first == 'webgui'

        LOGGER.debug "BFM", "Start to fetch binaries from #{sname.first.yellow} @ #{sname[1]["localdir"]}"

        added = self.new(sname).added
        LOGGER.info "BFM", "#{added.size.to_s.yellow} binaries retreived from #{sname.first.yellow}"

        added.each do |b|
          LOGGER.debug "BFM", "#{b[0]}\t#{File.basename(b[1][0])}\t#{b[1][3]}"
        end

      end
      if daemon
        LOGGER.info "BFM", "SLEEPING 10"
        sleep DoroSettings.bfm[:sleeptime].to_i
      end
      infinite = daemon
    end

  rescue SignalException #, RuntimeError
    LOGGER.warn "BFM", "SIGINT".red + " Catched [1], exiting gracefully."
  end
end