Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YARA-1748: Yara42 dex attributes #201

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions modules/module_dex.json
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,82 @@
}
]
}
},
{
"kind": "function",
"name": "has_method",
"return_type": "i",
"overloads": [
{
"arguments": [
{
"type": "s",
"name": "method name"
}
],
"documentation": "Returns true if any method has the specified method name."
},
{
"arguments": [
{
"type": "s",
"name": "class name"
},
{
"type": "s",
"name": "method name"
}
],
"documentation": "Returns true if any class has the specified class name and at the same time there is a method with the specified metod name"
},
{
"arguments": [
{
"type": "r",
"name": "method name"
}
],
"documentation": "Returns true if any method name matches the regex."
},
{
"arguments": [
{
"type": "r",
"name": "class name"
},
{
"type": "r",
"name": "method name"
}
],
"documentation": "Returns true if any class name matching the regex class name and at the same time there is a method with name matching the specified regex metod name"
}
]
},
{
"kind": "function",
"name": "has_class",
"return_type": "i",
"overloads": [
{
"arguments": [
{
"type": "s",
"name": "class name"
}
],
"documentation": "Returns true if any class has the specified name."
},
{
"arguments": [
{
"type": "r",
"name": "class name"
}
],
"documentation": "Returns true if any class name matches the regex."
}
]
}
]
}
62 changes: 62 additions & 0 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3439,6 +3439,68 @@ rule dotnet_module
EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
DexModuleWorks) {
prepareInput(
R"(
import "dex"

rule dex_module_has_method_1
{
condition:
dex.has_method("<init>")
}

rule dex_module_has_method_2
{
condition:
dex.has_method("Lcom/android/tools/ir/server/AppInfo;", "<clinit>")
}

rule dex_module_has_method_3
{
condition:
dex.has_method(/init/)
}

rule dex_module_has_method_4
{
condition:
dex.has_method(/AppInfo/, /init/)
}

rule dex_module_has_class_1
{
condition:
dex.has_class("Lcom/android/tools/ir/server/AppInfo;")
}

rule dex_module_has_class_2
{
condition:
dex.has_class(/AppInfo/)
}
)");

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

const auto& rule1 = driver.getParsedFile().getRules()[0];
EXPECT_EQ("dex.has_method(\"<init>\")", rule1->getCondition()->getText());
const auto& rule2 = driver.getParsedFile().getRules()[1];
EXPECT_EQ("dex.has_method(\"Lcom/android/tools/ir/server/AppInfo;\", \"<clinit>\")", rule2->getCondition()->getText());
const auto& rule3 = driver.getParsedFile().getRules()[2];
EXPECT_EQ("dex.has_method(/init/)", rule3->getCondition()->getText());
const auto& rule4 = driver.getParsedFile().getRules()[3];
EXPECT_EQ("dex.has_method(/AppInfo/, /init/)", rule4->getCondition()->getText());
const auto& rule5 = driver.getParsedFile().getRules()[4];
EXPECT_EQ("dex.has_class(\"Lcom/android/tools/ir/server/AppInfo;\")", rule5->getCondition()->getText());
const auto& rule6 = driver.getParsedFile().getRules()[5];
EXPECT_EQ("dex.has_class(/AppInfo/)", rule6->getCondition()->getText());

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

TEST_F(ParserTests,
ElfModuleWorks) {
prepareInput(
Expand Down