-
Notifications
You must be signed in to change notification settings - Fork 64
/
main_test.go
168 lines (157 loc) · 3.53 KB
/
main_test.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"log"
"path/filepath"
"testing"
"github.com/Binject/binjection/bj"
"github.com/Binject/shellcode"
)
func Test_Tar_Elf_Inject_1(t *testing.T) {
// init test shellcode repo
repo := shellcode.NewRepo("test")
config := &bj.BinjectConfig{Repo: repo, CodeCaveMode: false, InjectionMethod: bj.SilvioInject}
// Open test tar file with uninjected Elf in it
b, err := ioutil.ReadFile(filepath.Join("test", "testelf.tar"))
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
wet, err := Inject(buf, config)
if err != nil {
t.Fatal(err)
}
// Open up resulting tar and check for injection
tr := tar.NewReader(wet)
for {
// Read a file from input
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
f := hdr.FileInfo()
if f.IsDir() {
continue
}
if f.Name() == "static_ls" {
data, err := ioutil.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
data2, err := ioutil.ReadFile(filepath.Join("test", "static_ls_hello_injected"))
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data, data2) != 0 {
log.Fatal("Results don't match")
}
break
}
log.Fatal("static_ls not found")
}
}
func Test_Tgz_Elf_Inject_1(t *testing.T) {
// init test shellcode repo
repo := shellcode.NewRepo("test")
config := &bj.BinjectConfig{Repo: repo, CodeCaveMode: false, InjectionMethod: bj.SilvioInject}
// Open test tar file with uninjected Elf in it
b, err := ioutil.ReadFile(filepath.Join("test", "testelf.tgz"))
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
wet, err := Inject(buf, config)
if err != nil {
t.Fatal(err)
}
// Open up resulting tgz and check for injection
zr, err := gzip.NewReader(wet)
if err != nil {
t.Fatal(err)
}
defer zr.Close()
tr := tar.NewReader(zr)
for {
// Read a file from input
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
f := hdr.FileInfo()
if f.IsDir() {
continue
}
if f.Name() == "static_ls" {
data, err := ioutil.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
data2, err := ioutil.ReadFile(filepath.Join("test", "static_ls_hello_injected"))
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data, data2) != 0 {
log.Fatal("Results don't match")
}
break
}
log.Fatal("static_ls not found")
}
}
func Test_Zip_Elf_Inject_1(t *testing.T) {
// init test shellcode repo
repo := shellcode.NewRepo("test")
config := &bj.BinjectConfig{Repo: repo, CodeCaveMode: false, InjectionMethod: bj.SilvioInject}
// Open test tar file with uninjected Elf in it
b, err := ioutil.ReadFile(filepath.Join("test", "testelf.zip"))
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
wet, err := Inject(buf, config)
if err != nil {
t.Fatal(err)
}
// Open up resulting zip and check for injection
zr, err := zip.NewReader(bytes.NewReader(wet.Bytes()), int64(wet.Len()))
if err != nil {
t.Fatal(err)
}
for _, file := range zr.File {
// Read a file from input
if file.Mode().IsDir() {
continue
}
if file.Name == "static_ls" {
f, err := file.Open()
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
t.Fatal(err)
}
data2, err := ioutil.ReadFile(filepath.Join("test", "static_ls_hello_injected"))
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data, data2) != 0 {
log.Fatal("Results don't match")
}
break
}
log.Fatal("static_ls not found")
}
}