-
Notifications
You must be signed in to change notification settings - Fork 1
/
mRemoteParser.go
228 lines (193 loc) · 5.57 KB
/
mRemoteParser.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base64"
"encoding/xml"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/schollz/closestmatch"
"golang.org/x/crypto/pbkdf2"
)
/** The shared secret seems to be build into mRemote */
const sharedSecret = "mR3m"
var fileName = flag.String("f", "", "The config file containing the connections")
var listConnections = flag.Bool("l", false, "List all connections")
var printPassword = flag.Bool("p", false, "Print password of connection")
var execCommand = flag.String("c", "", "Execute a single command")
type Container struct {
Name string `xml:"Name,attr"`
Nodes []Node `xml:"Node"`
}
type Node struct {
XMLName xml.Name `xml:"Node"`
Type string `xml:"Type,attr"`
Username string `xml:"Username,attr"`
Password string `xml:"Password,attr"`
Hostname string `xml:"Hostname,attr"`
HomeDir string `xml:"UserField,attr"`
Port string `xml:"Port,attr"`
Container
}
type Connection struct {
Path string
Node
}
type ConnectionConfig struct {
XMLName xml.Name `xml:"Connections"`
Container
}
func (config ConnectionConfig) String() (str string) {
str = fmt.Sprintf("Config file: %s\n", config.Name)
for _, node := range config.Nodes {
str += node.String() + "\n"
}
return
}
func (node Node) String() (str string) {
if node.Type == "Connection" {
encPassword, _ := DecodePassword(node.Password)
str += fmt.Sprintf("%s -> %s@%s:%s Password: %s", node.Name, node.Username, node.Hostname, node.HomeDir, encPassword)
} else if node.Type == "Container" {
str += fmt.Sprintf("%s\n", node.Name)
}
for _, subNode := range node.Nodes {
str += subNode.String() + "\n"
}
return
}
func DecodePassword(base64DecodedEncryptedPassword string) (decodedPassword string, err error) {
if len(base64DecodedEncryptedPassword) == 0 {
return "", errors.New("password is empty")
}
encryptedPassword, err := base64.StdEncoding.DecodeString(base64DecodedEncryptedPassword)
if err != nil {
return "", err
}
salt := encryptedPassword[:aes.BlockSize]
nonce := encryptedPassword[aes.BlockSize:(2 * aes.BlockSize)]
cipherText := encryptedPassword[(2 * aes.BlockSize):]
key := pbkdf2.Key([]byte(sharedSecret), []byte(salt), 1000, 32, sha1.New)
cipherBlock, err := aes.NewCipher(key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCMWithNonceSize(cipherBlock, 16)
if err != nil {
return "", err
}
plaintext, err := gcm.Open(nil, nonce, cipherText, []byte(salt))
if err != nil {
return "", err
}
decodedPassword = string(plaintext)
return
}
func (node Container) FillConnectionMap(connections []Connection, path string) (newConnections []Connection) {
newConnections = connections
for _, subNode := range node.Nodes {
if subNode.Type == "Connection" {
var connection Connection
connection.Node = subNode
connection.Path = path + " " + subNode.Name
newConnections = append(newConnections, connection)
} else {
newPath := path + " " + subNode.Name
newConnections = subNode.FillConnectionMap(newConnections, newPath)
}
}
return newConnections
}
func buildDict(connections []Connection) (dict []string) {
for _, node := range connections {
dict = append(dict, node.Name)
}
return
}
func (config ConnectionConfig) closestMatch(query string) (node Node) {
connections := config.FillConnectionMap([]Connection{}, "")
dict := buildDict(connections)
bagSize := []int{2, 3, 4}
cm := closestmatch.New(dict, bagSize)
match := cm.Closest(strings.ToLower(query))
for _, connection := range connections {
if connection.Name == match {
node = connection.Node
return
}
}
panic("Could not find any connection")
}
func (node Node) ConnectCommand() error {
return node.ExecCommand("bash")
}
func (node Node) ExecCommand(command string) error {
if len(node.Password) == 0 {
cmd := exec.Command("ssh", "-o", "StrictHostKeyChecking=no", "-p", node.Port, "-t", node.Username+"@"+node.Hostname, "cd "+node.HomeDir+"; "+command)
return interactiveConsole(cmd)
}
password, err := DecodePassword(node.Password)
if err != nil {
fmt.Printf("Could not decode password: %v\n", err)
return err
}
cmd := exec.Command("sshpass", "-p", password, "ssh", "-o", "StrictHostKeyChecking=no", "-p", node.Port, "-t", node.Username+"@"+node.Hostname, "cd "+node.HomeDir+"; "+command)
return interactiveConsole(cmd)
}
func interactiveConsole(cmd *exec.Cmd) error {
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
return err
}
func main() {
flag.Parse()
if fileName == nil || len(*fileName) == 0 {
*fileName = os.Getenv("MREMOTE_CONFIG_FILE")
}
data, err := ioutil.ReadFile(*fileName)
if err != nil {
fmt.Printf("Could not read file '%s': %v\n", *fileName, err)
return
}
var config ConnectionConfig
xmlErr := xml.Unmarshal([]byte(data), &config)
if xmlErr != nil {
fmt.Printf("Error while parsing XML config: %v\n", xmlErr)
return
}
if *listConnections {
fmt.Println(config)
return
}
connectQuery := strings.Join(flag.Args(), " ")
if len(connectQuery) > 0 {
node := config.closestMatch(connectQuery)
fmt.Printf("Connection: %v\n", node)
if *printPassword {
password, err := DecodePassword(node.Password)
if err != nil {
fmt.Printf("Could not decode password: %v\n", err)
} else {
fmt.Printf("Password: %v\n", password)
}
} else if len(*execCommand) > 0 {
err := node.ExecCommand(*execCommand)
if err != nil {
fmt.Printf("Connection failed: %v\n", err)
}
} else {
err := node.ConnectCommand()
if err != nil {
fmt.Printf("Connection failed: %v\n", err)
}
}
}
}