Creating YARA files with Python
When I’m researching a piece of malware, I’ll have a notepad open (usually VS Code), where I’m capturing strings that might be useful for a detection rule. When I have a good set of indicators, the next step is to turn them into a YARA rule.
It’s easy enough to create a YARA file by hand. My objective was to streamline the boring stuff like formatting and generating a string identifier ($s1 = “stringOne”) for each string. Normally PowerShell is my goto, but this week I’m branching out and wanted to work on my Python coding.
The code relies on you having a file called strings.txt. One string per line.
When you run the script it will prompt for (metadata):
- rule name
- author
- description
- hash
It then takes the contents of strings.txt and combines those with the metadata to produce a cleanly formatted YARA rule.
Caveats:
If the strings have special characters that need to be escaped, you may need to tweak the strings in the rule after it’s created.
The script will define the condition “any of them”. If you prefer to have all strings required, you can change line 22 from
yara_rule += '\t\tany of them\n}\n'
to
yara_rule += '\t\tall of them\n}\n'
CreateYARA.py
def get_user_input():
rule_name = input("Enter the rule name: ")
author = input("Enter the author: ")
description = input("Enter the description: ")
hash_value = input("Enter the hash value: ")
return rule_name, author, description, hash_value
def create_yara_rule(rule_name, author, description, hash_value, strings_file):
yara_rule = f'''rule {rule_name} {{
meta:
\tauthor = "{author}"
\tdescription = "{description}"
\thash = "{hash_value}"
strings:
'''
with open(strings_file, 'r') as file:
for id, line in enumerate(file, start=1):
yara_rule += f'\t$s{id} = "{line.strip()}"\n\t'
yara_rule += '\n'
yara_rule += '\tcondition:\n'
yara_rule += '\t\tany of them\n}\n'
return yara_rule
def main():
rule_name, author, description, hash_value = get_user_input()
strings_file = 'strings.txt'
yara_rule = create_yara_rule(rule_name, author, description, hash_value, strings_file)
print("Generated YARA rule:")
print(yara_rule)
yar_filename = f'{rule_name}.yar'
with open(yar_filename, 'w') as yar_file:
yar_file.write(yara_rule)
print(f"YARA rule saved to {yar_filename}")
if __name__ == "__main__":
main()
Sample strings.txt file used as input for the YARA rule Running CreateYARA.py YARA rule created from Python script, viewed in VS Code.