-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathforwardserverport.go
140 lines (110 loc) · 3.05 KB
/
forwardserverport.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
package handlers
import (
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/NHAS/reverse_ssh/internal"
"github.com/NHAS/reverse_ssh/internal/server/multiplexer"
"github.com/NHAS/reverse_ssh/internal/server/users"
"github.com/NHAS/reverse_ssh/pkg/logger"
"golang.org/x/crypto/ssh"
)
var (
currentRemoteForwardsLck sync.RWMutex
currentRemoteForwards = map[string]string{}
remoteForwards = map[string]ssh.Channel{}
)
type chanAddress struct {
Port uint32
IP string
}
func (c *chanAddress) Network() string {
return "remote_forward_tcp"
}
func (c *chanAddress) String() string {
return net.JoinHostPort(c.IP, fmt.Sprintf("%d", c.Port))
}
type chanConn struct {
channel ssh.Channel
localAddr chanAddress
remoteAddr chanAddress
}
func (c *chanConn) Read(b []byte) (n int, err error) {
return c.channel.Read(b)
}
func (c *chanConn) Write(b []byte) (n int, err error) {
return c.channel.Write(b)
}
func (c *chanConn) Close() error {
return c.channel.Close()
}
func (c *chanConn) LocalAddr() net.Addr {
return &c.localAddr
}
func (c *chanConn) RemoteAddr() net.Addr {
return &c.remoteAddr
}
func (c *chanConn) SetDeadline(t time.Time) error {
return errors.New("not implemented on a channel")
}
func (c *chanConn) SetReadDeadline(t time.Time) error {
return errors.New("not implemented on a channel")
}
func (c *chanConn) SetWriteDeadline(t time.Time) error {
return errors.New("not implemented on a channel")
}
func channelToConn(channel ssh.Channel, drtMsg internal.ChannelOpenDirectMsg) net.Conn {
return &chanConn{
channel: channel,
localAddr: chanAddress{
Port: drtMsg.Lport,
IP: drtMsg.Raddr,
},
remoteAddr: chanAddress{
Port: drtMsg.Rport,
IP: drtMsg.Raddr,
},
}
}
func ServerPortForward(clientId string) func(_ string, _ *users.User, newChannel ssh.NewChannel, log logger.Logger) {
return func(_ string, _ *users.User, newChannel ssh.NewChannel, log logger.Logger) {
a := newChannel.ExtraData()
var drtMsg internal.ChannelOpenDirectMsg
err := ssh.Unmarshal(a, &drtMsg)
if err != nil {
log.Warning("Unable to unmarshal proxy %s", err)
newChannel.Reject(ssh.ResourceShortage, "Unable to unmarshal proxy")
return
}
connection, requests, err := newChannel.Accept()
if err != nil {
newChannel.Reject(ssh.ResourceShortage, "nope")
log.Warning("Unable to accept new channel %s", err)
return
}
go func() {
for req := range requests {
if req.WantReply {
req.Reply(false, nil)
}
}
StopRemoteForward(clientId)
}()
currentRemoteForwardsLck.Lock()
remoteForwards[clientId] = connection
currentRemoteForwards[clientId] = net.JoinHostPort(drtMsg.Raddr, fmt.Sprintf("%d", drtMsg.Rport))
currentRemoteForwardsLck.Unlock()
multiplexer.ServerMultiplexer.QueueConn(channelToConn(connection, drtMsg))
}
}
func StopRemoteForward(clientId string) {
currentRemoteForwardsLck.Lock()
defer currentRemoteForwardsLck.Unlock()
if remoteForwards[clientId] != nil {
remoteForwards[clientId].Close()
}
delete(remoteForwards, clientId)
delete(currentRemoteForwards, clientId)
}