Skip to content

ScanBatch

Collects metadata from batch script files.

Pygments is used as a lexer and the tokenized data is appended as metadata.

Attributes:

Name Type Description
lexer

Pygments lexer ('batch') used to parse the file.

Source code in strelka/src/python/strelka/scanners/scan_batch.py
class ScanBatch(strelka.Scanner):
    """Collects metadata from batch script files.

    Pygments is used as a lexer and the tokenized data is appended as metadata.

    Attributes:
        lexer: Pygments lexer ('batch') used to parse the file.
    """

    def init(self):
        self.lexer = lexers.get_lexer_by_name("batch")

    def scan(self, data, file, options, expire_at):
        highlight = pygments.highlight(
            data,
            self.lexer,
            formatters.RawTokenFormatter(),
        )
        highlight_list = highlight.split(b"\n")

        ordered_highlights = []
        for hl in highlight_list:
            split_highlight = hl.split(b"\t")
            if len(split_highlight) == 2:
                token = split_highlight[0].decode()
                value = split_highlight[1].decode().strip("'\"").strip()
                highlight_entry = {"token": token, "value": value}
                if highlight_entry["value"]:
                    ordered_highlights.append(highlight_entry)

        self.event.setdefault("tokens", [])
        self.event.setdefault("comments", [])
        self.event.setdefault("keywords", [])
        self.event.setdefault("labels", [])
        self.event.setdefault("strings", [])
        self.event.setdefault("text", [])
        self.event.setdefault("variables", [])

        position = 0
        while position < len(ordered_highlights):
            ohlp = ordered_highlights[position]
            if ohlp["token"] not in self.event["tokens"]:
                self.event["tokens"].append(ohlp["token"])
            if ohlp["token"] == "Token.Comment.Single":
                if ohlp["value"] not in self.event["comments"]:
                    self.event["comments"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Keyword":
                if ohlp["value"] not in self.event["keywords"]:
                    self.event["keywords"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Name.Label":
                if ohlp["value"] not in self.event["labels"]:
                    self.event["labels"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Literal.String.Double":
                if ohlp["value"] not in self.event["strings"]:
                    self.event["strings"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Literal.String.Single":
                if ohlp["value"] not in self.event["strings"]:
                    self.event["strings"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Text":
                if ohlp["value"] not in self.event["text"]:
                    self.event["text"].append(ohlp["value"])
            elif ohlp["token"] == "Token.Name.Variable":
                if ohlp["value"] not in self.event["variables"]:
                    self.event["variables"].append(ohlp["value"])
            position += 1

Features

The features of this scanner are detailed below. These features represent the capabilities and the type of analysis the scanner can perform. This may include support for Indicators of Compromise (IOC), the ability to emit files for further analysis, and the presence of extended documentation for complex analysis techniques.

Feature
Support
IOC Support
Emit Files
Extended Docs
Malware Scanner
Image Thumbnails

Tastes

Strelka's file distribution system assigns scanners to files based on 'flavors' and 'tastes'. Flavors describe the type of file, typically determined by MIME types from libmagic, matches from YARA rules, or characteristics of parent files. Tastes are the criteria used within Strelka to determine which scanners are applied to which files, with positive and negative tastes defining files to be included or excluded respectively.

Source Filetype
Include / Exclude
batch_file
text/x-msdos-batch

Scanner Fields

This section provides a list of fields that are extracted from the files processed by this scanner. These fields include the data elements that the scanner extracts from each file, representing the analytical results produced by the scanner. If the test file is missing or cannot be parsed, this section will not contain any data.

Field Name
Field Type
comments
list
elapsed
str
flags
list
keywords
list
labels
list
strings
list
text
list
tokens
list
variables
list

Sample Event

Below is a sample event generated by this scanner, demonstrating the kind of output that can be expected when it processes a file. This sample is derived from a mock scan event configured in the scanner's test file. If no test file is available, this section will not display a sample event.

    test_scan_event = {
        "elapsed": 0.001,
        "flags": [],
        "tokens": [
            "Token.Punctuation",
            "Token.Keyword",
            "Token.Text",
            "Token.Name.Variable",
            "Token.Literal.String.Double",
            "Token.Operator",
            "Token.Comment.Single",
            "Token.Name.Label",
        ],
        "comments": [
            "REM Simple batch script for calling avrdude with options for USBtinyISP",
            "REM (C) 2012, 2013 Michael Bemmerl",
            "REM License: WTFPL-2.0",
        ],
        "keywords": ["echo", "SETLOCAL", "SET", "IF", "NOT", "GOTO"],
        "labels": ["help", "exit"],
        "strings": ["avrdude", "\\\\bin\\\\avrdude.exe"],
        "text": [
            "off",
            "\\n",
            "\\n\\n",
            "-c",
            "usbtiny",
            "-P",
            "usb",
            "You",
            "probably",
            "want",
            "to",
            "add",
            "at",
            "least",
            "the",
            "part",
            "option",
            "-p",
            "[partno]",
            ".",
            "and",
            "some",
            "other",
            "AVRDUDE",
            "command",
            "line",
            "like",
            "-U",
            "flash:w:[file]",
        ],
        "variables": ["AVRDUDE", "%AVR32_HOME%", "%1", "%AVRDUDE%", "%*"],
    }