Skip to content

Commit

Permalink
Merge pull request #210 from avast/add_console_module
Browse files Browse the repository at this point in the history
YARA-1823: Add console module
  • Loading branch information
metthal committed Mar 22, 2022
2 parents 52b2c05 + 4ec50ec commit 1bca6d9
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(YARAMOD_DEPS_DIR "${PROJECT_SOURCE_DIR}/deps")
set(YARAMOD_MODULES_DIR "${PROJECT_SOURCE_DIR}/modules")
set(YARAMOD_MODULES_GENERATED_DIR "${YARAMOD_INCLUDE_DIR}/yaramod/types/modules/generated")
set(WRAP_MODULE_SOURCES
${YARAMOD_MODULES_GENERATED_DIR}/module_console.h
${YARAMOD_MODULES_GENERATED_DIR}/module_cuckoo_deprecated_generated.h
${YARAMOD_MODULES_GENERATED_DIR}/module_cuckoo_generated.h
${YARAMOD_MODULES_GENERATED_DIR}/module_dex_generated.h
Expand Down
108 changes: 108 additions & 0 deletions modules/module_console.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"kind": "struct",
"name": "console",
"attributes": [
{
"kind": "function",
"name": "log",
"return_type": "i",
"overloads": [
{
"arguments": [
{
"type": "s",
"name": "string"
}
],
"documentation": "Function which sends the string to the main callback.\n\nExample: ```console.log(pe.imphash())```"
},
{
"arguments": [
{
"type": "s",
"name": "message"
},
{
"type": "s",
"name": "string"
}
],
"documentation": "Function which sends the message and string to the main callback.\n\nExample: ```console.log(\"The imphash is: \", pe.imphash())```"
},
{
"arguments": [
{
"type": "i",
"name": "integer"
}
],
"documentation": "Function which sends the integer to the main callback.\n\nExample: ```console.log(uint32(0))```"
},
{
"arguments": [
{
"type": "s",
"name": "message"
},
{
"type": "i",
"name": "integer"
}
],
"documentation": "Function which sends the message and integer to the main callback.\n\nExample: ```console.log(\"32bits at 0: \", uint32(0))```"
},
{
"arguments": [
{
"type": "f",
"name": "float"
}
],
"documentation": "Function which sends the floating point value to the main callback.\n\nExample: ```console.log(math.entropy(0, filesize))```"
},
{
"arguments": [
{
"type": "s",
"name": "message"
},
{
"type": "f",
"name": "float"
}
],
"documentation": "Function which sends the message and the floating point value to the main callback.\n\nExample: ```console.log(\"Entropy: \", math.entropy(0, filesize))```"
}
]
},
{
"kind": "function",
"name": "hex",
"return_type": "i",
"overloads": [
{
"arguments": [
{
"type": "i",
"name": "integer"
}
],
"documentation": "Function which sends the integer to the main callback, formatted as a hex string.\n\nExample: ```console.hex(uint32(0))```"
},
{
"arguments": [
{
"type": "s",
"name": "message"
},
{
"type": "i",
"name": "integer"
}
],
"documentation": "Function which sends the integer to the main callback, formatted as a hex string.\n\nExample: ```console.hex(\"Hex at 0: \", uint32(0))```"
}
]
}
]
}
25 changes: 25 additions & 0 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3438,6 +3438,31 @@ rule rule_with_float_value_in_condition
EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
ConsoleModuleWorks) {
prepareInput(
R"(
import "console"
rule console_module
{
condition:
console.log("Hello") and
console.log("32bits at 0: ", uint32(0))
}
)");

EXPECT_TRUE(driver.parse(input));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];
EXPECT_EQ(R"(console.log("Hello") and console.log("32bits at 0: ", uint32(0)))", rule->getCondition()->getText());
EXPECT_EQ("console", rule->getCondition()->getFirstTokenIt()->getPureText());
EXPECT_EQ(")", rule->getCondition()->getLastTokenIt()->getPureText());

EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
CuckooModuleWorks) {
prepareInput(
Expand Down

0 comments on commit 1bca6d9

Please sign in to comment.