B2R2-org / B2R2

B2R2 is a collection of useful algorithms, functions, and tools for binary analysis.
https://b2r2.org
MIT License
413 stars 62 forks source link

I want to lift .so file but an error occurs #49

Closed sanghwa95 closed 6 months ago

sanghwa95 commented 6 months ago

Describe the bug I want to lift a .so file which is from Android APK file. But an error occurs. Other common .so files also occur the same issue. How can I lift .so file?

To Reproduce Steps to reproduce the behavior:

  1. I used F# script below

open B2R2 open B2R2.FrontEnd

[] let main argv = let isa = ISA.OfString "armv8a64" let handler = BinHandler.Init (isa, "libapp.so") let ins = BinHandler.ParseInstr handler 0UL ins.Translate handler.TranslationContext |> printfn "%A" 0

  1. $ dotnet build
  2. $ dotnet run
  3. Build is successful but an error occurs like below.

Screenshots image

Environment (please complete the following information):

sangkilc commented 6 months ago

Thanks for the report. Can you share your .so file somewhere?

sanghwa95 commented 6 months ago

https://github.com/sanghwa95/tmp/blob/main/libapp.so Above is so file link. Thank you for quick response.

sangkilc commented 6 months ago

I see two problems:

  1. You are giving the address 0 to ParseInstr, which is an invalid address. You may want to lift only the .text section. For instance:

    let isa = ISA.OfString "aarch64"
    let hdl = BinHandle.Init (isa, "libapp.so")
    let sec = hdl.BinFile.GetSections (name=".text") |> Seq.exactlyOne
    let ins = BinHandle.ParseInstr (hdl, sec.Address)
  2. Even with the above code, you will see an error because the text section starts with an invalid instruction. You should be able to confirm this using objdump or similar tools. A valid address starts @ 0x120068. So the following code will work, for instance.

    let isa = ISA.OfString "aarch64"
    let hdl = BinHandle.Init (isa, "libapp.so")
    let sec = hdl.BinFile.GetSections (name=".text") |> Seq.exactlyOne
    let ins = BinHandle.ParseInstr (hdl, sec.Address + 0x68UL)
    ins.Translate hdl.TranslationContext
    |> Pp.stmtsToString
    |> printfn "%s"

So this is not really a bug. You should gracefully handle parsing exceptions for invalid instructions in order to properly lift binaries.

sanghwa95 commented 6 months ago

Thanks for your kind reply!