Package: valhallaAPI
valhallaAPIThis module allows you to interact with the Valhalla API, retrieve YARA and Sigma rules in different formats, filter them and write them to disk. You can find more information about Valhalla onour website. The web interface, which doesn't have the filtering features of the Python module and the client, can be accessedhere.It contains a Python modulevalhallaAPIand a Python command line API clientvalhalla-cli.Python ModuleThe web API allows you to retrieve the subscribed rules.The 4 main functions of the Python module are:get_rules_text()retrieves YARA rules as textget_rules_json()retrieves YARA rules as JSONget_sigma_rules_zip()retrieves Sigma rules as ZIPget_sigma_rules_json()retrieves Sigma rules as JSONThe module provides functions to filter the retrieved rules based ontagsscore (YARA only)keywordssupported YARA version and required YARA modulesIt also allows you to retrieve a filtered rule set that fits the product that you use to apply the rules. For example, you can get a filtered rule set with rules that will run on yourFireEyeEXappliance by filtering all rules that use feature only available in YARA versions higher than the supported1.7.0.There are 2 extra functions for special lookups in the Valhalla database (for customers only):get_rule_inforetrieves rule information and all matching sample hashesget_hash_inforetrieves all rules matching on a certain sha256 hashget_sigma_rule_inforetrieves sigma rule informationDemo AccessThere is a demo API key that can be used for testing purposes.1111111111111111111111111111111111111111111111111111111111111111It will allow you to retrieve the processed publicsignature-baseYARA andSigmaHQSigma rule sets.The key will also allow you to query the rule info for a single rule, which is:Casing_Anomaly_ByPassPlease note that Valhalla has protection mechanisms in place that will block your end of the Bifrost for a significant amount of time if you try foolish things.Getting StartedpipinstallvalhallaAPINotes:make sure to use Python 3.6 or higheryou may need to usepip3 install valhallaAPIon Debian systemsUsageGet a service status (does not require a valid API key)fromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI()status=v.get_status()Response{
  "error": "none", 
  "num_rules": 10463, 
  "status": "green", 
  "version": 2020051212
}Text RulesGet all subscribed rules as text and save them to a filefromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_rules_text()withopen('valhalla-rules.yar','w')asfh:fh.write(response)Or use the DEMO API key, which allows you to retrieve all public rulesfromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="1111111111111111111111111111111111111111111111111111111111111111")response=v.get_rules_text()withopen('valhalla-rules.yar','w')asfh:fh.write(response)Get all subscribed rules with a minimum score of 75 and save it to a fileresponse=v.get_rules_text(score=75)Get all subscribed rules that contain the keywordMimikatzand save them to a fileresponse=v.get_rules_text(search="Mimikatz")Get all subscribed rules for your scan engine, which suppports YARA up to version3.2.0and thepemodule, and save them to a fileresponse=v.get_rules_text(max_version="3.2.0",modules=['pe'])Get all subscribed rules for yourFireEyeEXfromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_rules_text(product="FireEyeEX")The following products have predefined presetsFIREEYEAX="FireEyeAX"FIREEYENX="FireEyeNX"FIREEYEEX="FireEyeEX"CARBONBLACK="CarbonBlack"An example response will look like/*
    VALHALLA YARA RULE SET
    Retrieved: 2019-02-25 14:54
    Generated for User: a67
    Number of Rules: 8127
    ANY REPRODUCTION OR DISTRIBUTION IS STRICTLY PROHIBITED WITHOUT THE PRIOR WRITTEN CONSENT OF NEXTRON SYSTEMS AND MAY RESULT IN LEGAL ACTION AS WELL AS THE TERMINATION OF THE CONTRACTUAL RELATIONSHIP
*/

import "pe"

