-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch.go
91 lines (82 loc) · 1.58 KB
/
patch.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"flag"
"io"
"log"
"os"
)
const (
IP_PATTERN string = "XXXXXXXXXXXXXXX"
PORT_PATTERN string = "OOOOO"
)
func main() {
// flag
bin := flag.String("b", "", "the target binary name.")
ip := flag.String("i", "", "Specified IP.")
port := flag.String("p", "", "Specified port.")
flag.Parse()
// target binary
file, err := os.Open(*bin)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// create patched file
PatchedFile, err := os.Create(*bin + ".patched")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Read, search, and write
w := bufio.NewWriter(PatchedFile)
r := bufio.NewReader(file)
for true {
b, err := r.ReadByte()
if err == io.EOF {
break
}
if b == 'X' {
bs, err := r.Peek(len(IP_PATTERN) - 1)
if err == nil {
verified := true
for _, v := range bs {
if v != 'X' {
verified = false
break
}
}
if verified {
w.WriteString(*ip)
for i := len(*ip); i < len(IP_PATTERN); i++ {
w.WriteByte('\x00')
}
r.Discard(len(IP_PATTERN) - 1)
log.Println("IP pattern found.")
continue
}
}
} else if b == 'O' {
bs, err := r.Peek(len(PORT_PATTERN) - 1)
if err == nil {
verified := true
for _, v := range bs {
if v != 'O' {
verified = false
break
}
}
if verified {
w.WriteString(*port)
for i := len(*port); i < len(PORT_PATTERN); i++ {
w.WriteByte('\x00')
}
r.Discard(len(PORT_PATTERN) - 1)
log.Println("Port pattern found.")
continue
}
}
}
w.WriteByte(b)
}
}