Module: Dorothy

Defined in:
lib/dorothy2/BFM.rb,
lib/dorothy2.rb,
lib/dorothy2/MAM.rb,
lib/dorothy2/vtotal.rb,
lib/dorothy2/do-init.rb,
lib/dorothy2/Settings.rb,
lib/dorothy2/do-utils.rb,
lib/dorothy2/do-logger.rb

Overview

Copyright © 2010-2013 marco riccardi. This file is part of Dorothy - www.honeynet.it/dorothy See the file ‘LICENSE’ for copying permission.

Defined Under Namespace

Modules: DoroConfig, DoroSettings, Ssh, Util Classes: DoroLogger, Doro_NAM, Doro_VSM, DorothyFetcher, Insertdb, Loadmalw, Vtotal

Instance Method Summary collapse

Instance Method Details

#analyze(bin, guestvm) ⇒ Object

ANALYZE THE SOURCE



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
157
158
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/dorothy2.rb', line 74

def analyze(bin, guestvm)

  #RESERVING AN ANALYSIS ID
  db = Insertdb.new
  anal_id = db.get_anal_id



  #source.each do |sname, sinfo|

  #Dir.chdir(sinfo[:dir])

  #set home vars
  sample_home = DoroSettings.env[:analysis_dir] + "/#{anal_id}"
  bin.dir_bin = "#{sample_home}/bin/"
  bin.dir_pcap = "#{sample_home}/pcap/"
  bin.dir_screens = "#{sample_home}/screens/"
  bin.dir_downloads = "#{sample_home}/downloads/"


  LOGGER.info "SANDBOX", "VM#{guestvm[0]} ".yellow + "[" + "#{anal_id}".red + "]" + " Analyzing binary #{bin.filename}"

  begin
    #crate dir structure in analisys home
    unless File.directory?(sample_home)
      LOGGER.info "VSM","VM#{guestvm[0]} ".yellow + "Creating DIRS"
      Dir.mkdir sample_home
      Dir.mkdir bin.dir_bin
      Dir.mkdir bin.dir_pcap
      Dir.mkdir bin.dir_screens
      Dir.mkdir bin.dir_downloads

      if VERBOSE
        LOGGER.debug "VSM", sample_home
        LOGGER.debug "VSM",bin.dir_bin
        LOGGER.debug "VSM",bin.dir_pcap
        LOGGER.debug "VSM",bin.dir_screens
      end

    else
      LOGGER.warn "SANDBOX","Malware #{bin.md5} sample_home already present, WTF!? Skipping.."
      #print "\n"
      return false
    end



    FileUtils.cp(bin.binpath,bin.dir_bin)  # mv?


    #Creating a new VSM object for managing the SandBox VM
    LOGGER.info "VSM","VM#{guestvm[0]} ".yellow + "Connecting to ESX Server #{DoroSettings.esx[:host]}"

    vsm = Doro_VSM::ESX.new(DoroSettings.esx[:host],DoroSettings.esx[:user],DoroSettings.esx[:pass],guestvm[1], guestvm[3], guestvm[4])

    #Copy File to VM
    r = 0

    begin
      vsm.check_internet
    rescue
      if r <= 2
        r = r+1
        LOGGER.warn "SANDBOX","VM#{guestvm[0]}".yellow + " GUESTOS Connection problem to Internet, retry n. #{r}/3"
        sleep 20
        retry
      end
      LOGGER.error "SANDBOX", "VM#{guestvm[0]}".yellow + " Guest system is not able to connect to internet"
      r = 0
      retry
    end



    LOGGER.info "VSM","VM#{guestvm[0]} ".yellow + "Copying #{bin.md5} to VM"

    filecontent = File.open(bin.binpath, "rb") { |byte| byte.read } #load filebinary
    vsm.copy_file("#{bin.md5}#{bin.extension}",filecontent)

    #Start Sniffer
    dumpname = bin.md5
    pid = @nam.start_sniffer(guestvm[2],DoroSettings.nam[:interface], dumpname, DoroSettings.nam[:pcaphome]) #dumpname = vmfile.pcap
    LOGGER.info "NAM","VM#{guestvm[0]} ".yellow + "Start sniffing module"
    LOGGER.debug "NAM","VM#{guestvm[0]} ".yellow + "Tcpdump instance #{pid} started" if VERBOSE

    sleep 5

    begin
      #Execute File into VM
      LOGGER.info "VSM","VM#{guestvm[0]} ".yellow + "Executing #{bin.md5} File into VM"

      guestpid = vsm.exec_file("#{bin.md5}#{bin.extension}")

      LOGGER.debug "VSM","VM#{guestvm[0]} ".yellow + "Program executed with PID #{guestpid}" if VERBOSE


      LOGGER.info "VSM","VM#{guestvm[0]}".yellow + " Sleeping #{DoroSettings.sandbox[:sleeptime]} seconds".yellow

      #wait n seconds

      (1..DoroSettings.sandbox[:sleeptime]).each do |i|
        @screenshot1 = vsm.screenshot if i == DoroSettings.sandbox[:screen1time]
        @screenshot2 = vsm.screenshot if i == DoroSettings.sandbox[:screen2time]
        #t = "."*i
        #print "VM#{guestvm[0]}Sleeping #{SLEEPTIME} seconds".yellow  + " #{t}\r"
        #print "VM#{guestvm[0]}Sleeping #{SLEEPTIME} seconds".yellow + " #{t}" + " [Done]\n".green if i == SLEEPTIME
        sleep 1
        $stdout.flush
      end



      #Stopt Sniffer
      LOGGER.info "NAM", "VM#{guestvm[0]} ".yellow + "Stopping sniffing module " + pid.to_s
      @nam.stop_sniffer(pid)

      #Stop/Revert VM
      LOGGER.info "VSM","VM#{guestvm[0]} ".yellow + "Reverting VM"
      vsm.revert_vm

      sleep 5

    rescue => e

      LOGGER.error "SANDBOX", "VM#{guestvm[0]} - An error occourred while executing the file into the vm:\n  #{$!}"

      LOGGER.debug "SANDBOX" , "#{$!}\n #{e.inspect} \n #{e.backtrace}" if VERBOSE

      LOGGER.warn "SANDBOX", "VM#{guestvm[0]} ".red + "[RECOVER] Stopping sniffing module ".yellow + pid.to_s
      @nam.stop_sniffer(pid)

      LOGGER.warn "SANDBOX", "VM#{guestvm[0]} ".red + "[RECOVER] Reverting VM".yellow
      vsm.revert_vm
      sleep 5

      LOGGER.warn "SANDBOX", "VM#{guestvm[0]} ".red + "[RECOVER] Recovering finished, skipping to next binaries".yellow
      FileUtils.rm_r(sample_home)
      return false

    end


    #Downloading PCAP
    LOGGER.info "NAM", "VM#{guestvm[0]} ".yellow + "Downloading #{dumpname}.pcap to #{bin.dir_pcap}"
    #t = DoroSettings.nam[:pcaphome] + "/" + dumpname + ".pcap"
    Ssh.download(DoroSettings.nam[:host], DoroSettings.nam[:user],DoroSettings.nam[:pass], DoroSettings.nam[:pcaphome] + "/" + dumpname + ".pcap", bin.dir_pcap)

    #Downloading Screenshots from esx
    LOGGER.info "NAM", "VM#{guestvm[0]} ".yellow + "Downloading Screenshots"
    Ssh.download(DoroSettings.esx[:host],DoroSettings.esx[:user], DoroSettings.esx[:pass], @screenshot1, bin.dir_screens)
    Ssh.download(DoroSettings.esx[:host],DoroSettings.esx[:user], DoroSettings.esx[:pass], @screenshot2, bin.dir_screens)

    #Put them to 644
    File.chmod(0644, bin.dir_screens + File.basename(@screenshot1), bin.dir_screens + File.basename(@screenshot2) )

    #####################
    #UPDATE DOROTHIBE DB#
    #####################

    pcapfile =  bin.dir_pcap + dumpname + ".pcap"
    dump = Loadmalw.new(pcapfile)

    #pcaprpath = bin.md5 + "/pcap/" + dump.filename
    pcaprid = Loadmalw.calc_pcaprid(dump.filename, dump.size)

    LOGGER.debug "NAM", "VM#{guestvm[0]} ".yellow + "Pcaprid: " + pcaprid if VERBOSE

    empty_pcap = false

    if dump.size <= 30
      LOGGER.warn "NAM", "VM#{guestvm[0]} WARNING - EMPTY PCAP FILE!!!! ::.."
      #FileUtils.rm_r(sample_home)
      empty_pcap = true
    end

    dumpvalues = [dump.sha, dump.size, pcaprid, dump.binpath, 'false']
    dump.sha = "EMPTYPCAP" if empty_pcap
    analysis_values = [anal_id, bin.sha, guestvm[0], dump.sha, get_time]

    if pcaprid.nil? || bin.dir_pcap.nil? || bin.sha.nil? || bin.md5.nil?
      LOGGER.error "SANDBOX", "VM#{guestvm[0]} Can't retrieve the required information"
      FileUtils.rm_r(sample_home)
      return false
    end


    LOGGER.debug "DB", "VM#{guestvm[0]} Database insert phase" if VERBOSE

    db = Insertdb.new
    db.begin_t  #needed for rollbacks

    unless empty_pcap
      unless db.insert("traffic_dumps", dumpvalues)
        LOGGER.fatal "DB", "VM#{guestvm[0]} Error while inserting data into table traffic_dumps. Skipping binary #{bin.md5}"
        FileUtils.rm_r(sample_home)
        return false
      end
    end



    unless db.insert("analyses", analysis_values)
      LOGGER.fatal "DB", "VM#{guestvm[0]} Error while inserting data into table analyses. Skipping binary #{bin.md5}"
      FileUtils.rm_r(sample_home)
      return false
    end

    #TODO ADD RT CODE

    db.commit
    db.close

    LOGGER.info "VSM", "VM#{guestvm[0]} ".yellow + "Removing file from /bins directory"
    FileUtils.rm(bin.binpath)
    LOGGER.info "VSM", "VM#{guestvm[0]} ".yellow + "Process compleated successfully"

  rescue => e

    LOGGER.error "SANDBOX", "VM#{guestvm[0]} An error occurred while analyzing #{bin.filename}, skipping\n"
    LOGGER.debug "Dorothy" , "#{$!}\n #{e.inspect} \n #{e.backtrace}" if VERBOSE

    FileUtils.rm_r(sample_home)
    db.rollback unless db.nil?  #rollback in case there is a transaction on going
    return false
  end





