Skip to content

Commit

Permalink
Added tags for windows drivers + c routes (#38)
Browse files Browse the repository at this point in the history
* Added tags for windows drivers

Signed-off-by: Prabhu Subramanian <[email protected]>

* Refactor c route tagging

Signed-off-by: Prabhu Subramanian <[email protected]>

---------

Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu authored Nov 30, 2023
1 parent 372526c commit 76fc17e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "chen"
ThisBuild / organization := "io.appthreat"
ThisBuild / version := "1.0.4"
ThisBuild / version := "1.0.5"
ThisBuild / scalaVersion := "3.3.1"

val cpgVersion = "1.4.22"
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"downloadUrl": "https://github.com/AppThreat/chen",
"issueTracker": "https://github.com/AppThreat/chen/issues",
"name": "chen",
"version": "1.0.4",
"version": "1.0.5",
"description": "Code Hierarchy Exploration Net (chen) is an advanced exploration toolkit for your application source code and its dependency hierarchy.",
"applicationCategory": "code-analysis",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions console/src/main/scala/io/appthreat/console/Console.scala
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ class Console[T <: Project](
): String =
if tree then
val rootTree = richTreeLib.Tree(title, highlight = true)
atom.file.whereNot(_.name("<unknown>")).foreach { f =>
atom.file.whereNot(_.name("<(unknown|includes)>")).foreach { f =>
val childTree = richTreeLib.Tree(f.name, highlight = true)
f.method.foreach(m =>
val addedMethods = mutable.Map.empty[String, Boolean]
Expand Down Expand Up @@ -528,7 +528,7 @@ class Console[T <: Project](
val table = richTableLib.Table(title = title, highlight = true, show_lines = true)
table.add_column("File Name")
table.add_column("Methods")
atom.file.whereNot(_.name("<unknown>")).foreach { f =>
atom.file.whereNot(_.name("<(unknown|includes)>")).foreach { f =>
table.add_row(
f.name,
f.method.filterNot(m =>
Expand Down
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set version = "1.0.4" %}
{% set version = "1.0.5" %}

package:
name: chen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object FileDefaults:
val SOURCE_FILE_EXTENSIONS: Set[String] = Set(C_EXT, CC_EXT, CPP_EXT)

val HEADER_FILE_EXTENSIONS: Set[String] =
Set(C_HEADER_EXT, CPP_HEADER_EXT, OTHER_HEADER_EXT, ".h.in")
Set(C_HEADER_EXT, CPP_HEADER_EXT, OTHER_HEADER_EXT, ".h.in", ".tmh")

private val CPP_FILE_EXTENSIONS = Set(CC_EXT, CPP_EXT, CPP_HEADER_EXT, ".ccm", ".cxxm", ".c++m")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CdxPass(atom: Cpg) extends CpgPass(atom):
private def PY_REQUEST_PATTERNS = Array(".*views.py:<module>.*")

private def containsRegex(str: String) =
Pattern.quote(str) == str || str.contains("*") || str.contains("(") || str.contains(")")
Pattern.quote(str) != str || str.contains("*") || str.contains("(") || str.contains(")")

private val BOM_JSON_FILE = ".*(bom|cdx).json"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,40 @@ class ChennaiTagsPass(atom: Cpg) extends CpgPass(atom):
"django/(conf/)?urls.py:<module>.(path|re_path|url).*",
".*(route|web\\.|add_resource).*"
)

private def C_ROUTES_CALL_REGEXES = Array(
"Routes::(Post|Get|Delete|Head|Options|Put).*",
"API_CALL",
"API_CALL_ASYNC",
"ENDPOINT",
"ENDPOINT_ASYNC",
"ENDPOINT_INTERCEPTOR",
"ENDPOINT_INTERCEPTOR_ASYNC",
"registerHandler",
"PATH_ADD",
"ADD_METHOD_TO",
"ADD_METHOD_VIA_REGEX",
"WS_PATH_ADD",
"svr\\.(Post|Get|Delete|Head|Options|Put)"
)
private val PYTHON_ROUTES_DECORATORS_REGEXES = Array(
".*(route|endpoint|_request|require_http_methods|require_GET|require_POST|require_safe|_required)\\(.*",
".*def\\s(get|post|put)\\(.*"
)
private val HTTP_METHODS_REGEX = ".*(request|session)\\.(args|get|post|put|form).*"

private def tagCRoutes(dstGraph: DiffGraphBuilder): Unit =
C_ROUTES_CALL_REGEXES.foreach { r =>
atom.method.fullName(r).parameter.newTagNode(FRAMEWORK_INPUT).store()(
dstGraph
)
atom.call
.where(_.methodFullName(r))
.argument
.isLiteral
.newTagNode(FRAMEWORK_ROUTE)
.store()(dstGraph)
}
private def tagPythonRoutes(dstGraph: DiffGraphBuilder): Unit =
PYTHON_ROUTES_CALL_REGEXES.foreach { r =>
atom.call
Expand Down Expand Up @@ -64,6 +93,8 @@ class ChennaiTagsPass(atom: Cpg) extends CpgPass(atom):
override def run(dstGraph: DiffGraphBuilder): Unit =
if language == Languages.PYTHON || language == Languages.PYTHONSRC then
tagPythonRoutes(dstGraph)
if language == Languages.NEWC || language == Languages.C then
tagCRoutes(dstGraph)
atom.configFile("chennai.json").content.foreach { cdxData =>
val ctagsJson = parse(cdxData).getOrElse(Json.Null)
val cursor: HCursor = ctagsJson.hcursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,44 @@ class EasyTagsPass(atom: Cpg) extends CpgPass(atom):
if language == Languages.NEWC || language == Languages.C
then
atom.method.internal.name("main").parameter.newTagNode("cli-source").store()(dstGraph)
atom.method.internal.name("wmain").parameter.newTagNode("cli-source").store()(dstGraph)
atom.method.external.name("(cuda|curl_|BIO_).*").parameter.newTagNode(
"library-call"
).store()(
dstGraph
)
atom.method.external.name("DriverEntry").parameter.newTagNode("driver-source").store()(
dstGraph
)
atom.method.external.name("WdfDriverCreate").parameter.newTagNode(
"driver-source"
).store()(dstGraph)
atom.method.external.name("OnDeviceAdd").parameter.newTagNode(
"driver-source"
).store()(dstGraph)
atom.method.external.fullName(
"(Aws|Azure|google|cloud)(::|\\.).*"
).parameter.newTagNode(
"cloud"
).store()(dstGraph)
atom.method.external.fullName("(CDevice|CDriver)(::|\\.).*").parameter.newTagNode(
"device-driver"
).store()(dstGraph)
atom.method.external.fullName(
"(Windows|WEX|WDMAudio|winrt|wilEx)(::|\\.).*"
).parameter.newTagNode("windows").store()(dstGraph)
atom.method.external.fullName("(RpcServer)(::|\\.).*").parameter.newTagNode(
"rpc"
).store()(
dstGraph
)
atom.method.external.fullName(
"(Pistache|Http|Rest|oatpp|HttpClient|HttpRequest|WebSocketClient|HttpResponse|drogon|chrono|httplib)(::|\\.).*"
).parameter.newTagNode(
"http"
).store()(
dstGraph
)
end if
end run
end EasyTagsPass
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "appthreat-chen"
version = "1.0.4"
version = "1.0.5"
description = "Code Hierarchy Exploration Net (chen)"
authors = ["Team AppThreat <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 76fc17e

Please sign in to comment.