|
| 1 | +# Contributing |
| 2 | + |
| 3 | +Found a new malware? Great! Add a new sample and rule in 5 minutes. |
| 4 | + |
| 5 | +1. Add malware file |
| 6 | +1. Find a literal string or regex that identifies the malware |
| 7 | +1. Run tests to validate results |
| 8 | +1. Make Pull Request |
| 9 | +1. Profit! |
| 10 | + |
| 11 | +Every malware should have at least 1 corresponding rule. |
| 12 | + |
| 13 | +Every rule should have at least 1 corresponding malware. |
| 14 | + |
| 15 | +# How to add malware |
| 16 | + |
| 17 | +After installing the requirements, malware can be added by following these steps. |
| 18 | + |
| 19 | +## Gather your malware samples |
| 20 | + |
| 21 | +Gather your malware samples in the `malware/incoming` folder of the _magento-malware-scanner_. To check if your samples aren't already detected by the current set of rules. |
| 22 | + |
| 23 | +```bash |
| 24 | +yara -r ./rules/all-confirmed.yar ./malware/incoming |
| 25 | +``` |
| 26 | + |
| 27 | +If your sample is already detected for the right reasons, you may skip this sample. |
| 28 | + |
| 29 | +## Process the samples |
| 30 | + |
| 31 | +Running `md5_to_incoming.sh` will move your samples to the `malware/backend` directory and rename them with an md5 checksum. |
| 32 | + |
| 33 | +```bash |
| 34 | +./tools/md5_to_incoming.sh |
| 35 | +``` |
| 36 | + |
| 37 | +We can now generate boilerplate rules using `tools/runtests.py`. This will make sure that each sample is covered by a YARA rule. If not, a boilerplate rule will be outputted. |
| 38 | + |
| 39 | +__example output__ |
| 40 | + |
| 41 | +``` |
| 42 | +// malware/backend/4c4b3d4ba5bce7191a5138efa2468679 |
| 43 | +rule md5_4c4b3d4ba5bce7191a5138efa2468679 { |
| 44 | + strings: $ = "" |
| 45 | + condition: any of them |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Copy over the boilerplate rule(s) to `rules/backend.yar` and start editing them. |
| 50 | + |
| 51 | +## Creating rules |
| 52 | + |
| 53 | +Using the boilerplate rules, creating a rule can be as simple as defining a string to match. |
| 54 | +Malware signatures can be extremely specific (a file checksum) or too generic (check for suspicious `eval()`). |
| 55 | + |
| 56 | +__Example malware__ |
| 57 | + |
| 58 | +```PHP |
| 59 | +<?php @eval(stripslashes($_REQUEST[q])); |
| 60 | +``` |
| 61 | +__Example rule__ |
| 62 | + |
| 63 | +``` |
| 64 | +rule md5_d201d61510f7889f1a47257d52b15fa2 { // rule name (sample filename or descriptive name) |
| 65 | + strings: $ = "@eval(stripslashes($_REQUEST[q]));" // string(s) to match |
| 66 | + condition: any of them // conditions |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The above example is simple and sufficient. More advanced samples may require more complex signatures. See below for signature writing strategies. In short: |
| 71 | + |
| 72 | +1. One signature can cover multiple strains of malware. |
| 73 | +1. It's better to be specific and have less coverage, than broad coverage and possibly raise false positives. |
| 74 | +1. Prefer Yara rules that have "any of them" as these rules can be converted in simple `grep` rules. |
| 75 | +1. Prefer string match over regex match (for speed) |
| 76 | +1. If using regex, limit the use of unbound match operators (`+` or `*`) as they might have to scan the whole file. Better: `{,x}` to limit to x characters. |
| 77 | + |
| 78 | +For more information on writing rules, refer to the documentation below |
| 79 | + |
| 80 | +* [Writing YARA rules](http://yara.readthedocs.io/en/v3.5.0/writingrules.html) |
| 81 | +* [YARA in a Nutshell](http://virustotal.github.io/yara/) |
| 82 | +* [Example Yara Rules](https://github.com/Yara-Rules/rules) |
| 83 | + |
| 84 | +# 4. Test your rules |
| 85 | + |
| 86 | +This repository contains 2 tools and automated tests to make sure samples and fingerprints have at least one match and don't raise false flags. |
| 87 | +Run these before sending a pull request. |
| 88 | + |
| 89 | +## Verifying that samples and fingerprints have matches |
| 90 | + |
| 91 | +To verify this, run the following command from the project root: |
| 92 | + |
| 93 | +```bash |
| 94 | +python tools/validate_signatures.py |
| 95 | +``` |
| 96 | + |
| 97 | +## Checking for false flags against vanilla Magento code |
| 98 | + |
| 99 | +The `tools/mageffcheck.sh` bash script has a couple of commands available to get you started. |
| 100 | +The idea is to download and extract various vanilla Magento packages and run the YARA tests against it. |
| 101 | +When these tests return output, it's most likely a false flag. |
| 102 | + |
| 103 | +Running the following command will output some information: |
| 104 | + |
| 105 | +```bash |
| 106 | +./tools/mageffcheck.sh |
| 107 | + |
| 108 | +# OR |
| 109 | + |
| 110 | +./tools/mageffcheck.sh help |
| 111 | +``` |
| 112 | + |
| 113 | +You can initialize the test setup by appending the `init` argument |
| 114 | + |
| 115 | +```bash |
| 116 | +./tools/mageffcheck.sh init |
| 117 | +``` |
| 118 | + |
| 119 | +This will download and extract several Magento packages from the [OpenMage Magento Mirror](https://github.com/OpenMage/magento-mirror). |
| 120 | + |
| 121 | +__Note: When using an IDE__ |
| 122 | + |
| 123 | +Downloading and extracting several Magento package may trigger the IDE's indexation. Due to the amount of code it may lag or even hang (for a while). |
| 124 | +These packages will be stored in `./tmp` make sure to _mark this directory as excluded_ before or right after running `init`. |
| 125 | + |
| 126 | +After this you could run the following to trigger the YARA tests against the vanilla code. |
| 127 | + |
| 128 | +```bash |
| 129 | +./tools/mageffcheck.sh run |
| 130 | +``` |
| 131 | +You should expect _no output_ from running this command. |
| 132 | + |
| 133 | + |
| 134 | +# Signature strategies |
| 135 | + |
| 136 | +Malware signatures can be extremely specific (a file checksum) to generic (check for suspicious `eval()`) or anything in between. As a signature author, you have to decide on a proper balance. Pro specific: no chance for false positives. Pro generic: less work, as one signature will catch multiple strains of malware. |
| 137 | + |
| 138 | +> In case of doubt, choose specific. |
| 139 | +
|
| 140 | +Taking a shortcut with a more generic signature might be tempting, but if it causes false positives (possibly in the future), you will have to deal with the fallout, _plus_ have to repeat your malware analysis. |
| 141 | + |
| 142 | +Remember, we can already win a lot by de-duplicating unique malware instance identification across organisations. Identifying future, or merely suspicious, malware is also desireable but requires a different strategy. |
| 143 | + |
| 144 | +Other malware research suggests that malwares are quickly morphing, rendering the average lifespan of a unique malware instance to be [only 58 seconds](https://twitter.com/jeremiahg/status/799734794737184768). But I haven't seen this behaviour in Magento specific malware yet. Let's enjoy the tide while it lasts. |
| 145 | + |
0 commit comments