Package: yaralyzer
THE YARALYZERVisually inspect all of the regex matches (and their sexier, more cloak and dagger cousins, theYARAmatches) found in binary data and/or text. See what happens when you force various character encodings upon those matched bytes.With colors.Quick Startpipxinstallyaralyzer# Scan against YARA definitions in a file:yaralyze--yara-rules/secret/vault/sigmunds_malware_rules.yaralacan_buys_the_dip.pdf# Scan against an arbitrary regular expression:yaralyze--regex-pattern'good and evil.*of\s+\w+byte'the_crypto_archipelago.exe# Scan against an arbitrary YARA hex patternyaralyze--hex-pattern'd0 93 d0 a3 d0 [-] 9b d0 90 d0 93'one_day_in_the_life_of_ivan_cryptosovich.binWhat It DoSee the actual bytes your YARA rules are matching.No more digging around copy/pasting the start positions reported by YARA into your favorite hex editor. Displays both the bytes matched by YARA as well as a configurable number of bytes before and after each match in hexadecimal and "raw" python string representation.Do the same for byte patterns and regular expressions without writing a YARA file.If you're too lazy to write a YARA file but are trying to determine, say, whether there's a regular expression hidden somewhere in the file you could scan for the pattern'/.+/'and immediately get a window into all the bytes in the file that live between front slashes. Same story for quotes, BOMs, etc. Any regex YARA can handle is supported so the sky is the limit.Detect the possible encodings of each set of matched bytes.Thechardetlibraryis a sophisticated library for guessing character encodings and it is leveraged here.Display the result of forcing various character encodings upon the matched areas.Several default character encodings will beforciblyattempted in the region around the match.chardetwill also be leveraged to see if the bytes fit the pattern ofanyknown encoding. Ifchardetis confident enough (configurable), an attempt at decoding the bytes using that encoding will be displayed.Export the matched regions/decodings to SVG, HTML, and colored text files.Show off your ASCII art.Why It DoThe Yaralyzer's functionality was extracted fromThe Pdfalyzerwhen it became apparent that visualizing and decoding pattern matches in binaries had more utility than just in a PDF analysis tool.YARA, for those who are unaware[^1], is branded as a malware analysis/alerting tool but it's actually both a lot more and a lot less than that. One way to think about it is that YARA is a regular expression matching engine on steroids. It can locate regex matches in binaries like any regex engine but it can also do far wilder things like combine regexes in logical groups, compare regexes against all 256 XORed versions of a binary, check forbase64and other encodings of the pattern, and more.  Maybe most importantly of all YARA provides a standard text based format for
people tosharetheir 'roided regexes with the world. All these features are particularly useful when analyzing or reverse engineering malware, whose authors tend to invest a great deal of time into making stuff hard to find.But... that's also all YARA does. Everything else is up to the user. YARA's just a match engine and if you don't know what to match (or even what character encoding you might be able to match in) it only gets you so far. I found myself a bit frustrated trying to use YARA to look at all the matches of a few critical patterns:Bytes between escaped quotes (\".+\"and\'.+\')Bytes between front slashes (/.+/). Front slashes demarcate a regular expression in many implementations and I was trying to see if any of the bytes matching this pattern wereactuallyregexes.YARA just tells you the byte position and the matched string but it can't tell you whether those bytes are UTF-8, UTF-16, Latin-1, etc. etc. (or none of the above). I also found myself wanting to understand what was goingin the regionof the matched bytes and not justinthe matched bytes. In other words I wanted to scope the bytes immediately before and after whatever got matched.EnterThe Yaralyzer, which lets you quickly scan the regions around matches while also showing you what those regions would look like if they were forced into various character encodings.It's important to note thatThe Yaralyzerisn't a full on malware reversing tool. It can't do all the things a tool likeCyberChefdoes and it doesn't try to. It's more intended to give you a quick visual overview of suspect regions in the binary so you can hone in on the areas you might want to inspect with a more serious tool likeCyberChef.InstallationInstall it withpipxorpip3.pipxis a marginally better solution as it guarantees any packages installed with it will be isolated from the rest of your local python environment. Of course if you don't really have a local python environment this is a moot point and you can feel free to install withpip/pip3.pipx install yaralyzerUsageRunyaralyze -hto see the command line options (screenshot below).For info on exporting SVG images, HTML, etc., seeExample Output.ConfigurationIf you place a filed called.yaralyzerin your home directory or the current working directory then environment variables specified in that.yaralyzerfile will be added to the environment each time yaralyzer is invoked. This provides a mechanism for permanently configuring various command line options so you can avoid typing them over and over. See the example file.yaralyzer.exampleto see which options can be configured this way.Only one.yaralyzerfile will be loaded and the working directory's.yaralyzertakes precedence over the home directory's.yaralyzer.As A LibraryYaralyzeris the main class. It has a variety of constructors supporting:Precompiled YARA rulesCreating a YARA rule from a stringLoading YARA rules from filesLoading YARA rules from all.yarafile in a directoryScanningbytesScanning a fileShould you want to iterate over theBytesMatch(like are.Matchobject for a YARA match) andBytesDecoder(tracks decoding attempt stats) objects returned by The Yaralyzer, you can do so like this:fromyaralyzer.yaralyzerimportYaralyzeryaralyzer=Yaralyzer.for_rules_files(['/secret/rule.yara'],'lacan_buys_the_dip.pdf')forbytes_match,bytes_decoderinyaralyzer.match_iterator():do_stuff()Example OutputThe Yaralyzer can export visualizations to HTML, ANSI colored text, and SVG vector images using the file export functionality that comes withRich. SVGs can be turned intopngformat images with a tool likeInkscapeorcairosvg. In our experience they both work though we've seen some glitchiness withcairosvg.PyPi Users:If you are reading this documenton PyPibe aware that it renders a lot betterover on GitHub. Pretty pictures, footnotes that work, etc.Raw YARA match result:Display hex, raw python string, and various attempted decodings of both the match and the bytes before and after the match (configurable):Bonus: see whatchardet.detect()thinks about the likelihood your bytes are in a given encoding/language:TODOhighlight decodes done atchardets behestdeal with repetitive matches[^1]: As I was until recently.