Skip to content
This repository
  • 5 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
1  docs/AUTHORS
@@ -17,6 +17,7 @@ CONTRIBUTORS:
17 17
 Thorsten Sick
18 18
 Adam Pridgen
19 19
 Mike Tu
  20
+Loic Jaquemet
20 21
 
21 22
 BUG REPORTERS/ADVISORS:
22 23
 Giacomo Milani
2  lib/cuckoo/core/guest.py
@@ -103,7 +103,7 @@ def start_analysis(self, options):
103 103
     
104 104
             self.server.add_malware(data, options["file_name"])
105 105
             self.server.execute()
106  
-        except socket.timeout:
  106
+        except (socket.timeout, socket.error):
107 107
             raise CuckooGuestError("%s: guest communication timeout, check networking or try to increase timeout" % self.id)
108 108
 
109 109
     def wait_for_completion(self):
35  lib/cuckoo/core/scheduler.py
@@ -24,6 +24,8 @@
24 24
 
25 25
 mmanager = None
26 26
 machine_lock = Lock()
  27
+db_lock = Lock()
  28
+
27 29
 
28 30
 class AnalysisManager(Thread):
29 31
     """Analysis manager thread."""
@@ -158,11 +160,13 @@ def launch_analysis(self):
158 160
                     os.remove(self.task.file_path)
159 161
                 except OSError as e:
160 162
                     log.error("Unable to delete original file at path \"%s\": %s" % (self.task.file_path, e))
161  
-
162  
-            # Stop machine
163  
-            mmanager.stop(vm.label)
164  
-            # Release the machine from lock
165  
-            mmanager.release(vm.label)
  163
+            try:
  164
+                # Stop machine
  165
+                mmanager.stop(vm.label)
  166
+                # Release the machine from lock
  167
+                mmanager.release(vm.label)
  168
+            except CuckooMachineError as e:
  169
+                log.error("Unable to release vm %s, reason %s. You have to fix it manually" % (vm.label, e))
166 170
 
167 171
         # Check analysis file size to avoid memory leaks.
168 172
         try:
@@ -182,9 +186,6 @@ def run(self):
182 186
         """Run manager thread."""
183 187
         success = True
184 188
 
185  
-        db = Database()
186  
-        db.lock(self.task.id)
187  
-
188 189
         try:
189 190
             self.launch_analysis()
190 191
         except CuckooMachineError as e:
@@ -194,7 +195,7 @@ def run(self):
194 195
             log.error(e)
195 196
             success = False
196 197
         finally:
197  
-            db.complete(self.task.id, success)
  198
+            Database().complete(self.task.id, success)
198 199
 
199 200
 class Scheduler:
200 201
     """Task scheduler."""
@@ -252,13 +253,17 @@ def start(self):
252 253
             if mmanager.availables() == 0:
253 254
                 #log.debug("No machines available, try again")
254 255
                 continue
255  
-
  256
+            db_lock.acquire()
256 257
             task = self.db.fetch()
257 258
 
258  
-            if not task:
  259
+            if task:
  260
+                self.db.lock(task.id)
  261
+                db_lock.release()
  262
+                # Go with analysis.
  263
+                analysis = AnalysisManager(task)
  264
+                analysis.daemon = True
  265
+                analysis.start()
  266
+            else:
259 267
                 #log.debug("No pending tasks, try again")
260  
-                continue
  268
+                db_lock.release()
261 269
 
262  
-            analysis = AnalysisManager(task)
263  
-            analysis.daemon = True
264  
-            analysis.start()
42  modules/machinemanagers/virtualbox.py
@@ -22,6 +22,8 @@ class VirtualBox(MachineManager):
22 22
     SAVED = "saved"
23 23
     RUNNING = "running"
24 24
     POWEROFF = "poweroff"
  25
+    ABORTED = "aborted"
  26
+    ERROR = "machete"
25 27
 
26 28
     def _initialize_check(self):
27 29
         """Runs all checks when a machine manager is initialized.
@@ -73,14 +75,19 @@ def stop(self, label):
73 75
         @raise CuckooMachineError: if unable to stop.
74 76
         """
75 77
         log.debug("Stopping vm %s" % label)
76  
-        try:
77  
-            if subprocess.call([self.options.virtualbox.path, "controlvm", label, "poweroff"],
78  
-                               stdout=subprocess.PIPE,
79  
-                               stderr=subprocess.PIPE):
80  
-                raise CuckooMachineError("VBoxManage exited with error powering off the machine")
81  
-        except OSError as e:
82  
-            raise CuckooMachineError("VBoxManage failed powering off the machine: %s" % e)
83  
-        self._wait_status(label, self.POWEROFF)
  78