rule SUSP_Katz_PDB_RID664 : EXE SUSP DEMO FILE {
   meta:
      description = "Detects suspicious PDB in file"
      author = "Florian Roth"
      reference = "Internal Research"
      date = "2019-02-04 10:32:31"
      score = 70
      customer = "demo"
      copyright = "Distribution to third parties is not permitted and will be pursued with legal measurements" 
      minimum_yara = "1.7"
      
   strings:
      $s1 = /\\Release\\[a-z]{0,8}katz.pdb/ 
      $s2 = /\\Debug\\[a-z]{0,8}katz.pdb/ 
   condition: 
      uint16 ( 0 ) == 0x5a4d and filesize < 6000KB and all of them
} 
...JSON OutputGet all subscribed rules with theAPTtag asJSONand save them to a fileimportjsonfromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_rules_json(tags=['APT'])withopen('valhalla-rules.json','w')asfh:fh.write(json.dumps(response))An example response will look like{"api_version":"1.0","copyright":"Nextron Systems GmbH","customer":"demo","date":"2019-03-07 10:55","legal_note":"Any reproduction or distribution is strictly prohibited without the prior written consent of Nextron Systems and may result in legal action as well as the termination of the contractual relationship","rules":[{"author":"Florian Roth","content":"rule EXP_Libre_Office_CVE_2018_16858_RIDBA9 : EXPLOIT OFFICE DEMO FILE APT {\n   meta:\n      description = \"Detects exploits addressing CVE-2018-16858 in LibreOffice - modified version\"\n      author = \"Florian Roth\"\n      reference = \"https://insert-script.blogspot.com/2019/02/libreoffice-cve-2018-16858-remote-code.html\"\n      date = \"2019-02-05 14:17:21\"\n      score = 70\n      customer = \"demo\"\n      copyright = \"Distribution to third parties is not permitted and will be pursued with legal measurements\" \n      minimum_yara = \"1.7\"\n      \n   strings:\n      $x1 = \"&#x74;&#x65;&#x6d;&#x70;&#x66;&#x69;&#x6c;&#x65;&#x70;&#x61;&#x67;&#x65;&#x72\" \n      $x2 = \"&#116;&#101;&#109;&#112;&#102;&#105;&#108;&#101;&#112;&#97;&#103;&#101;&#114;\" \n      $s1 = \"xlink:href=\\\"vnd.sun.star.script:\" ascii nocase\n      $s2 = \".py$tempfilepager\" ascii nocase\n      $s3 = \"language=Python\" ascii nocase\n   condition: \n      uint32be ( 0 ) == 0x3c3f786d and all of them or 1 of ( $x* )\n}","date":"2019-02-05 12:54:31","description":"Detects exploits addressing CVE-2018-16858 in LibreOffice - modified version","minimum_yara":"1.7","name":"EXP_Libre_Office_CVE_2018_16858_RID9B8","reference":"https://insert-script.blogspot.com/2019/02/libreoffice-cve-2018-16858-remote-code.html","required_modules":[],"score":70,"tags":["EXPLOIT","OFFICE","DEMO","FILE","APT"]},...Rule InfoGet the information for ruleCasing_Anomaly_ByPassfromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_rule_info(rulename="Casing_Anomaly_ByPass")Note that the rule info forCasing_Anomaly_ByPassis the only info that you can retrieve with the DEMO API key.
IMPORTANT: The rule info endpoint is rate limited. You can use it for single lookups. Bulk requests lead to bans.An example output of a rule info request will look like{"author":"Florian Roth","av_ratio":16.52,"av_verdicts":{"clean":1,"malicious":21,"suspicious":27},"date":"2019-01-17 11:50:21","description":"Detects suspicious casing of bypass statement","minimum_yara":"1.7","name":"Casing_Anomaly_ByPass_RID837","reference":"Internal Research","required_modules":[],"rule_matches":[{"hash":"bdde03b5b4f94ec7dbf947f3099f2009efac43b69659f788f513d3e615b98353","positives":24,"size":319485,"timestamp":"Thu, 07 Mar 2019 06:29:06 GMT","total":56},{"hash":"646d446fb11eae76ca8b6e54306bb022431a4f20cc8cef5daa40dd6ec3537aff","positives":3,"size":573,"timestamp":"Thu, 07 Mar 2019 00:15:07 GMT","total":57}],"score":60,"tags":["SUSP","CASING"]}Hash InfoGet the information for hash8a883a74702f83a273e6c292c672f1144fd1cce8ee126cd90c95131e870744af(only SHA256 hashes are supported)fromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_hash_info(hash="8a883a74702f83a273e6c292c672f1144fd1cce8ee126cd90c95131e870744af")An example output of a hash info request will look like{"api_version":"1.0.1","results":[{"positives":4,"rulename":"Casing_Anomaly_ByPass","tags":["T1027","SUSP","CASING"],"timestamp":"Tue, 11 Jun 2019 23:57:10 GMT","total":58},{"positives":4,"rulename":"Casing_Anomaly_Convert_PS","tags":["T1027","CASING","SCRIPT","T1064"],"timestamp":"Tue, 11 Jun 2019 23:57:12 GMT","total":58},{"positives":4,"rulename":"Casing_Anomaly_PowerShell","tags":["T1027","CASING","SCRIPT","T1064","T1086"],"timestamp":"Tue, 11 Jun 2019 23:57:13 GMT","total":58}],"status":"success"}Keyword Lookup(only available for customers)Get all rules based on a keyword search (e.g.Turla,BypassorPlugX)fromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_keyword_rules(keyword="Turla")An example output of a keyword request will look like{"api_version":"1.1.0","results":[{"date":"2020-12-02","description":"Detects forensic artefacts as reported in Turla Crutch report","name":"APT_RU_Turla_CrutchReport_ForensicArtefacts_Dec20_1","reference":"https://www.welivesecurity.com/2020/12/02/turla-crutch-keeping-back-door-open/","required_modules":[]},{"date":"2020-12-02","description":"Detects Turla Crutch malware","name":"APT_RU_Turla_CrutchReport_Crutch_Dec20_1","reference":"https://www.welivesecurity.com/2020/12/02/turla-crutch-keeping-back-door-open/","required_modules":[]},...],"status":"success"}Keyword Matches Lookup(only available for customers)Get all sample matches of rules selected by keyword search (e.g.Turla,BypassorPlugX)fromvalhallaAPI.valhallaimportValhallaAPIv=ValhallaAPI(api_key="Your API Key")response=v.get_keyword_rule_matches(keyword="LuckyMouse")An example output of a keyword request will look like{"api_version":"1.1.0","results":[{"hash":"00847787ea6568cfaaa762f4ee333b44f35a34e90858c1c8899144be016510ef","positives":44,"rulename":"APT_MAL_CN_LuckyMouse_Loader_Dec20_2","size":81920,"timestamp":"Mon, 28 Dec 2020 09:45:12 GMT","total":70},{"hash":"c2dc17bdf16a609cdb5a93bf153011d67c6206f7608931b1ca1c1d316b5ad54f","positives":49,"rulename":"APT_MAL_CN_LuckyMouse_Loader_Dec20_2","size":81920,"timestamp":"Thu, 10 Dec 2020 17:04:55 GMT","total":68},{"hash":"2b1d6a8538452e3b315283c124f6ee7e27dfd55f52996d3aa89a5919f80e0ef7","positives":13,"rulename":"APT_LuckyMouse_Mal_1","size":81920,"timestamp":"Fri, 23 Oct 2020 16:22:19 GMT","total":70},{"hash":"b85aee07213836bd8784852860ff3b180d71f36fd98d49cc432162aa2234f99d","positives":12,"rulename":"APT_MAL_LuckyMouse_EmissaryPanda_Gen_May19_1","size":71680,"timestamp":"Thu, 30 Jan 2020 20:18:48 GMT","total":70},{"hash":"a8a2221814aab518db0a48d9646f598d9da1bd6c749a792a3605a562eac79980","positives":0,"rulename":"APT_MAL_LuckyMouse_EmissaryPanda_Gen_May19_1","size":45568,"timestamp":"Sat, 07 Dec 2019 13:45:06 GMT","total":68},{"hash":"2dde8881cd9b43633d69dfa60f23713d7375913845ac3fe9b4d8a618660c4528","positives":43,"rulename":"APT_MAL_LuckyMouse_EmissaryPanda_Gen_May19_1","size":71680,"timestamp":"Thu, 30 May 2019 02:37:05 GMT","total":70}],"status":"success"}API ClientThe API client allows you to query the Web API from command line. It requires Python3.Getting StartedInstall Python3 and then run the following command:pip3installvalhallaAPIYou should then be able to runvalhalla-clifrom command line using a Linux or macOS system.On Windows, do the following:wherevalhalla-cliCopy the full path and then runpythonC:\Python37\Scripts\valhalla-clior just download the precompiledvalhalla-cli.exefrom the latest release in thereleasesection.Usageusage: valhalla-cli [-h] [-k apikey] [-c config-file] [-o output-file] [--check] [--debug] [-p proxy-url] [-pu proxy-user] [-pp proxy-pass] [-fp product] [-fv yara-version] [-fm modules [modules ...]]
                    [-ft tags [tags ...]] [-fs score] [-fq query] [--nocrypto] [-lr lookup-rule] [-lh lookup-hash] [-lk lookup-keyword] [-lkm lookup-keyword] [-lo lookup-output]

Valhalla-CLI

optional arguments:
  -h, --help            show this help message and exit
  -k apikey             API KEY
  -c config-file        Config file (see README for details)
  -o output-file        output file
  -s                    Load sigma rules
  --check               Check subscription info and total rule count
  --debug               Debug output

=======================================================================
Proxy:
  -p proxy-url          proxy URL (e.g. https://my.proxy.net:8080)
  -pu proxy-user        proxy user
  -pp proxy-pass        proxy password

=======================================================================
Filter:
  -fp product           filter product (valid products are: FireEyeAX, FireEyeNX, FireEyeEX, CarbonBlack, Tanium, Tenable, SymantecMAA, osquery, GRR, McAfeeATD3, McAfeeATD4)
  -fv yara-version      get rules that support the given YARA version and lower
  -fm modules [modules ...]
                        set a list of modules that your product supports (e.g. "-fm pe hash") (setting no modules means that all modules are supported by your product)
  -ft tags [tags ...]   set a list of tags to receive (e.g. "-ft APT MAL")
  -fs score             minimum score of rules to retrieve (e.g. "-fs 75")
  -fq query             get only rules that match a certain keyword in name or description (e.g. "-fq Mimikatz")
  --nocrypto            filter all rules that require YARA to be compiled with crypto support (OpenSSL)

=======================================================================
Lookups:
  -lr lookup-rule       Lookup a certain rule (returns matching samples)
  -lh lookup-hash       Lookup a certain sample hash (sha256) (returns matching rules)
  -lk lookup-keyword    Lookup rules with a certain keyword (returns matching rules)
  -lkm lookup-keyword   Lookup hashes of samples on which rules have matches that contain a certain keyword (returns matching sample hashes)
  -lo lookup-output     Output file for the lookup outputExamplesCheck the status of the demo user subscriptionvalhalla-cli--checkCheck the status of your subscriptionvalhalla-cli-kYOUR-API-KEY--checkGet all subscribed rules and save them tovalhalla-rules.yarvalhalla-cli-kYOUR-API-KEYGet all sigma rules and save them tovalhalla-rules.zipvalhalla-cli-kYOUR-API-KEY-sGet rules with score higher than 75 and save them tovalhalla-rules.yarvalhalla-cli-kYOUR-API-KEY-fs75Get rules that work with CarbonBlack and save them tovalhalla-april-cb.yarvalhalla-cli-kYOUR-API-KEY-fpCarbonBlack-ovalhalla-april-cb.yarGet rules that contain the keywordMimikatzand save them tomimikatz-rules.yarvalhalla-cli-kYOUR-API-KEY-fqMimikatz-omimikatz-rules.yarGet a set of rules with the highest compatibility (lowest requirements) using the demo API keyvalhalla-cli-fv1.7Get list of rules for the keywordTurlavalhalla-cli-kYOUR-API-KEY-lkTurlaGet all matches of rules that matched on the keywordTurla(limit 10,000 results)valhalla-cli-kYOUR-API-KEY-lkmTurlaConfig FileValhalla-CLI will check~/.valhallaas the default location for a config file.The config file currently contains nothing but the API key and mus look like:[DEFAULT]APIKEY=786feaef202a37a8d693c57b1aeb7c8995313e358b901015c4e60033776929c3LookupsValhalla-CLI has certain functions to perform lookups on its database.The lookups return JSON output. You can use the-lo fileoption to save the JSON results to a file.Hash LookupsHash lookup can be used to search the Valhalla database for a certain hash (SHA256 only)../valhalla-cli-lh8a883a74702f83a273e6c292c672f1144fd1cce8ee126cd90c95131e870744afIt will return a JSON structure.{"api_version":"1.0.1","results":[{"positives":4,"rulename":"Casing_Anomaly_ByPass","tags":["T1027","SUSP","CASING"],"timestamp":"Tue, 11 Jun 2019 23:57:10 GMT","total":58},{"positives":4,"rulename":"Casing_Anomaly_Convert_PS","tags":["T1027","CASING","SCRIPT","T1064"],"timestamp":"Tue, 11 Jun 2019 23:57:12 GMT","total":58},{"positives":4,"rulename":"Casing_Anomaly_PowerShell","tags":["T1027","CASING","SCRIPT","T1064","T1086"],"timestamp":"Tue, 11 Jun 2019 23:57:13 GMT","total":58}],"status":"success"}Rule LookupsRule lookups can be used to search the Valhalla database for a certain rule and their matches (premium feature)../valhalla-cli-lrCasing_Anomaly_ByPassIt will return a JSON structure.{"author":"Florian Roth","av_ratio":18.3,"av_verdicts":{"clean":10,"malicious":59,"suspicious":52},"date":"2019-01-17 11:50:21","description":"Detects suspicious casing of bypass statement","minimum_yara":"1.7","name":"Casing_Anomaly_ByPass_RID2F47","reference":"Internal Research","required_modules":[],"rule_hash":"69b40d02020addf42cd12d3449933a3f","rule_matches":[{"hash":"8a883a74702f83a273e6c292c672f1144fd1cce8ee126cd90c95131e870744af","positives":4,"size":5645,"timestamp":"Tue, 11 Jun 2019 23:57:10 GMT","total":58},{"hash":"6999c997b09754fa100779af9d23a005c2a5b8944ee46175857e58e47626de65","positives":11,"size":1830,"timestamp":"Tue, 11 Jun 2019 08:46:05 GMT","total":58},],"score":60,"tags":["T1027","SUSP","CASING"]}To query sigma rules, add-s:./valhalla-cli-s-lr06d71506-7beb-4f22-8888-e2e5e2ca7fd8ScoresThe following list explains the scores used in the rule setScoreTypeDescription1-39InfoLow scoring rules used in our scanners (excluded from Valhalla, only used in our scanners)40-59NoteworthyAnomaly and threat hunting rules60-74SuspiciousRules for suspicious objects75-100AlertHard malicious matchesImportant NoticesWe constantly improve old rules. They may have changed the next time you fetch the rule set. Therefore it is recommended to always fetch a full set and replace older rules with their newer versions.The full rule set contains YARA rules with scores lower than 60, which are meant for threat hunting and anomaly detection use cases.