2024-08-03 00:45:27 -04:00
|
|
|
package profile
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/thunderbrewhq/binana/go/symfile"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (profile *Profile) generate_symbols_idc() (err error) {
|
|
|
|
|
|
|
|
|
|
var (
|
2024-08-09 07:15:03 -04:00
|
|
|
f *os.File
|
2024-08-03 00:45:27 -04:00
|
|
|
)
|
2024-08-09 07:15:03 -04:00
|
|
|
f, err = os.Create(filepath.Join(profile.Directory, "ida", "import_symbols.idc"))
|
2024-08-03 00:45:27 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, "#include <idc.idc>\n")
|
|
|
|
|
fmt.Fprintf(f, "\n")
|
|
|
|
|
fmt.Fprintf(f, "#include \"import_data_types.idc\"\n")
|
|
|
|
|
fmt.Fprintf(f, "static main() {\n")
|
|
|
|
|
fmt.Fprintf(f, " // Make names\n")
|
2024-08-03 00:45:27 -04:00
|
|
|
|
|
|
|
|
for _, symbol := range profile.SymbolTable.Entries {
|
|
|
|
|
quoted_name := strconv.Quote(symbol.Name)
|
|
|
|
|
address := fmt.Sprintf("0x%08X", symbol.StartAddress)
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " set_name(%s, %s);\n", address, quoted_name)
|
2024-08-03 00:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " // Make functions\n")
|
2024-08-03 00:45:27 -04:00
|
|
|
|
|
|
|
|
for _, function_symbol := range profile.SymbolTable.Entries {
|
|
|
|
|
if function_symbol.Kind == symfile.Function {
|
|
|
|
|
address := fmt.Sprintf("0x%08X", function_symbol.StartAddress)
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " set_func_start(%s, %s);\n", address, address)
|
2024-08-03 00:45:27 -04:00
|
|
|
if function_symbol.EndAddress != 0 {
|
|
|
|
|
end_address := fmt.Sprintf("0x%08X", function_symbol.EndAddress)
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " set_func_end(%s, %s);\n", address, end_address)
|
2024-08-03 00:45:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " // Apply data types\n")
|
|
|
|
|
fmt.Fprintf(f, " import_data_types();\n")
|
|
|
|
|
fmt.Fprintf(f, "}\n")
|
|
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
|
|
f, err = os.Create(filepath.Join(profile.Directory, "ida", "import_data_types.idc"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(f, "#include <idc.idc>\n")
|
|
|
|
|
fmt.Fprintf(f, "\n")
|
|
|
|
|
fmt.Fprintf(f, "static import_data_types() {\n")
|
2024-08-05 04:23:13 -04:00
|
|
|
|
|
|
|
|
for _, symbol := range profile.SymbolTable.Entries {
|
|
|
|
|
if symbol.DataType != "" {
|
|
|
|
|
quoted_data_type := strconv.Quote(symbol.DataType)
|
|
|
|
|
address := fmt.Sprintf("0x%08X", symbol.StartAddress)
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, " apply_type(%s, %s);\n", address, quoted_data_type)
|
2024-08-05 04:23:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-03 00:45:27 -04:00
|
|
|
|
2024-08-09 07:15:03 -04:00
|
|
|
fmt.Fprintf(f, "}\n")
|
2024-08-03 00:45:27 -04:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (profile *Profile) CreateIDAFiles() (err error) {
|
|
|
|
|
err = profile.generate_symbols_idc()
|
|
|
|
|
return
|
|
|
|
|
}
|