diff --git a/cmd/cmd.go b/cmd/cmd.go index 67adf84..989cbde 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -52,7 +52,9 @@ const ( proxyFlagStr = "proxy" // Compile - Standard Flags - outputFlagStr = "output" + outputFlagStr = "output" + allCodeFlagStr = "all" + verboseFlagStr = "verbose" // Compile - Obfuscation Flags bcfFlagStr = "bcf" @@ -97,6 +99,8 @@ func init() { // Compile - Standard options compileCmd.Flags().StringP(outputFlagStr, "o", "", "output file") + compileCmd.Flags().BoolP(allCodeFlagStr, "a", false, "obfuscate all code including nim stdlib") + compileCmd.Flags().BoolP(verboseFlagStr, "v", false, "display verbose information") rootCmd.AddCommand(compileCmd) } diff --git a/cmd/compile.go b/cmd/compile.go index 4f0466c..66514e9 100644 --- a/cmd/compile.go +++ b/cmd/compile.go @@ -34,22 +34,43 @@ var compileCmd = &cobra.Command{ if !preflight() { return } - if len(args) < 1 { fmt.Printf(Warn + "Missing input files\n") return } - obfArgs, err := getObfArgs(cmd) + allCode, err := cmd.Flags().GetBool(allCodeFlagStr) + if err != nil { + fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", allCodeFlagStr, err) + return + } + output, err := cmd.Flags().GetString(outputFlagStr) + if err != nil { + fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", outputFlagStr, err) + return + } + verbose, err := cmd.Flags().GetBool(verboseFlagStr) if err != nil { + fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", verboseFlagStr, err) return } buildArgs := &build.Build{ - Name: filepath.Base(args[0]), - NimFiles: args, - UserCodeOnly: true, + Name: filepath.Base(args[0]), + NimFiles: args, + Output: output, + ObfAllCode: allCode, + Verbose: verbose, + } + + obfArgs, err := getObfArgs(cmd) + if err != nil { + return + } + + err = build.Compile(buildArgs, obfArgs) + if err != nil { + fmt.Printf(Warn+"%s", err) } - build.Compile(buildArgs, obfArgs) }, } diff --git a/make.bat b/make.bat index 4badb87..d4b019c 100644 --- a/make.bat +++ b/make.bat @@ -5,7 +5,7 @@ @echo off SETLOCAL -SET VERSION=0.0.1 +SET VERSION=0.0.2 SET CMD_PKG=github.com/moloch--/denim/cmd SET LDFLAGS=-s -w diff --git a/pkg/build/build.go b/pkg/build/build.go index 3f9be2e..7410349 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -31,7 +31,10 @@ type Build struct { Name string NimFiles []string - UserCodeOnly bool + Output string + ObfAllCode bool + + Verbose bool } // Compile a nim program with Obfuscator-LLVM @@ -42,7 +45,7 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error { } // Compile Nim - nimCache, err := compileNimCode(build.Name, build.NimFiles, clang) + nimCache, err := compileNimCode(build, clang) if err != nil { return err } @@ -56,17 +59,30 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error { if len(step) != 2 { return fmt.Errorf("Malformed step: %v", step) } + cFile := filepath.Base(step[0]) compileCmd := strings.Fields(step[1]) if compileCmd[0] == "clang" || compileCmd[0] == "clang.exe" { compileCmd = compileCmd[1:] } + + var stdout []byte + var stderr []byte var err error - if strings.HasPrefix(cFile, "@") || !build.UserCodeOnly { - _, _, err = clang.ObfCompile(nimCache, compileCmd, obfArgs) + if strings.HasPrefix(cFile, "@") || build.ObfAllCode { + stdout, stderr, err = clang.ObfCompile(nimCache, compileCmd, obfArgs) } else { - _, _, err = clang.Compile(nimCache, compileCmd) + stdout, stderr, err = clang.Compile(nimCache, compileCmd) + } + if build.Verbose { + if 0 < len(stdout) { + fmt.Printf(string(stdout)) + } + if 0 < len(stderr) { + fmt.Printf(string(stderr)) + } } + if err != nil { return err } @@ -75,7 +91,15 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error { if linker[0] == "clang" || linker[0] == "clang.exe" { linker = linker[1:] } - _, _, err = clang.Compile(nimCache, linker) + stdout, stderr, err := clang.Compile(nimCache, linker) + if build.Verbose { + if 0 < len(stdout) { + fmt.Printf(string(stdout)) + } + if 0 < len(stderr) { + fmt.Printf(string(stderr)) + } + } if err != nil { return err } @@ -84,14 +108,27 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error { } // nim compile --genScript --compileOnly --cc=clang --clang.exe:PATH --nimcache:PATH helloworld.nim -func compileNimCode(project string, nimFiles []string, clang *ollvm.Clang) (string, error) { - nimCache := filepath.Join(assets.GetNimCacheRoot(), project) +func compileNimCode(build *Build, clang *ollvm.Clang) (string, error) { + nimCache := filepath.Join(assets.GetNimCacheRoot(), build.Name) args := []string{"--genScript", "--compileOnly", "--cc:clang"} args = append(args, fmt.Sprintf("--clang.exe=%s", clang.ClangExe)) args = append(args, fmt.Sprintf("--nimcache:%s", nimCache)) - args = append(args, nimFiles...) + if build.Output != "" { + args = append(args, fmt.Sprintf("--out:%s", build.Output)) + } + args = append(args, build.NimFiles...) + workDir, _ := os.Getwd() - _, _, err := nim.Compile(workDir, os.Environ(), args) + stdout, stderr, err := nim.Compile(workDir, os.Environ(), args) + if build.Verbose { + if 0 < len(stdout) { + fmt.Printf(string(stdout)) + } + if 0 < len(stderr) { + fmt.Printf(string(stderr)) + } + } + return nimCache, err }