Skip to content

Commit ac89319

Browse files
committed
Refactor
1 parent b8023b4 commit ac89319

File tree

9 files changed

+422
-100
lines changed

9 files changed

+422
-100
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,38 @@ if err := client.Login("user", "pass"); err != nil {
2323
}
2424
defer client.Logout()
2525
```
26+
## Encode data with an encoder
27+
```golang
28+
encoded, err := client.Module.Encode("AAAA", "x86/shikata_ga_nai", &gomsf.EncodeOptions{
29+
Format: "c",
30+
})
31+
if err != nil {
32+
panic(err)
33+
}
34+
fmt.Printf("%s\n", encoded)
35+
```
36+
This will encode 'AAAA' with shikata_ga_nai, and prints the following c code:
37+
```bash
38+
unsigned char buf[] =
39+
"\xbb\xc6\xee\x4d\x66\xd9\xee\xd9\x74\x24\xf4\x58\x33\xc9\xb1"
40+
"\x02\x31\x58\x12\x83\xe8\xfc\x03\x9e\xe0\xaf\x93\x5f\xbc\x6e"
41+
"\x1d";
42+
```
43+
## Get infos about a module
44+
```golang
45+
info, err := client.Module.Info(gomsf.ExploitType, "windows/smb/ms08_067_netapi")
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
fmt.Printf("Name: %s\n", info.Name)
51+
fmt.Printf("Rank: %s\n", info.Rank)
52+
```
53+
This gives us the metadata of ms08_067_netapi
54+
```bash
55+
Name: MS08-067 Microsoft Server Service Relative Path Stack Corruption
56+
Rank: great
57+
```
2658

2759
## License
2860
[MIT](LICENCE)

_example/main.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,35 @@ func main() {
2828

2929
fmt.Printf("Version: %s\nRuby: %s\nAPI: %s\n\n", version.Version, version.Ruby, version.API)
3030

31-
// encodeResult, err := client.Module.Encode("AAAA", "x86/shikata_ga_nai", &rpc.EncodingOptions{
32-
// Format: "c",
33-
// })
34-
// if err != nil {
35-
// panic(err)
36-
// }
37-
38-
// fmt.Println("'AAAA' encoded with shikata_ga_nai:")
39-
// fmt.Printf("%s\n", encodeResult.Encoded)
31+
encoded, err := client.Module.Encode("AAAA", "x86/shikata_ga_nai", &gomsf.EncodeOptions{
32+
Format: "c",
33+
})
34+
if err != nil {
35+
panic(err)
36+
}
4037

41-
// infoResult, err := client.Module.Info(rpc.Exploit, "windows/smb/ms08_067_netapi")
42-
// if err != nil {
43-
// panic(err)
44-
// }
38+
fmt.Println("'AAAA' encoded with shikata_ga_nai:")
39+
fmt.Printf("%s\n", encoded)
4540

46-
// fmt.Printf("Name: %s\n", infoResult.Name)
47-
// fmt.Printf("Rank: %s\n", infoResult.Rank)
41+
exploit, err := client.Module.UseExploit("multi/handler")
42+
if err != nil {
43+
panic(err)
44+
}
4845

49-
// moduleOptions := rpc.NewModuleOptions()
50-
// moduleOptions.SetStringOption("LHOST", "0.0.0.0")
51-
// moduleOptions.SetIntOption("LPORT", 4444)
52-
// moduleOptions.SetStringOption("PAYLOAD", "generic/shell_reverse_tcp")
46+
fmt.Println(exploit.Options())
5347

54-
// executeResult, err := client.Module.Execute(rpc.Exploit, "multi/handler", moduleOptions)
48+
// payloads, err := exploit.Payloads()
5549
// if err != nil {
5650
// panic(err)
5751
// }
5852

59-
// fmt.Printf("JobID: %d\n", executeResult.JobID)
60-
// fmt.Printf("UUID: %s\n", executeResult.UUID)
53+
// fmt.Println(payloads)
54+
55+
info, err := client.Module.Info(gomsf.ExploitType, "windows/smb/ms08_067_netapi")
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
fmt.Printf("Name: %s\n", info.Name)
61+
fmt.Printf("Rank: %s\n", info.Rank)
6162
}

core.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package gomsf
22

3-
import "github.com/hupe1980/gomsf/rpc"
3+
import (
4+
"errors"
5+
6+
"github.com/hupe1980/gomsf/rpc"
7+
)
48

59
type CoreManager struct {
610
rpc *rpc.RPC
@@ -9,3 +13,17 @@ type CoreManager struct {
913
func (c *CoreManager) Version() (*rpc.CoreVersionRes, error) {
1014
return c.rpc.Core.Version()
1115
}
16+
17+
// Stop stops the core
18+
func (c *CoreManager) Stop() error {
19+
r, err := c.rpc.Core.Stop()
20+
if err != nil {
21+
return err
22+
}
23+
24+
if r.Result == rpc.FAILURE {
25+
return errors.New("cannot stop the core")
26+
}
27+
28+
return nil
29+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
)
1212

1313
require (
14+
github.com/fatih/structs v1.1.0
1415
github.com/stretchr/testify v1.7.0
1516
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
1617
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
4+
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

gomsf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type MSF struct {
2121
Health *HealthManager
2222
Plugins *PluginManager
2323
Jobs *JobManager
24+
Module *ModuleManager
2425
}
2526

2627
type MSFOptions struct {
@@ -65,6 +66,7 @@ func New(address string, optFns ...func(o *MSFOptions)) (*MSF, error) {
6566
Health: &HealthManager{rpc: rpc},
6667
Plugins: &PluginManager{rpc: rpc},
6768
Jobs: &JobManager{rpc: rpc},
69+
Module: &ModuleManager{rpc: rpc},
6870
}
6971

7072
return msf, nil

module.go

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package gomsf
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/fatih/structs"
7+
"github.com/hupe1980/gomsf/rpc"
8+
)
9+
10+
type ModuleType string
11+
12+
const (
13+
ExploitType ModuleType = "exploit"
14+
AuxiliaryType ModuleType = "auxiliary"
15+
PostType ModuleType = "post"
16+
PayloadType ModuleType = "payload"
17+
EvasionType ModuleType = "evasion"
18+
)
19+
20+
type module struct {
21+
rpc *rpc.RPC
22+
moduleType ModuleType
23+
moduleName string
24+
info *rpc.ModuleInfoRes
25+
options *rpc.ModuleOptionsRes
26+
}
27+
28+
func newModule(rpc *rpc.RPC, moduleType ModuleType, moduleName string) (*module, error) {
29+
info, err := rpc.Module.Info(string(moduleType), moduleName)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
options, err := rpc.Module.Options(string(moduleType), moduleName)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return &module{
40+
rpc: rpc,
41+
moduleType: moduleType,
42+
moduleName: moduleName,
43+
info: info,
44+
options: options,
45+
}, nil
46+
}
47+
48+
func (m *module) Options() []string {
49+
keys := make([]string, len(*m.options))
50+
for k := range *m.options {
51+
keys = append(keys, k)
52+
}
53+
54+
return keys
55+
}
56+
57+
type Exploit struct {
58+
*module
59+
}
60+
61+
func (e *Exploit) Payloads() ([]string, error) {
62+
r, err := e.rpc.Module.CompatiblePayloads(e.moduleName)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return r.Payloads, nil
68+
}
69+
70+
type Post struct {
71+
*module
72+
}
73+
74+
type Auxiliary struct {
75+
*module
76+
}
77+
78+
type Payload struct {
79+
*module
80+
}
81+
82+
type ModuleManager struct {
83+
rpc *rpc.RPC
84+
}
85+
86+
func (mm *ModuleManager) Architectures() (*rpc.ModuleArchitecturesRes, error) {
87+
return mm.rpc.Module.Architectures()
88+
}
89+
90+
func (mm *ModuleManager) Info(moduleType ModuleType, moduleName string) (*rpc.ModuleInfoRes, error) {
91+
return mm.rpc.Module.Info(string(moduleType), moduleName)
92+
}
93+
94+
func (mm *ModuleManager) Execute(moduleType ModuleType, moduleName string, options map[string]string) (*rpc.ModuleExecuteRes, error) {
95+
return mm.rpc.Module.Execute(string(moduleType), moduleName, options)
96+
}
97+
98+
type EncodeOptions struct {
99+
Format string `structs:"format,omitempty"` // Encoding format
100+
Badchars string `structs:"badchars,omitempty"` // Bad characters
101+
Platform string `structs:"platform,omitempty"` // Platform
102+
Arch string `structs:"arch,omitempty"` // Architecture
103+
ECount int `structs:"ecount,omitempty"` // Number of times to encode
104+
Inject bool `structs:"inject,omitempty"` // Enable injection
105+
Template string `structs:"template,omitempty"` // The template file (an executable)
106+
TemplatePath string `structs:"template_path,omitempty"` // Template path
107+
Addshellcode string `structs:"addshellcode,omitempty"` // Custom shellcode
108+
}
109+
110+
func (mm *ModuleManager) Encode(data string, encoderModule string, options *EncodeOptions) ([]byte, error) {
111+
sMap := structs.Map(options)
112+
113+
optMap := make(map[string]string)
114+
for k, v := range sMap {
115+
optMap[k] = fmt.Sprintf("%v", v)
116+
}
117+
118+
r, err := mm.rpc.Module.Encode(data, encoderModule, optMap)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
return r.Encoded, nil
124+
}
125+
126+
func (mm *ModuleManager) Exploits() ([]string, error) {
127+
r, err := mm.rpc.Module.Exploits()
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
return r.Modules, nil
133+
}
134+
135+
func (mm *ModuleManager) Evasions() ([]string, error) {
136+
r, err := mm.rpc.Module.Evasion()
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
return r.Modules, nil
142+
}
143+
144+
func (mm *ModuleManager) Payloads() ([]string, error) {
145+
r, err := mm.rpc.Module.Payloads()
146+
if err != nil {
147+
return nil, err
148+
}
149+
150+
return r.Modules, nil
151+
}
152+
153+
func (mm *ModuleManager) Auxiliaries() ([]string, error) {
154+
r, err := mm.rpc.Module.Auxiliary()
155+
if err != nil {
156+
return nil, err
157+
}
158+
159+
return r.Modules, nil
160+
}
161+
162+
func (mm *ModuleManager) Posts() ([]string, error) {
163+
r, err := mm.rpc.Module.Post()
164+
if err != nil {
165+
return nil, err
166+
}
167+
168+
return r.Modules, nil
169+
}
170+
171+
func (mm *ModuleManager) Nops() ([]string, error) {
172+
r, err := mm.rpc.Module.Nops()
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
return r.Modules, nil
178+
}
179+
180+
func (mm *ModuleManager) Encoders() ([]string, error) {
181+
r, err := mm.rpc.Module.Encoders()
182+
if err != nil {
183+
return nil, err
184+
}
185+
186+
return r.Modules, nil
187+
}
188+
189+
func (mm *ModuleManager) Platforms() (*rpc.ModulePlatformsRes, error) {
190+
return mm.rpc.Module.Platforms()
191+
}
192+
193+
func (mm *ModuleManager) UseExploit(moduleName string) (*Exploit, error) {
194+
m, err := newModule(mm.rpc, ExploitType, moduleName)
195+
if err != nil {
196+
return nil, err
197+
}
198+
199+
return &Exploit{
200+
module: m,
201+
}, nil
202+
}
203+
204+
func (mm *ModuleManager) UseAuxiliary(moduleName string) (*Auxiliary, error) {
205+
m, err := newModule(mm.rpc, AuxiliaryType, moduleName)
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
return &Auxiliary{
211+
module: m,
212+
}, nil
213+
}
214+
215+
func (mm *ModuleManager) UsePost(moduleName string) (*Post, error) {
216+
m, err := newModule(mm.rpc, PostType, moduleName)
217+
if err != nil {
218+
return nil, err
219+
}
220+
221+
return &Post{
222+
module: m,
223+
}, nil
224+
}

0 commit comments

Comments
 (0)