end

#check_support(bin) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dorothy2.rb', line 60

def check_support(bin)
  if bin.extension == ".exe" || bin.extension == ".bat"
    true
  else
    LOGGER.warn("SANDBOX", "File #{bin.filename} actually not supported, skipping\n" + "	Filtype: #{bin.type}") # if VERBOSE
    dir_not_supported = File.dirname(bin.binpath) + "/not_supported"
    Dir.mkdir(dir_not_supported) unless File.exists?(dir_not_supported)
    FileUtils.cp(bin.binpath,dir_not_supported) #mv?
    FileUtils.rm(bin.binpath) ## mv?
    return false
  end
end

#get_timeObject



39
40
41
42
# File 'lib/dorothy2.rb', line 39

def get_time
  time = Time.new
  time.utc.strftime("%Y-%m-%d %H:%M:%S")
end

#start_analysis(bins) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dorothy2.rb', line 45

def start_analysis(bins)
  bins.each do |bin|
    next unless check_support(bin)
    scan(bin) unless DoroSettings.env[:testmode]   #avoid to stress VT if we are just testing
    @analysis_threads << Thread.new(bin.filename){
      db = Insertdb.new
      sleep 30 while !(guestvm = db.find_vm)  #guestvm struct: array ["sandbox id", "sandbox name", "ipaddress", "user", "password"]
      analyze(bin, guestvm)
      db.free_vm(guestvm[0])
      db.close
    }
  end
end