forked from blacknon/go-sshlib
/
example_multiproxy_shell.go
103 lines (84 loc) · 1.99 KB
/
example_multiproxy_shell.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
// Copyright (c) 2021 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Shell connection Example file.
// Change the value of the variable and compile to make sure that you can actually connect.
//
// This file has a simple ssh proxy connection.
// Also, the authentication method is password authentication.
// Please replace as appropriate.
package main
import (
"fmt"
"os"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)
var (
// http proxy server
httpProxyHost = "http-proxy.com"
httpProxyPort = "4444"
// socks5 proxy server
socks5ProxyHost = "socks5-proxy.com"
socks5ProxyPort = "5555"
// ssh target server
host = "target.com"
port = "22"
user = "user"
password = "password"
termlog = "./test_termlog"
)
func main() {
// ==========
// http proxy connect
// ==========
httpProxy := &sshlib.Proxy{
Type: "http",
Addr: httpProxyHost,
Port: httpProxyPort,
}
httpProxyDialer, err := httpProxy.CreateProxyDialer()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// ==========
// socks5 proxy connect
// ==========
socks5Proxy := &sshlib.Proxy{
Type: "socks5",
Addr: socks5ProxyHost,
Port: socks5ProxyPort,
Forwarder: httpProxyDialer,
}
socks5ProxyDialer, err := httpProxy.CreateProxyDialer()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// ==========
// target connect
// ==========
// Create target sshlib.Connect
targetCon := &sshlib.Connect{
ProxyDialer: socks5ProxyDialer,
}
// Create target ssh.AuthMethod
authMethod := sshlib.CreateAuthMethodPassword(password)
// Connect target server
err = targetCon.CreateClient(host, user, port, []ssh.AuthMethod{authMethod})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Set terminal log
targetCon.SetLog(termlog, false)
// Create Session
session, err := targetCon.CreateSession()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Start ssh shell
targetCon.Shell(session)
}