Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reverse a stack trace of a executable file that was built with -seed=random? #696

Closed
nghia-nguyen-ts opened this issue Mar 9, 2023 · 3 comments · Fixed by #713
Closed

Comments

@nghia-nguyen-ts
Copy link

nghia-nguyen-ts commented Mar 9, 2023

What version of Garble and Go are you using?

$ garble version
mvdan.cc/garble v0.9.3

Build settings:
      -buildmode exe
       -compiler gc
     CGO_ENABLED 1
          GOARCH arm64
            GOOS darwin
$ go version
go version go1.20.1 darwin/arm64

What environment are you running Garble on?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/nghianguyen/Library/Caches/go-build"
GOENV="/Users/nghianguyen/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/nghianguyen/go/pkg/mod"
GONOPROXY="github.com/tsocial/*"
GONOSUMDB="github.com/tsocial/*"
GOOS="darwin"
GOPATH="/Users/nghianguyen/go"
GOPRIVATE="github.com/tsocial/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/nghianguyen/Desktop/playground/garble/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/26/qs1y2gzs5xl1sslkyxvbzv_80000gr/T/go-build2983500078=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

  1. Build an executable file by: garble build -seed=random main.go, the main.go has some panic inside and prints stack trace.
  2. Run main executable file and copy output to stack.txt.
  3. garble reverse main.go stack.txt.
  4. garble -seed=random reverse main.go stack.txt.

What did you expect to see?

Step 3 and 4 print the un-obfuscated stack trace.

What did you see instead?

Step 3. nor 4. prints the un-obfuscated stack trace.

Question

garble -seed=X reverse main.go stack.txt would prints the un-obfuscated stack trace if X is the value of the random seed used by step 1, right? So can we print the random seed upon garble -seed=random build for later reverse usage?

@lu4p
Copy link
Member

lu4p commented Mar 9, 2023

Yes you are correct if you want to reverse panics you need to save the random seed.

@lu4p lu4p closed this as completed Mar 9, 2023
@nghia-nguyen-ts
Copy link
Author

But garble doesn't print the random seed it uses? How do I get the random seed?

@lu4p lu4p reopened this Mar 9, 2023
@lu4p
Copy link
Member

lu4p commented Mar 9, 2023

garble/main.go

Line 247 in 059c1d6

fmt.Fprintf(os.Stderr, "random seed: %s\n", base64.RawStdEncoding.EncodeToString(flagSeed.bytes))

You're right the seed is only printed when an error occured, I think it got printed in all cases before. We should definitely fix this.

mvdan added a commit to mvdan/garble-fork that referenced this issue Apr 9, 2023
The seedFlag.random field had never worked,
as my refactor in December 2021 never set it to true.

Even if the boolean was working, we only printed the random seed
when we failed. It's still useful to see it when a build succeeds,
for example when wanting to reproduce the same binary
or when wanting to reverse a panic from the produced binary.

Add a test this time.

Fixes burrowers#696.
@mvdan mvdan closed this as completed in #713 Apr 9, 2023
mvdan added a commit that referenced this issue Apr 9, 2023
The seedFlag.random field had never worked,
as my refactor in December 2021 never set it to true.

Even if the boolean was working, we only printed the random seed
when we failed. It's still useful to see it when a build succeeds,
for example when wanting to reproduce the same binary
or when wanting to reverse a panic from the produced binary.

Add a test this time.

Fixes #696.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants