This repository was archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from joernio/claudiu/ghidra-simp
Add MainArgsToStrcpy ghidra query
- Loading branch information
Showing
5 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/scala/io/joern/scanners/ghidra/MainArgsToStrcpy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.joern.scanners.ghidra | ||
|
||
import io.joern.scanners._ | ||
import io.shiftleft.console._ | ||
import io.shiftleft.macros.QueryMacros._ | ||
import io.shiftleft.semanticcpg.language._ | ||
import io.shiftleft.dataflowengineoss.language._ | ||
import io.shiftleft.dataflowengineoss.queryengine.EngineContext | ||
|
||
object MainArgsToStrcpy extends QueryBundle { | ||
|
||
implicit val resolver: ICallResolver = NoResolve | ||
|
||
@q | ||
def mainArgsToStrcpy()(implicit context: EngineContext): Query = | ||
Query.make( | ||
name = "main-args-to-strcpy", | ||
author = Crew.claudiu, | ||
title = "`main` fn arguments used in strcpy source buffer", | ||
description = | ||
""" | ||
|User-input ends up in source buffer argument of strcpy, which might overflow the destination buffer. | ||
|""".stripMargin, | ||
score = 4, | ||
withStrRep({ cpg => | ||
def source = cpg.method.fullName("main").parameter | ||
def sink = cpg.call.methodFullName("strcpy").argument | ||
sink.reachableBy(source).l | ||
}), | ||
tags = List(QueryTags.badfn) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// gcc -fno-stack-protector -z execstack -no-pie -o buf1 buf1.c | ||
int main(int argc, char *argv[]) { | ||
if (argc == 1) { | ||
printf("Program executed with no arguments.\n"); | ||
return 0; | ||
} | ||
char c[6]; | ||
strcpy(c, argv[1]); | ||
printf("First argument is: %s\n", c); | ||
return 0; | ||
} |
Binary file not shown.
14 changes: 14 additions & 0 deletions
14
src/test/scala/io/joern/scanners/ghidra/MainArgsToStrcpyTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.joern.scanners.ghidra | ||
|
||
import io.joern.suites.GhidraQueryTestSuite | ||
|
||
class MainArgsToStrcpyTests extends GhidraQueryTestSuite { | ||
override def queryBundle = MainArgsToStrcpy | ||
|
||
"find main function with data flow between argument and strcpy" in { | ||
buildCpgForBin("buf1.exe") | ||
val query = queryBundle.mainArgsToStrcpy() | ||
val results = findMatchingMethodParam(query) | ||
results shouldBe Set("main") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters