Skip to content

Commit

Permalink
feat: add /syms/rescan route to ipswd to allow and IPSW to be res…
Browse files Browse the repository at this point in the history
…canned w/ say NEW symbolicator signatures etc
  • Loading branch information
blacktop committed Sep 25, 2024
1 parent fda8c8e commit d221d58
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
58 changes: 58 additions & 0 deletions api/server/routes/syms/syms.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,64 @@ func AddRoutes(rg *gin.RouterGroup, db db.Database, pemDB, sigsDir string) {
}
c.JSON(http.StatusOK, successResponse{Success: true})
})
// swagger:route POST /syms/rescan Syms postRescan
//
// Rescan
//
// Rescan symbols for a given IPSW.
//
// Produces:
// - application/json
//
// Parameters:
// + name: path
// in: query
// description: path to IPSW
// required: true
// type: string
// + name: pem_db
// in: query
// description: path to AEA pem DB JSON file
// required: false
// type: string
// + name: sig_dir
// in: query
// description: path to symbolication signatures directory
// required: false
// type: string
// Responses:
// 200: successResponse
// 500: genericError
rg.POST("/syms/rescan", func(c *gin.Context) {
ipswPath, ok := c.GetQuery("path")
if !ok {
c.AbortWithStatusJSON(http.StatusBadRequest, types.GenericError{Error: "missing path query parameter"})
return
} else {
ipswPath = filepath.Clean(ipswPath)
}
pemDbPath, ok := c.GetQuery("pem_db")
if ok {
pemDbPath = filepath.Clean(pemDbPath)
} else {
if pemDB != "" {
pemDbPath = filepath.Clean(pemDB)
}
}
signaturesDir, ok := c.GetQuery("sig_dir")
if ok {
signaturesDir = filepath.Clean(signaturesDir)
} else {
if sigsDir != "" {
signaturesDir = filepath.Clean(sigsDir)
}
}
if err := syms.Rescan(ipswPath, pemDbPath, signaturesDir, db); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}
c.JSON(http.StatusOK, successResponse{Success: true})
})
// swagger:route GET /syms/ipsw Syms getIPSW
//
// IPSW
Expand Down
61 changes: 61 additions & 0 deletions internal/syms/syms.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,67 @@ func Scan(ipswPath, pemDB, sigsDir string, db db.Database) (err error) {
return db.Save(ipsw)
}

// Rescan re-scans the IPSW file and extracts information about the kernels, DSCs, and file system.
func Rescan(ipswPath, pemDB, sigsDir string, db db.Database) (err error) {
/* IPSW */
sha1, err := utils.Sha1(ipswPath)
if err != nil {
return fmt.Errorf("failed to calculate sha1: %w", err)
}
ipsw, err := db.Get(sha1)
if err != nil {
return fmt.Errorf("failed to get IPSW from database: %w", err)
}
/* KERNEL */
if ipsw.Kernels, err = scanKernels(ipswPath, sigsDir); err != nil {
return fmt.Errorf("failed to scan kernels: %w", err)
}
/* DSC */
if ipsw.DSCs, err = scanDSCs(ipswPath, pemDB); err != nil {
return fmt.Errorf("failed to scan DSCs: %w", err)
}
/* FileSystem */
if err := search.ForEachMachoInIPSW(ipswPath, pemDB, func(path string, m *macho.File) error {
if m.UUID() != nil {
mm := &model.Macho{
UUID: m.UUID().String(),
Path: model.Path{Path: path},
}
if text := m.Segment("__TEXT"); text != nil {
mm.TextStart = text.Addr
mm.TextEnd = text.Addr + text.Filesz
}
for _, fn := range m.GetFunctions() {
var msym *model.Symbol
if syms, err := m.FindAddressSymbols(fn.StartAddr); err == nil {
for _, sym := range syms {
fn.Name = sym.Name
}
msym = &model.Symbol{
Name: model.Name{Name: fn.Name},
Start: fn.StartAddr,
End: fn.EndAddr,
}
} else {
msym = &model.Symbol{
Name: model.Name{Name: fmt.Sprintf("func_%x", fn.StartAddr)},
Start: fn.StartAddr,
End: fn.EndAddr,
}
}
mm.Symbols = append(mm.Symbols, msym)
}
ipsw.FileSystem = append(ipsw.FileSystem, mm)
}
return nil
}); err != nil {
return fmt.Errorf("failed to search for machos in IPSW: %w", err)
}

log.Debug("Saving IPSW with FileSystem")
return db.Save(ipsw)
}

func GetIPSW(version, build, device string, db db.Database) (*model.Ipsw, error) {
return db.GetIPSW(version, build, device)
}
Expand Down

0 comments on commit d221d58

Please sign in to comment.