-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `JoernScan` * Set CPG version
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
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
78 changes: 78 additions & 0 deletions
78
joern-cli/src/main/scala/io/shiftleft/joern/JoernScan.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,78 @@ | ||
package io.shiftleft.joern | ||
|
||
import io.shiftleft.console.scan.{ScanPass, outputFindings} | ||
import io.shiftleft.console.{BridgeBase, DefaultArgumentProvider, JoernProduct, Query, QueryDatabase} | ||
import io.shiftleft.dataflowengineoss.queryengine.EngineContext | ||
import io.shiftleft.joern.console.AmmoniteBridge | ||
import io.shiftleft.semanticcpg.layers.{LayerCreator, LayerCreatorContext, LayerCreatorOptions} | ||
|
||
import scala.reflect.runtime.universe._ | ||
|
||
case class JoernScanConfig(src: String = "", overwrite: Boolean = false, store: Boolean = false) | ||
|
||
object JoernScan extends App with BridgeBase { | ||
|
||
def parseScanConfig(args: Array[String]): Option[JoernScanConfig] = { | ||
new scopt.OptionParser[JoernScanConfig]("joern-scan") { | ||
head("Scan code") | ||
help("help") | ||
arg[String]("src") | ||
.text("source code directory to scan") | ||
.action((x, c) => c.copy(src = x)) | ||
|
||
opt[Unit]("overwrite") | ||
.action((_, c) => c.copy(overwrite = true)) | ||
.text("Overwrite CPG if it already exists") | ||
|
||
opt[Unit]("store") | ||
.action((_, c) => c.copy(store = true)) | ||
.text("Store graph changes made by bundle") | ||
} | ||
}.parse(args, JoernScanConfig()) | ||
|
||
parseScanConfig(args).foreach { config => | ||
val shellConfig = io.shiftleft.console | ||
.Config() | ||
.copy(bundleToRun = Some("scan"), src = Some(config.src), overwrite = config.overwrite, store = config.store) | ||
runAmmonite(shellConfig, JoernProduct) | ||
} | ||
|
||
override protected def predefPlus(lines: List[String]) = AmmoniteBridge.predefPlus(lines) | ||
override protected def shutdownHooks = AmmoniteBridge.shutdownHooks | ||
override protected def promptStr() = AmmoniteBridge.promptStr() | ||
} | ||
|
||
object Scan { | ||
val overlayName = "scan" | ||
val description = "Joern Code Scanner" | ||
def defaultOpts = new ScanOptions() | ||
} | ||
|
||
class ScanOptions() extends LayerCreatorOptions {} | ||
|
||
class Scan(options: ScanOptions)(implicit engineContext: EngineContext) extends LayerCreator { | ||
|
||
override val overlayName: String = Scan.overlayName | ||
override val description: String = Scan.description | ||
|
||
override def create(context: LayerCreatorContext, storeUndoInfo: Boolean): Unit = { | ||
val queryDb = new QueryDatabase(new JoernDefaultArgumentProvider()) | ||
val allQueries: List[Query] = queryDb.allQueries | ||
if (allQueries.isEmpty) { | ||
println("You have not installed any query bundles") | ||
} | ||
runPass(new ScanPass(context.cpg, allQueries), context, storeUndoInfo) | ||
outputFindings(context.cpg) | ||
} | ||
} | ||
|
||
class JoernDefaultArgumentProvider(implicit context: EngineContext) extends DefaultArgumentProvider { | ||
|
||
override def defaultArgument(method: MethodSymbol, im: InstanceMirror, x: Symbol, i: Int): Option[Any] = { | ||
if (x.typeSignature.toString.endsWith("EngineContext")) { | ||
Some(context) | ||
} else { | ||
super.defaultArgument(method, im, x, i) | ||
} | ||
} | ||
} |
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,17 @@ | ||
#!/usr/bin/env sh | ||
|
||
if [ "$(uname -s)" = "Darwin" ]; then | ||
SCRIPT_ABS_PATH=$(greadlink -f "$0") | ||
else | ||
SCRIPT_ABS_PATH=$(readlink -f "$0") | ||
fi | ||
SCRIPT_ABS_DIR=$(dirname "$SCRIPT_ABS_PATH") | ||
SCRIPT="$SCRIPT_ABS_DIR"/bin/joern-scan | ||
|
||
if [ ! -f "$SCRIPT" ]; then | ||
echo "Unable to find $SCRIPT, have you created the distribution?" | ||
exit 1 | ||
fi | ||
|
||
$SCRIPT -J-XX:+UseG1GC -J-XX:CompressedClassSpaceSize=128m -Dlog4j.configurationFile="$SCRIPT_ABS_DIR"/conf/log4j2.xml -J-XX:+UseStringDeduplication "$@" | ||
|
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 @@ | ||
./joern-cli/target/universal/stage/joern-scan |