forked from hupe1980/gomsf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
124 lines (97 loc) · 2.28 KB
/
console.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
package gomsf
import (
"fmt"
"strings"
"github.com/deranged0tter/gomsf/rpc"
)
type Console struct {
id string
rpc *rpc.RPC
}
// newConsole initializes a msf console
func newConsole(rpc *rpc.RPC) (*Console, error) {
r, err := rpc.Console.Create()
if err != nil {
return nil, err
}
return &Console{
id: r.ID,
rpc: rpc,
}, nil
}
// Read reads data from the console
func (c *Console) Read() (*rpc.ConsoleReadRes, error) {
return c.rpc.Console.Read(c.id)
}
// Write writes data to the console.
func (c *Console) Write(command string) error {
if !strings.HasSuffix(command, "\n") {
command = fmt.Sprintf("%s\n", command)
}
if _, err := c.rpc.Console.Write(c.id, command); err != nil {
return err
}
return nil
}
// SessionKill kills all active meterpreter or shell sessions
func (c *Console) SessionKill() error {
r, err := c.rpc.Console.SessionKill(c.id)
if err != nil {
return err
}
if r.Result == rpc.FAILURE {
return fmt.Errorf("cannot kill sessions for console %s", c.id)
}
return nil
}
// SessionDetach detachs the current meterpreter or shell session
func (c *Console) SessionDetach() error {
r, err := c.rpc.Console.SessionDetach(c.id)
if err != nil {
return err
}
if r.Result == rpc.FAILURE {
return fmt.Errorf("cannot detatch session for %s", c.id)
}
return nil
}
func (c *Console) Tabs(line string) ([]string, error) {
r, err := c.rpc.Console.Tabs(c.id, line)
if err != nil {
return nil, err
}
return r.Tabs, nil
}
// Destroy destroys the console
func (c *Console) Destroy() error {
r, err := c.rpc.Console.Destroy(c.id)
if err != nil {
return err
}
if r.Result == rpc.FAILURE {
return fmt.Errorf("cannot destroy console %s", c.id)
}
return nil
}
type ConsoleManager struct {
rpc *rpc.RPC
}
// List lists active consoles
func (cm *ConsoleManager) List() (*rpc.ConsoleListRes, error) {
return cm.rpc.Console.List()
}
// Console creates a new framework console instance
func (cm *ConsoleManager) Console() (*Console, error) {
return newConsole(cm.rpc)
}
// Destroy destroys an active console
func (cm *ConsoleManager) Destroy(consoleID string) error {
r, err := cm.rpc.Console.Destroy(consoleID)
if err != nil {
return err
}
if r.Result == rpc.FAILURE {
return fmt.Errorf("cannot destroy console %s", consoleID)
}
return nil
}