From 0031a3612a735332d242afe6075aa65c075317b8 Mon Sep 17 00:00:00 2001 From: Gaulthier Gain Date: Tue, 18 May 2021 17:07:41 +0200 Subject: [PATCH] Add analysis argument to further handle the analysis Signed-off-by: Gaulthier Gain --- srcs/dependtool/args.go | 22 +++++++++++++++------- srcs/dependtool/run_deptool.go | 29 ++++++++++++++++++++--------- srcs/dependtool/static_analyser.go | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/srcs/dependtool/args.go b/srcs/dependtool/args.go index e06cc9d..8cba529 100644 --- a/srcs/dependtool/args.go +++ b/srcs/dependtool/args.go @@ -13,13 +13,15 @@ import ( ) const ( - programArg = "program" - testFileArg = "testFile" - configFileArg = "configFile" - optionsArg = "options" - waitTimeArg = "waitTime" - saveOutputArg = "saveOutput" - fullDepsArg = "fullDeps" + programArg = "program" + testFileArg = "testFile" + configFileArg = "configFile" + optionsArg = "options" + waitTimeArg = "waitTime" + saveOutputArg = "saveOutput" + fullDepsArg = "fullDeps" + fullStaticAnalysis = "fullStaticAnalysis" + typeAnalysis = "typeAnalysis" ) // parseLocalArguments parses arguments of the application. @@ -45,6 +47,12 @@ func parseLocalArguments(p *argparse.Parser, args *u.Arguments) error { args.InitArgParse(p, args, u.BOOL, "", fullDepsArg, &argparse.Options{Required: false, Default: false, Help: "Show dependencies of dependencies"}) + args.InitArgParse(p, args, u.BOOL, "", fullStaticAnalysis, + &argparse.Options{Required: false, Default: false, + Help: "Full static analysis (analyse shared libraries too)"}) + args.InitArgParse(p, args, u.INT, "", typeAnalysis, + &argparse.Options{Required: false, Default: 0, + Help: "Kind of analysis (0: both; 1: static; 2: dynamic)"}) return u.ParserWrapper(p, os.Args) } diff --git a/srcs/dependtool/run_deptool.go b/srcs/dependtool/run_deptool.go index d044ed4..fbf0d80 100644 --- a/srcs/dependtool/run_deptool.go +++ b/srcs/dependtool/run_deptool.go @@ -1,6 +1,7 @@ package dependtool import ( + "errors" "fmt" "github.com/fatih/color" "runtime" @@ -26,6 +27,12 @@ func RunAnalyserTool(homeDir string, data *u.Data) { u.PrintErr(err) } + // Get the kind of analysis (0: both; 1: static; 2: dynamic) + typeAnalysis := *args.IntArg[typeAnalysis] + if typeAnalysis < 0 || typeAnalysis > 2 { + u.PrintErr(errors.New("analysis argument must be between [0,2]")) + } + // Get program path programPath, err := u.GetProgramPath(&*args.StringArg[programArg]) if err != nil { @@ -51,17 +58,21 @@ func RunAnalyserTool(homeDir string, data *u.Data) { checkMachOS(&programPath) } - // Run static analyser - u.PrintHeader1("(1.1) RUN STATIC ANALYSIS") - runStaticAnalyser(args, programName, programPath, outFolder, data) + if typeAnalysis == 0 || typeAnalysis == 1 { + // Run static analyser + u.PrintHeader1("(1.1) RUN STATIC ANALYSIS") + runStaticAnalyser(args, programName, programPath, outFolder, data) + } // Run dynamic analyser - if strings.ToLower(runtime.GOOS) == "linux" { - u.PrintHeader1("(1.2) RUN DYNAMIC ANALYSIS") - runDynamicAnalyser(args, programName, programPath, outFolder, data) - } else { - // dtruss/dtrace on mac needs to disable system integrity protection - u.PrintWarning("Dynamic analysis is not currently supported on macOS") + if typeAnalysis == 0 || typeAnalysis == 2 { + if strings.ToLower(runtime.GOOS) == "linux" { + u.PrintHeader1("(1.2) RUN DYNAMIC ANALYSIS") + runDynamicAnalyser(args, programName, programPath, outFolder, data) + } else { + // dtruss/dtrace on mac needs to disable system integrity protection + u.PrintWarning("Dynamic analysis is not currently supported on macOS") + } } // Save Data to JSON diff --git a/srcs/dependtool/static_analyser.go b/srcs/dependtool/static_analyser.go index 766a4a3..2a40575 100644 --- a/srcs/dependtool/static_analyser.go +++ b/srcs/dependtool/static_analyser.go @@ -181,6 +181,7 @@ func staticAnalyser(args u.Arguments, data *u.Data, programPath string) { programName := *args.StringArg[programArg] fullDeps := *args.BoolArg[fullDepsArg] + fullStaticAnalysis := *args.BoolArg[fullStaticAnalysis] staticData := &data.StaticData @@ -222,6 +223,25 @@ func staticAnalyser(args u.Arguments, data *u.Data, programPath string) { } } + // Detect symbols from shared libraries + if fullStaticAnalysis { + u.PrintHeader2("(*) Gathering symbols and system calls of shared libraries from binary file") + for key, path := range staticData.SharedLibs { + if len(path) > 0 { + fmt.Printf("\t-> Analysing %s - %s\n", key, path[0]) + if err := gatherStaticSymbols(path[0], staticData); err != nil { + u.PrintWarning(err) + } + if err := gatherStaticSystemCalls(path[0], "-D", staticData); err != nil { + // Check without the dynamic argument + if err := gatherStaticSystemCalls(path[0], "", staticData); err != nil { + u.PrintWarning(err) + } + } + } + } + } + if strings.ToLower(runtime.GOOS) == "linux" { // Gather Data from apt-cache u.PrintHeader2("(*) Gathering dependencies from apt-cache depends")