|
| 1 | +""" |
| 2 | +.. module:: extractor |
| 3 | + :platform: Unix |
| 4 | + :synopsis: A helper module that understands that some zip files submitted |
| 5 | + to Analys will be zips that may be password protected. |
| 6 | +
|
| 7 | + This module is a helper module that understands that |
| 8 | + some zip files submitted to Analys will be zips that may be password |
| 9 | + protected. If this is the case Analys will attempt to bruteforce the password. |
| 10 | + All files contained inside of the zip will submitted individually to Analys. |
| 11 | +
|
| 12 | +.. version:: 1.0 |
| 13 | +.. moduleauthor:: Kevin Glisson <[email protected]> |
| 14 | +""" |
| 15 | +import os |
| 16 | +import zipfile |
| 17 | +import rarfile |
| 18 | +import logging |
| 19 | +from analys.plugins.interfaces import File |
| 20 | +log = logging.getLogger(__file__) |
| 21 | + |
| 22 | +class Extractor(object): |
| 23 | + """ Extract attempts to identify the password used to |
| 24 | + encrypt a zip file. The most common passwords. |
| 25 | +
|
| 26 | + :param data: raw file bytes |
| 27 | + :type data: byte string |
| 28 | +
|
| 29 | + """ |
| 30 | + def __init__(self, file, passwords): |
| 31 | + self.file = file |
| 32 | + self.samples = [] |
| 33 | + self.max_depth = 5 #only allow five levels of recursion for compressed files |
| 34 | + self.depth = 0 |
| 35 | + self.passwords = passwords |
| 36 | + |
| 37 | + def extract(self): |
| 38 | + if self.depth <= self.max_depth: |
| 39 | + if 'zip' in self.file.extension(): |
| 40 | + with zipfile.ZipFile(self.file.create_temp_file(), 'r') as myzip: |
| 41 | + #sanity checks on the file |
| 42 | + for member in myzip.namelist(): |
| 43 | + filename = os.path.basename(member) |
| 44 | + if not filename: |
| 45 | + continue |
| 46 | + |
| 47 | + try: |
| 48 | + sample = myzip.open(member, 'r',).read() |
| 49 | + f = File(member, sample) |
| 50 | + if f.extension() in ['zip', 'rar']: |
| 51 | + self.depth = self.depth + 1 |
| 52 | + self.extract(f) |
| 53 | + else: |
| 54 | + self.samples.append(f) |
| 55 | + |
| 56 | + except (zipfile.BadZipfile, RuntimeError) as err: |
| 57 | + for password in self.passwords: |
| 58 | + |
| 59 | + try: |
| 60 | + sample = myzip.open(member, 'r', password).read() |
| 61 | + f = File(member, sample) |
| 62 | + if f.extension() in ['zip', 'rar']: |
| 63 | + self.depth = self.depth + 1 |
| 64 | + self.extract(f) |
| 65 | + else: |
| 66 | + self.samples.append(f) |
| 67 | + break |
| 68 | + |
| 69 | + except (zipfile.BadZipfile, RuntimeError) as err: |
| 70 | + continue |
| 71 | + |
| 72 | + if 'rar' in self.file.extension(): |
| 73 | + with rarfile.RarFile(self.file.create_temp_file(), 'r') as myrar: |
| 74 | + #sanity checks on the file |
| 75 | + for member in myrar.infolist(): |
| 76 | + if not member.filename: |
| 77 | + continue |
| 78 | + try: |
| 79 | + sample = myrar.open(member, 'r').read() |
| 80 | + f = File(member, sample) |
| 81 | + if f.extension() in ['zip', 'rar']: |
| 82 | + self.depth = self.depth + 1 |
| 83 | + self.extract(f) |
| 84 | + else: |
| 85 | + self.samples.append(f) |
| 86 | + except: |
| 87 | + for password in self.passwords: |
| 88 | + try: |
| 89 | + sample = myrar.open(member, 'r', |
| 90 | + password).read() |
| 91 | + f = File(member, sample) |
| 92 | + if f.extension() in ['zip', 'rar']: |
| 93 | + self.depth = self.depth + 1 |
| 94 | + self.extract(f) |
| 95 | + else: |
| 96 | + self.samples.append(f) |
| 97 | + break |
| 98 | + except: |
| 99 | + continue |
| 100 | + return self.samples |
| 101 | + |
0 commit comments