Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbremer committed Jul 20, 2015
1 parent 522b9a1 commit b3cecc5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
63 changes: 33 additions & 30 deletions lib/cuckoo/common/objects.py
Expand Up @@ -254,39 +254,42 @@ def get_yara(self, rulepath=os.path.join(CUCKOO_ROOT, "data", "yara", "index_bin
"""
matches = []

if HAVE_YARA:
if os.path.getsize(self.file_path) > 0:
if not os.path.exists(rulepath):
log.warning("The specified rule file at %s doesn't exist, skip",
rulepath)
return

try:
rules = yara.compile(rulepath)

for match in rules.match(self.file_path):
strings = []
for s in match.strings:
# Beware, spaghetti code ahead.
try:
new = s[2].encode("utf-8")
except UnicodeDecodeError:
s = s[2].lstrip("uU").encode("hex").upper()
s = " ".join(s[i:i+2] for i in range(0, len(s), 2))
new = "{ %s }" % s

if new not in strings:
strings.append(new)

matches.append({"name": match.rule,
"meta": match.meta,
"strings": strings})
except Exception as e:
log.warning("Unable to match Yara signatures: %s", e)
else:
if not HAVE_YARA:
if not File.notified_yara:
File.notified_yara = True
log.warning("Unable to import yara (please compile from sources)")
return matches

if not os.path.exists(rulepath):
log.warning("The specified rule file at %s doesn't exist, skip",
rulepath)
return matches

if not os.path.getsize(self.file_path):
return matches

try:
rules = yara.compile(rulepath)

for match in rules.match(self.file_path):
strings = []
for s in match.strings:
# Beware, spaghetti code ahead.
try:
new = s[2].encode("utf-8")
except UnicodeDecodeError:
s = s[2].lstrip("uU").encode("hex").upper()
s = " ".join(s[i:i+2] for i in range(0, len(s), 2))
new = "{ %s }" % s

if new not in strings:
strings.append(new)

matches.append({"name": match.rule,
"meta": match.meta,
"strings": strings})
except Exception as e:
log.exception("Unable to match Yara signatures: %s", e)

return matches

Expand Down
4 changes: 2 additions & 2 deletions lib/cuckoo/core/database.py
Expand Up @@ -509,10 +509,10 @@ def set_status(self, task_id, status):
session = self.Session()
try:
row = session.query(Task).get(task_id)

if not row:
return

row.status = status

if status == TASK_RUNNING:
Expand Down
2 changes: 0 additions & 2 deletions lib/cuckoo/core/resultserver.py
Expand Up @@ -112,7 +112,6 @@ def build_storage_path(self, ip):

return os.path.join(CUCKOO_ROOT, "storage", "analyses", str(task.id))


class ResultHandler(SocketServer.BaseRequestHandler):
"""Result handler.
Expand Down Expand Up @@ -320,7 +319,6 @@ def close(self):
if self.fd:
self.fd.close()


class LogHandler(object):
def __init__(self, handler):
self.handler = handler
Expand Down
2 changes: 1 addition & 1 deletion web/templates/analysis/overview/_signatures.html
Expand Up @@ -68,4 +68,4 @@ <h4>Signatures</h4>
{% else %}
<p>No signatures</p>
{% endif %}
</section>
</section>

0 comments on commit b3cecc5

Please sign in to comment.