-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
61 lines (54 loc) · 1.53 KB
/
Copy pathexample_test.go
File metadata and controls
61 lines (54 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package resurgo_test
import (
"debug/elf"
"fmt"
"log"
"github.com/maxgio92/resurgo"
)
func ExampleDetectPrologues() {
// x86-64 machine code: nop; push rbp; mov rbp, rsp
code := []byte{0x90, 0x55, 0x48, 0x89, 0xe5}
prologues, err := resurgo.DetectPrologues(code, 0x1000, resurgo.ArchAMD64)
if err != nil {
log.Fatal(err)
}
for _, p := range prologues {
fmt.Printf("[%s] 0x%x: %s\n", p.Type, p.Address, p.Instructions)
}
// Output:
// [classic] 0x1001: push rbp; mov rbp, rsp
}
func ExampleDetectCallSites() {
// x86-64 machine code: call $+0x20 (E8 1B 00 00 00)
// At address 0x1000, calls target at 0x1000 + 5 + 0x1B = 0x1020
code := []byte{0xE8, 0x1B, 0x00, 0x00, 0x00}
edges, err := resurgo.DetectCallSites(code, 0x1000, resurgo.ArchAMD64)
if err != nil {
log.Fatal(err)
}
for _, e := range edges {
fmt.Printf("[%s] 0x%x -> 0x%x (%s, %s)\n",
e.Type, e.SourceAddr, e.TargetAddr, e.AddressMode, e.Confidence)
}
// Output:
// [call] 0x1000 -> 0x1020 (pc-relative, high)
}
func ExampleDetectFunctionsFromELF() {
f, err := elf.Open("/usr/bin/ls")
if err != nil {
log.Fatal(err)
}
defer f.Close()
candidates, err := resurgo.DetectFunctionsFromELF(f)
if err != nil {
log.Fatal(err)
}
// Count candidates by detection type.
counts := make(map[resurgo.DetectionType]int)
for _, c := range candidates {
counts[c.DetectionType]++
}
fmt.Printf("total: %d\n", len(candidates))
fmt.Printf("prologue+callsite: %d\n", counts[resurgo.DetectionPrologueCallSite])
fmt.Printf("cfi: %d\n", counts[resurgo.DetectionCFI])
}