yara.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. pygments.lexers.yara
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for YARA.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words
  9. from pygments.token import Comment, String, Name, Text, Punctuation, \
  10. Operator, Keyword, Whitespace, Number
  11. __all__ = ['YaraLexer']
  12. class YaraLexer(RegexLexer):
  13. """
  14. For YARA rules
  15. """
  16. name = 'YARA'
  17. url = 'https://virustotal.github.io/yara/'
  18. aliases = ['yara', 'yar']
  19. filenames = ['*.yar']
  20. mimetypes = ['text/x-yara']
  21. version_added = '2.16'
  22. tokens = {
  23. 'root': [
  24. (r'\s+', Whitespace),
  25. (r'//.*?$', Comment.Single),
  26. (r'\#.*?$', Comment.Single),
  27. (r'/\*', Comment.Multiline, 'comment'),
  28. (words(('rule', 'private', 'global', 'import', 'include'),
  29. prefix=r'\b', suffix=r'\b'),
  30. Keyword.Declaration),
  31. (words(('strings', 'condition', 'meta'), prefix=r'\b', suffix=r'\b'),
  32. Keyword),
  33. (words(('ascii', 'at', 'base64', 'base64wide', 'condition',
  34. 'contains', 'endswith', 'entrypoint', 'filesize', 'for',
  35. 'fullword', 'icontains', 'iendswith', 'iequals', 'in',
  36. 'include', 'int16', 'int16be', 'int32', 'int32be', 'int8',
  37. 'int8be', 'istartswith', 'matches', 'meta', 'nocase',
  38. 'none', 'of', 'startswith', 'strings', 'them', 'uint16',
  39. 'uint16be', 'uint32', 'uint32be', 'uint8', 'uint8be',
  40. 'wide', 'xor', 'defined'),
  41. prefix=r'\b', suffix=r'\b'),
  42. Name.Builtin),
  43. (r'(true|false)\b', Keyword.Constant),
  44. (r'(and|or|not|any|all)\b', Operator.Word),
  45. (r'(\$\w+)', Name.Variable),
  46. (r'"[^"]*"', String.Double),
  47. (r'\'[^\']*\'', String.Single),
  48. (r'\{.*?\}$', Number.Hex),
  49. (r'(/.*?/)', String.Regex),
  50. (r'[a-z_]\w*', Name),
  51. (r'[$(){}[\].?+*|]', Punctuation),
  52. (r'[:=,;]', Punctuation),
  53. (r'.', Text)
  54. ],
  55. 'comment': [
  56. (r'[^*/]+', Comment.Multiline),
  57. (r'/\*', Comment.Multiline, '#push'),
  58. (r'\*/', Comment.Multiline, '#pop'),
  59. (r'[*/]', Comment.Multiline)
  60. ]
  61. }