From d86867f224d9947696bf1071f48ef9b094aee172 Mon Sep 17 00:00:00 2001 From: Michael Murphy Date: Tue, 3 Mar 2015 14:51:05 -0600 Subject: Add compression parameters Added manual compression, best, brute and ultra-brute compression flags. Manual compression is called with the -c flag and ranges from 1 through 9. -best, -brute and -ultra-brute call their repective UPX compression levels. --- main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 0461b68..8530940 100644 --- a/main.go +++ b/main.go @@ -2,26 +2,72 @@ package main import ( "flag" + "fmt" + "github.com/pwaller/goupx/hemfix" "log" "os" "os/exec" - - "github.com/pwaller/goupx/hemfix" ) const usageText = "usage: goupx [args...] path\n" -// usage prints some nice output instead of panic stacktrace when an user calls goupx without arguments +var ( + run_strip = flag.Bool("s", false, "run strip") + run_upx = flag.Bool("u", true, "run upx") + manual_compression = flag.Uint("c", 0, "Manual Compression Level: (1-9)") + best_compression = flag.Bool("best", false, "Best Compression") + brute_compression = flag.Bool("brute", false, "Brute Compression") + ultra_brute_compression = flag.Bool("ultra-brute", false, "Ultra Brute Compression") +) + +// usage prints some nice output instead of panic stacktrace when an user calls +// goupx without arguments func usage() { os.Stderr.WriteString(usageText) flag.PrintDefaults() } -func main() { +// checkCompressionLevel sets the compression level to 9 if it is higher than +// the maximum limit +func checkCompressionLevel() { + if *manual_compression > uint(9) { + *manual_compression = uint(9) + } +} - run_strip := flag.Bool("s", false, "run strip") - run_upx := flag.Bool("u", true, "run upx") +// stripBinary attempts to strip the binary. +func stripBinary(input_file string) { + cmd := exec.Command("strip", "-s", input_file) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Panic("strip failed: ", err) + } +} +// compressBinary attempts to compress the binary with UPX. +func compressBinary(input_file string) { + var cmd *exec.Cmd + if *best_compression { + cmd = exec.Command("upx", "--best", input_file) + } else if *brute_compression { + cmd = exec.Command("upx", "--brute", input_file) + } else if *ultra_brute_compression { + cmd = exec.Command("upx", "--ultra-brute", input_file) + } else if *manual_compression != 0 { + cmd = exec.Command("upx", + fmt.Sprintf("%s%d", "-", *manual_compression), input_file) + } else { + cmd = exec.Command("upx", input_file) + } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Panic("upx failed: ", err) + } +} + +func main() { flag.Parse() if flag.NArg() != 1 { @@ -29,6 +75,8 @@ func main() { return } + checkCompressionLevel() + defer func() { if err := recover(); err != nil { log.Print("Panicked. Giving up.") @@ -45,23 +93,10 @@ func main() { log.Print("File fixed!") if *run_strip { - cmd := exec.Command("strip", "-s", input_file) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - log.Panic("strip failed: ", err) - } + stripBinary(input_file) } if *run_upx { - cmd := exec.Command("upx", input_file) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - log.Panic("upx failed: ", err) - } + compressBinary(input_file) } - } -- cgit v1.2.3 From eccd895e70fd659988bdb422a7e0ab5aea272d08 Mon Sep 17 00:00:00 2001 From: Michael Murphy Date: Thu, 5 Mar 2015 14:58:33 -0600 Subject: Pass Arguments Directly Replaced the flag package with a custom implementation utilizing os.Args. I've also fixed the program so that it can compress multiple Go binaries. --- main.go | 123 +++++++++++++++++++++++++++++----------------------------------- 1 file changed, 55 insertions(+), 68 deletions(-) diff --git a/main.go b/main.go index 8530940..9aec87c 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,6 @@ package main import ( - "flag" - "fmt" "github.com/pwaller/goupx/hemfix" "log" "os" @@ -11,92 +9,81 @@ import ( const usageText = "usage: goupx [args...] path\n" -var ( - run_strip = flag.Bool("s", false, "run strip") - run_upx = flag.Bool("u", true, "run upx") - manual_compression = flag.Uint("c", 0, "Manual Compression Level: (1-9)") - best_compression = flag.Bool("best", false, "Best Compression") - brute_compression = flag.Bool("brute", false, "Brute Compression") - ultra_brute_compression = flag.Bool("ultra-brute", false, "Ultra Brute Compression") -) +var run_strip = false +var run_upx = true // usage prints some nice output instead of panic stacktrace when an user calls // goupx without arguments func usage() { os.Stderr.WriteString(usageText) - flag.PrintDefaults() -} - -// checkCompressionLevel sets the compression level to 9 if it is higher than -// the maximum limit -func checkCompressionLevel() { - if *manual_compression > uint(9) { - *manual_compression = uint(9) - } } -// stripBinary attempts to strip the binary. -func stripBinary(input_file string) { - cmd := exec.Command("strip", "-s", input_file) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - log.Panic("strip failed: ", err) +// parseArguments parses arguments from os.Args and separates the goupx flags +// from the UPX flags, as well as separating the files from the arguments. +func parseArguments() (args []string, files []string) { + args = append(args, "/usr/bin/upx") + for _, arg := range os.Args[1:] { + switch { + case arg == "-h" || arg == "--help": + usage() + case arg == "--no-upx": + run_upx = false + case arg == "--strip-binary": + run_strip = true + case arg[0] != '-': + files = append(files, arg) + default: + args = append(args, arg) + } } + return } // compressBinary attempts to compress the binary with UPX. -func compressBinary(input_file string) { - var cmd *exec.Cmd - if *best_compression { - cmd = exec.Command("upx", "--best", input_file) - } else if *brute_compression { - cmd = exec.Command("upx", "--brute", input_file) - } else if *ultra_brute_compression { - cmd = exec.Command("upx", "--ultra-brute", input_file) - } else if *manual_compression != 0 { - cmd = exec.Command("upx", - fmt.Sprintf("%s%d", "-", *manual_compression), input_file) - } else { - cmd = exec.Command("upx", input_file) - } - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - log.Panic("upx failed: ", err) +func compressBinary(input_file string, arguments []string) { + if run_upx { + cmd := &exec.Cmd{ + Path: "/usr/bin/upx", + Args: append(arguments, input_file), + Stdout: os.Stdout, + Stderr: os.Stderr, + } + if err := cmd.Run(); err != nil { + log.Panic("upx failed: ", err) + } } } -func main() { - flag.Parse() - - if flag.NArg() != 1 { - usage() - return - } - - checkCompressionLevel() - - defer func() { - if err := recover(); err != nil { - log.Print("Panicked. Giving up.") - panic(err) - return +// stripBinary attempts to strip the binary. +func stripBinary(input_file string) { + if run_strip { + cmd := exec.Command("strip", "-s", input_file) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Panic("strip failed: ", err) } - }() + } +} - input_file := flag.Arg(0) - err := hemfix.FixFile(input_file) - if err != nil { +// runHemfix will attempt to fix the current input file. +func runHemfix(input_file string) { + if err := hemfix.FixFile(input_file); err != nil { log.Panicf("Failed to fix '%s': %v", input_file, err) } log.Print("File fixed!") +} - if *run_strip { - stripBinary(input_file) +func main() { + arguments, files := parseArguments() + for _, file := range files { + runHemfix(file) + stripBinary(file) + compressBinary(file, arguments) } - - if *run_upx { - compressBinary(input_file) + if err := recover(); err != nil { + log.Print("Panicked. Giving up.") + panic(err) + return } } -- cgit v1.2.3 From 5af74f3f95db38568a7584d8bd784ec1aca2e700 Mon Sep 17 00:00:00 2001 From: Michael Murphy Date: Thu, 5 Mar 2015 15:13:42 -0600 Subject: Improve usageText --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 9aec87c..ee3e59d 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,12 @@ import ( "os/exec" ) -const usageText = "usage: goupx [args...] path\n" +const usageText = `usage: goupx [args...] files...\n + + --no-upx: Disables UPX from running. + --strip-binary: Strips binaries before compressing them. + +See UPX's documentation (man upx) for information on UPX's flags.` var run_strip = false var run_upx = true -- cgit v1.2.3 From a3deb458f548979660c0e630003a7046ef7870c8 Mon Sep 17 00:00:00 2001 From: Michael Murphy Date: Thu, 5 Mar 2015 15:17:21 -0600 Subject: Remove \n from usageText --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index ee3e59d..ff0a982 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "os/exec" ) -const usageText = `usage: goupx [args...] files...\n +const usageText = `usage: goupx [args...] files... --no-upx: Disables UPX from running. --strip-binary: Strips binaries before compressing them. -- cgit v1.2.3 From 235c2406bb1d7f5c928348aeebdac38a7673f4fb Mon Sep 17 00:00:00 2001 From: Michael Murphy Date: Thu, 5 Mar 2015 15:52:06 -0600 Subject: Add Empty Argument Check Print usage if no arguments are given. --- main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ff0a982..40fb246 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,8 @@ const usageText = `usage: goupx [args...] files... --no-upx: Disables UPX from running. --strip-binary: Strips binaries before compressing them. -See UPX's documentation (man upx) for information on UPX's flags.` +See UPX's documentation (man upx) for information on UPX's flags. +` var run_strip = false var run_upx = true @@ -26,6 +27,9 @@ func usage() { // parseArguments parses arguments from os.Args and separates the goupx flags // from the UPX flags, as well as separating the files from the arguments. func parseArguments() (args []string, files []string) { + if len(os.Args) == 1 { + usage() + } args = append(args, "/usr/bin/upx") for _, arg := range os.Args[1:] { switch { @@ -48,11 +52,11 @@ func parseArguments() (args []string, files []string) { func compressBinary(input_file string, arguments []string) { if run_upx { cmd := &exec.Cmd{ - Path: "/usr/bin/upx", - Args: append(arguments, input_file), - Stdout: os.Stdout, - Stderr: os.Stderr, + Path: "/usr/bin/upx", + Args: append(arguments, input_file), } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Panic("upx failed: ", err) } -- cgit v1.2.3