|
| 1 | +import shutil |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | + |
| 5 | +from strelka import strelka |
| 6 | + |
| 7 | + |
| 8 | +class ScanClamav(strelka.Scanner): |
| 9 | + """ |
| 10 | + This scanner runs against a given file and returns a ClamAV scan that has a determination if the file is infected |
| 11 | + or not based on the ClamAV signature database. |
| 12 | +
|
| 13 | + Scanner Type: Collection |
| 14 | +
|
| 15 | + Attributes: |
| 16 | + None |
| 17 | +
|
| 18 | + ## Detection Use Cases |
| 19 | + !!! info "Detection Use Cases" |
| 20 | + - **Scan Determination** |
| 21 | + - This scanner provides a inital determination on a file if it is infected or not based on the |
| 22 | + the ClamAV signature database. |
| 23 | +
|
| 24 | + ## Known Limitations |
| 25 | + !!! warning "Known Limitations" |
| 26 | + - **ClamAV Signature Database** |
| 27 | + - This scanner relies on the ClamAV signature database which is not necesarily all-encompassing. Though |
| 28 | + the scanner may return a determination, users should be advise that this is not exaustive. |
| 29 | +
|
| 30 | + ## To Do |
| 31 | + !!! question "To Do" |
| 32 | + - The ClamAV signature database is currently pulled every scan as a POC. This could be converted to a |
| 33 | + signature pull on a cadence, such as every 24 hours. |
| 34 | +
|
| 35 | + ## References |
| 36 | + !!! quote "References" |
| 37 | + - [ClamAV Documentation Source](https://docs.clamav.net/Introduction.html) |
| 38 | + - [BlogPost on ClamAV Scanner](https://simovits.com/strelka-let-us-build-a-scanner/) |
| 39 | +
|
| 40 | + ## Contributors |
| 41 | + !!! example "Contributors" |
| 42 | + - [Sara Kalupa](https://github.com/skalupa) |
| 43 | +
|
| 44 | + """ |
| 45 | + |
| 46 | + def scan(self, data, file, options, expire_at): |
| 47 | + try: |
| 48 | + # Check if ClamAV package is installed |
| 49 | + if not shutil.which("clamscan"): |
| 50 | + self.flags.append("clamav_not_installed_error") |
| 51 | + return |
| 52 | + except Exception as e: |
| 53 | + self.flags.append(str(e)) |
| 54 | + return |
| 55 | + |
| 56 | + try: |
| 57 | + with tempfile.NamedTemporaryFile(dir="/tmp/", mode="wb") as tmp_data: |
| 58 | + tmp_data.write(data) |
| 59 | + tmp_data.flush() |
| 60 | + tmp_data.seek(0) |
| 61 | + |
| 62 | + # Run freshclam to gret the newest database signatures |
| 63 | + stdout, stderr = subprocess.Popen( |
| 64 | + ["freshclam"], |
| 65 | + stdout=subprocess.PIPE, |
| 66 | + stderr=subprocess.PIPE, |
| 67 | + ).communicate(timeout=self.scanner_timeout) |
| 68 | + |
| 69 | + temp_log = tempfile.NamedTemporaryFile(dir="/tmp/", mode="wb") |
| 70 | + loglocation = "--log=" + temp_log.name |
| 71 | + |
| 72 | + # Run the actual ClamAV scan and report to local temp log file |
| 73 | + process = subprocess.Popen( |
| 74 | + ["clamscan", "--disable-cache", loglocation, tmp_data.name], |
| 75 | + stdout=subprocess.PIPE, |
| 76 | + stderr=subprocess.PIPE, |
| 77 | + ) |
| 78 | + stdout, stderr = process.communicate() |
| 79 | + |
| 80 | + with open(temp_log.name, "r") as file: |
| 81 | + for line in file: |
| 82 | + if ":" in line: |
| 83 | + # Attempt to split out scan information |
| 84 | + splitline = line.split(":") |
| 85 | + self.event[splitline[0]] = splitline[1].strip() |
| 86 | + else: |
| 87 | + continue |
| 88 | + |
| 89 | + except strelka.ScannerTimeout: |
| 90 | + raise |
| 91 | + except Exception: |
| 92 | + self.flags.append("clamAV_Scan_process_error") |
| 93 | + raise |
| 94 | + finally: |
| 95 | + # Ensure that tempfile gets closed out if there are any issues |
| 96 | + tmp_data.close() |
0 commit comments