+
  79
+        status = self._status(label)
  80
+        if status == self.POWEROFF or status == self.ABORTED:
  81
+            log.debug("Trying to stop an already stopped vm %s" % label)
  82
+        else:
  83
+            try:
  84
+                if subprocess.call([self.options.virtualbox.path, "controlvm", label, "poweroff"],
  85
+                                   stdout=subprocess.PIPE,
  86
+                                   stderr=subprocess.PIPE):
  87
+                    log.debug("VBoxManage exited with error powering off the machine")
  88
+            except OSError as e:
  89
+                raise CuckooMachineError("VBoxManage failed powering off the machine: %s" % e)
  90
+            self._wait_status(label, self.POWEROFF)
84 91
 
85 92
     def _list(self):
86 93
         """Lists virtual machines installed.
@@ -111,7 +118,6 @@ def _status(self, label):
111 118
         """Gets current status of a vm.
112 119
         @param label: virtual machine name.
113 120
         @return: status string.
114  
-        @raise CuckooMachineError: if unable to enumerate status.
115 121
         """
116 122
         log.debug("Getting status for %s"% label)
117 123
         try:
@@ -124,31 +130,39 @@ def _status(self, label):
124 130
             output, err = proc.communicate()
125 131
 
126 132
             if proc.returncode != 0:
127  
-              raise CuckooMachineError("VBoxManage returns error checking status for machine %s: %s"
  133
+                # It's quite common for virtualbox crap utility to exit with:
  134
+                # VBoxManage: error: Details: code E_ACCESSDENIED (0x80070005)
  135
+                # So we just log to debug this.
  136
+                log.debug("VBoxManage returns error checking status for machine %s: %s"
128 137
                                        % (label, err))
  138
+                return self.ERROR
129 139
         except OSError as e:
130  
-            raise CuckooMachineError("VBoxManage failed to check status for machine %s: %s"
  140
+            log.warning("VBoxManage failed to check status for machine %s: %s"
131 141
                                      % (label, e))
  142
+            return self.ERROR
132 143
 
133 144
         for line in output.split("\n"):
134 145
             state = re.match(r"VMState=\"(\w+)\"", line, re.M|re.I)
135 146
             if state:
136 147
                 status = state.group(1)
137 148
                 log.debug("Machine %s status %s" % (label, status))
138  
-                return status
  149
+                return status.lower()
139 150
         raise CuckooMachineError("Unable to get status for %s" % label)
140 151
 
141 152
     def _wait_status(self, label, state):
142 153
         """Waits for a vm status.
143 154
         @param label: virtual machine name.
144  
-        @param state: virutal machine status.
  155
+        @param state: virtual machine status.
145 156
         @raise CuckooMachineError: if default waiting timeout expire.
146 157
         """
  158
+        # This block was originally suggested by Loic Jaquemet.
147 159
         waitme = 0
148  
-        while state != self._status(label):
  160
+        current = self._status(label)
  161
+        while state != current and current != self.ERROR:
149 162
             log.debug("Waiting %i cuckooseconds for vm %s to switch to status %s" % (waitme, label, state))
150 163
             if waitme > int(self.options.virtualbox.timeout):
151 164
                 self.stop(label)
152 165
                 raise CuckooMachineError("Waiting too much for vm %s status change. Stopping vm and aborting" % label)
153 166
             time.sleep(1)
154 167
             waitme += 1
  168
+            current = self._status(label)
18  modules/machinemanagers/vmware.py
... ...
@@ -1,17 +1,7 @@
1  
-# Copyright (C) 2012 Mike Tu (@mt00at)
2  
-#
3  
-# This program is free software: you can redistribute it and/or modify
4  
-# it under the terms of the GNU General Public License as published by
5  
-# the Free Software Foundation, either version 3 of the License, or
6  
-# (at your option) any later version.
7  
-#
8  
-# This program is distributed in the hope that it will be useful,
9  
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
10  
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  
-# GNU General Public License for more details.
12  
-#
13  
-# You should have received a copy of the GNU General Public License
14  
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
  1
+# Copyright (C) 2010-2012 Cuckoo Sandbox Developers.
  2
+# This file was originally produced by Mike Tu.
  3
+# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
  4
+# See the file 'docs/LICENSE' for copying permission.
15 5
 
16 6
 import logging
17 7
 import subprocess

No commit comments for this range

Something went wrong with that request. Please try again.