-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
201 lines (189 loc) · 5.24 KB
/
main.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
package main
import (
"flag"
"io/ioutil"
"log"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
)
var (
logFile = flag.String("l", "/tmp/attempts.log", "Log file to log SSH attempts")
errFile = flag.String("e", "/tmp/shellsForShits.log", "Log file to log errors")
bindAddr = flag.String("b", "", "Address to bind to, if nil, bind all")
portNum = flag.String("p", "22", "Port to bind to")
keyFile = flag.String("k", "/tmp/hostkey", "Host keyfile for the server")
remoteServer = flag.String("s", "ds063140.mongolab.com:63140", "mongodb server")
mong_user = flag.String("user", "", "mongodb user")
mong_pass = flag.String("pass", "", "mongodb password")
db = flag.String("db", "sshforshits", "mongodb database")
pcoll = flag.String("pcoll", "pwns", "mongodb shell activity collection")
acoll = flag.String("acoll", "attempts", "mongodb login attempts collection")
versionBanner = flag.String("v", "SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u2", "Banner to present")
logger *log.Logger
errLog *log.Logger
hostPrivateKeySigner ssh.Signer
activityClient *shellActivityClient
attemptChan chan attempt
)
func init() {
flag.Parse()
if *remoteServer == "" {
log.Panic("Invalid API server url")
}
if *mong_user == "" || *mong_pass == "" || *db == "" || *pcoll == "" || *acoll == "" {
log.Panic("Empty mongo info")
}
if *logFile != "" {
fout, err := os.OpenFile(*logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
logger = log.New(fout, "", log.LstdFlags)
} else {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
if *errFile != "" {
fout, err := os.OpenFile(*errFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
errLog = log.New(fout, "", log.LstdFlags)
} else {
errLog = log.New(ioutil.Discard, "", log.LstdFlags)
}
hostPrivateKey, err := ioutil.ReadFile(*keyFile)
if err != nil {
panic(err)
}
hostPrivateKeySigner, err = ssh.ParsePrivateKey(hostPrivateKey)
if err != nil {
panic(err)
}
attemptChan = make(chan attempt, 16)
}
func passAuth(conn ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
var host string
var err error
if host, _, err = net.SplitHostPort(conn.RemoteAddr().String()); err != nil {
host = conn.RemoteAddr().String()
}
logger.Printf("%s %s:%s\n", host, conn.User(), string(pass))
creds := map[string]string{}
creds["username"] = conn.User()
creds["password"] = string(pass)
perm := ssh.Permissions{
CriticalOptions: nil,
Extensions: creds,
}
attemptChan <- attempt{
User: conn.User(),
Pass: string(pass),
TS: time.Now().Format(time.RFC3339Nano),
Origin: conn.RemoteAddr().String(),
}
return &perm, nil
}
func main() {
var err error
var port string
config := ssh.ServerConfig{
PasswordCallback: passAuth,
ServerVersion: *versionBanner,
}
config.AddHostKey(hostPrivateKeySigner)
if *portNum == "" {
port = "2222"
} else {
port = *portNum
}
hnd := NewHandler()
registerCommands(hnd)
activityClient, err = NewShellActivityClient(*remoteServer, *db, *pcoll, *acoll, *mong_user, *mong_pass)
if err != nil {
panic(err)
}
if err = activityClient.Login(); err != nil {
errLog.Printf("Failed to login, all writes will be cached until we can actually login")
errLog.Printf("Error: \"%v\"\n", err)
}
go attempter(activityClient)
socket, err := net.Listen("tcp", *bindAddr+":"+port)
if err != nil {
panic(err)
}
defer socket.Close()
for {
conn, err := socket.Accept()
if err != nil {
continue
}
// From a standard TCP connection to an encrypted SSH connection
sshConn, chans, reqs, err := ssh.NewServerConn(conn, &config)
if err != nil {
conn.Close()
continue
}
if sshConn.Permissions == nil {
errLog.Printf("Failed to get ssh permissions\n")
sshConn.Close()
conn.Close()
continue
}
if sshConn.Permissions.Extensions == nil {
errLog.Printf("ssh permissions extensions are nil")
sshConn.Close()
conn.Close()
continue
}
username, ok := sshConn.Permissions.Extensions["username"]
if !ok {
errLog.Printf("ssh permission extensions is missing the username")
sshConn.Close()
conn.Close()
continue
}
password, ok := sshConn.Permissions.Extensions["password"]
if !ok {
errLog.Printf("ssh permission extensions is missing the password")
sshConn.Close()
conn.Close()
continue
}
dg := datagram{
Login: time.Now().Format(time.RFC3339Nano),
Src: sshConn.Conn.RemoteAddr().String(),
Dst: sshConn.Conn.LocalAddr().String(),
User: username,
Pass: password,
}
go mainHandler(sshConn, chans, reqs, hnd, dg)
}
}
func attempter(sac *shellActivityClient) {
var ats []attempt
for at := range attemptChan {
if err := sac.WriteAttempt(at); err != nil {
ats = append(ats, at)
if err = sac.Login(); err != nil {
time.Sleep(10 * time.Second)
continue
}
}
//a write worked, try to clear the backlog
i := 0
wloop:
for i = 0; i < len(ats); i++ {
if err := sac.WriteAttempt(ats[i]); err != nil {
break wloop
}
}
if i == len(ats) {
//we got everything out
ats = nil
} else {
ats = ats[i:len(ats)]
}
}
}