2024-07-17 01:37:26 -04:00
|
|
|
package profile
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-02-28 02:34:20 -05:00
|
|
|
"os"
|
2024-07-17 01:37:26 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
|
2026-02-28 02:34:20 -05:00
|
|
|
"github.com/thunderbrewhq/binana/go/symbols"
|
2024-07-17 01:37:26 -04:00
|
|
|
"github.com/thunderbrewhq/binana/go/x64dbg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func hex_address(u uint64) string {
|
|
|
|
|
return fmt.Sprintf("0x%x", u)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 02:34:20 -05:00
|
|
|
func compile_x64dbg_database(profile *Profile, compress bool) (err error) {
|
2024-07-17 01:37:26 -04:00
|
|
|
// Convert symbol table into x64dbg database
|
2024-11-27 01:55:46 -05:00
|
|
|
|
|
|
|
|
is_64bit := profile.Info.Arch == "amd64"
|
|
|
|
|
|
|
|
|
|
module_name := profile.Info.ModuleName
|
|
|
|
|
base_address := profile.Info.ModuleBase
|
|
|
|
|
|
2024-07-17 01:37:26 -04:00
|
|
|
var dd x64dbg.Database
|
|
|
|
|
|
2026-02-28 02:34:20 -05:00
|
|
|
for entry := range profile.Symbols.Entries() {
|
|
|
|
|
relative_start_address := entry.Symbol.StartAddress - base_address
|
2024-07-17 01:37:26 -04:00
|
|
|
relative_end_address := relative_start_address
|
|
|
|
|
|
2026-02-28 02:34:20 -05:00
|
|
|
if entry.Symbol.EndAddress != 0 {
|
|
|
|
|
relative_end_address = entry.Symbol.EndAddress - base_address
|
2024-07-17 01:37:26 -04:00
|
|
|
// for x64dbg, the end address is the last instruction.
|
|
|
|
|
// for us, the end address is the address immediately after the last instruction.
|
|
|
|
|
relative_end_address -= 1
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 17:40:52 -04:00
|
|
|
if relative_end_address < relative_start_address || relative_end_address-relative_start_address >= 50000 {
|
2026-02-28 02:34:20 -05:00
|
|
|
fmt.Printf("Strange symbol %s %08x %08x (offset %d)\n", entry.Symbol.Name, relative_start_address, relative_end_address, relative_end_address-relative_start_address)
|
2024-07-17 01:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create label
|
|
|
|
|
var label x64dbg.Label
|
|
|
|
|
label.Manual = true
|
|
|
|
|
label.Address = hex_address(relative_start_address)
|
2026-02-28 02:34:20 -05:00
|
|
|
label.Text = entry.Symbol.Name
|
2024-07-17 01:37:26 -04:00
|
|
|
label.Module = module_name
|
|
|
|
|
dd.Labels = append(dd.Labels, label)
|
|
|
|
|
|
2026-02-28 02:34:20 -05:00
|
|
|
if entry.Symbol.Kind == symbols.Function {
|
2024-07-17 01:37:26 -04:00
|
|
|
var fn x64dbg.Function
|
|
|
|
|
fn.Manual = true
|
|
|
|
|
fn.Start = hex_address(relative_start_address)
|
|
|
|
|
fn.End = hex_address(relative_end_address)
|
|
|
|
|
|
|
|
|
|
fn.Module = module_name
|
|
|
|
|
|
|
|
|
|
fn.InstructionCount = hex_address(0)
|
|
|
|
|
fn.Parent = hex_address(relative_start_address)
|
|
|
|
|
|
|
|
|
|
dd.Functions = append(dd.Functions, fn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 01:55:46 -05:00
|
|
|
filename := "game.dd32"
|
|
|
|
|
if is_64bit {
|
|
|
|
|
filename = "game.dd64"
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 01:37:26 -04:00
|
|
|
// save database
|
2026-02-28 02:34:20 -05:00
|
|
|
dd_path := filepath.Join(profile.ArtifactsDirectory, "x64dbg")
|
|
|
|
|
if err = os.MkdirAll(dd_path, 0755); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err = x64dbg.SaveDatabase(filepath.Join(dd_path, filename), &dd, compress); err != nil {
|
2024-07-17 01:37:26 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("database generated!", dd_path)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|