Skip to content

Commit

Permalink
Ensure HOME is set, remove large literal ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
moloch-- committed Jan 20, 2023
1 parent 7eb77b0 commit 1469482
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions server/gogo/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"

"github.com/bishopfox/sliver/server/log"
"github.com/shirou/gopsutil/v3/mem"
)

const (
Expand Down Expand Up @@ -81,19 +80,13 @@ func GetGoModCache(appDir string) string {
return cachePath
}

// The Gb limit here is somewhat arbitrary but is based on my own testing
func garbleMaxLiteralSize() string {
vmStat, err := mem.VirtualMemory()
if err != nil {
gogoLog.Errorf("Failed to detect amount of system memory: %s", err)
return fmt.Sprintf("%d", 1*kb) // Use default
}
if 10*gb < vmStat.Total {
gogoLog.Infof("More than 10Gb of system memory, enable large literal obfuscation")
return fmt.Sprintf("%d", 64*kb)
// Garble requires $HOME to be defined, if it's not set we use the os temp dir
func getHomeDir() string {
home := os.Getenv("HOME")
if home == "" {
return os.TempDir()
}
gogoLog.Infof("Low system memory, disable large literal obfuscation")
return fmt.Sprintf("%d", 2*kb)
return home
}

// GarbleCmd - Execute a go command
Expand All @@ -103,7 +96,7 @@ func GarbleCmd(config GoConfig, cwd string, command []string) ([]byte, error) {
return nil, fmt.Errorf(fmt.Sprintf("Invalid compiler target: %s", target))
}
garbleBinPath := filepath.Join(config.GOROOT, "bin", "garble")
garbleFlags := []string{"-seed=random", "-literals"}
garbleFlags := []string{"-seed=random", "-literals", "-tiny"}
command = append(garbleFlags, command...)
cmd := exec.Command(garbleBinPath, command...)
cmd.Dir = cwd
Expand All @@ -116,11 +109,11 @@ func GarbleCmd(config GoConfig, cwd string, command []string) ([]byte, error) {
fmt.Sprintf("GOCACHE=%s", config.GOCACHE),
fmt.Sprintf("GOMODCACHE=%s", config.GOMODCACHE),
fmt.Sprintf("GOPROXY=%s", config.GOPROXY),
fmt.Sprintf("GARBLE_MAX_LITERAL_SIZE=%s", garbleMaxLiteralSize()),
fmt.Sprintf("HTTP_PROXY=%s", config.HTTPPROXY),
fmt.Sprintf("HTTPS_PROXY=%s", config.HTTPSPROXY),
fmt.Sprintf("PATH=%s:%s", filepath.Join(config.GOROOT, "bin"), os.Getenv("PATH")),
fmt.Sprintf("GOGARBLE=%s", config.GOGARBLE),
fmt.Sprintf("HOME=%s", getHomeDir()),
}
var stdout bytes.Buffer
var stderr bytes.Buffer
Expand Down

0 comments on commit 1469482

Please sign in to comment.