forked from Binject/shellcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepo.go
74 lines (67 loc) · 1.66 KB
/
repo.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
package shellcode
import (
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/sanitycheck/binject-shellcode/api"
)
// Repo - Shellcode Repository, directory-backed
type Repo struct {
Directory string
}
// NewRepo - create a new Repo object, if dirName is provided, directory structure will be created if it doesn't exist
func NewRepo(dirName string) *Repo {
repo := new(Repo)
if dirName != "" {
repo.Directory = dirName
if err := initShellcodeDir(dirName); err != nil {
log.Fatal(err)
}
}
return repo
}
func initShellcodeDir(dirName string) error {
if !DirExists(dirName) {
if err := os.Mkdir(dirName, os.FileMode(int(0755))); err != nil {
return err
}
}
for _, ose := range api.Oses {
osDir := filepath.Join(dirName, ose)
if !DirExists(osDir) {
if err := os.Mkdir(osDir, os.FileMode(int(0755))); err != nil {
return err
}
}
for _, arch := range api.Arches {
archDir := filepath.Join(osDir, arch)
if !DirExists(archDir) {
if err := os.Mkdir(archDir, os.FileMode(int(0755))); err != nil {
return err
}
}
}
}
return nil
}
// Lookup - fetches a completed shellcode from the filesystem
func (r *Repo) Lookup(os api.Os, arch api.Arch, pattern string) ([]byte, error) {
if r.Directory != "" {
// check specific directory first
dir := filepath.Join(r.Directory, string(os), string(arch))
if DirExists(dir) {
files, err := WalkMatch(dir, pattern)
if err != nil {
return nil, err
}
if len(files) > 0 {
return ioutil.ReadFile(files[0])
}
}
// todo: fallback from intel32 or 64 to 32y64
}
//todo: lookup in the compiled-in modules
return nil, errors.New("No Matching Shellcode Found")
}