@@ -6,11 +6,14 @@ import (
6
6
"bytes"
7
7
"encoding/base64"
8
8
"encoding/json"
9
+ "errors"
9
10
"fmt"
10
11
"github.com/MythicAgents/poseidon/Payload_Type/poseidon/agent_code/pkg/responses"
12
+ "github.com/MythicAgents/poseidon/Payload_Type/poseidon/agent_code/pkg/utils"
11
13
"github.com/MythicAgents/poseidon/Payload_Type/poseidon/agent_code/pkg/utils/structs"
12
14
"io"
13
15
"net"
16
+ "os"
14
17
"strconv"
15
18
"strings"
16
19
"time"
@@ -313,9 +316,9 @@ func connectToUDPProxy(channelId uint32, toMythicSocksChannel chan structs.Socks
313
316
}
314
317
//fmt.Printf("read destination from udp proxy message: %s\n", dest.Address())
315
318
316
- target , err := net .Dial ("udp4" , dest . Address () )
319
+ localListen , err := net .ListenUDP ("udp4" , nil )
317
320
if err != nil {
318
- fmt .Printf ("failed to connect to udp proxy : %v\n " , err )
321
+ utils . PrintDebug ( fmt .Sprintf ("failed to start listening for future responses : %v\n " , err ) )
319
322
msg := structs.SocksMsg {
320
323
ServerId : channelId ,
321
324
Exit : true ,
@@ -324,35 +327,39 @@ func connectToUDPProxy(channelId uint32, toMythicSocksChannel chan structs.Socks
324
327
return
325
328
}
326
329
//fmt.Printf("have %d bytes left to write\n", r.Len())
327
-
328
- _ , err = r .WriteTo (target )
330
+ _ , err = localListen . WriteToUDP ( data [ len ( data ) - r . Len ():], & net. UDPAddr { IP : dest . IP , Port : dest . Port })
331
+ // _, err = r.WriteTo(target)
329
332
if err != nil {
330
- // fmt.Printf ("failed to write to udp: %v\n", err)
333
+ utils . PrintDebug ( fmt .Sprintf ("failed to write to udp: %v\n " , err ) )
331
334
msg := structs.SocksMsg {
332
335
ServerId : channelId ,
333
336
Exit : true ,
334
337
}
335
338
toMythicSocksChannel <- msg
336
339
return
337
340
}
338
- //fmt.Printf("wrote to %d udp proxy message\n", written )
341
+ //fmt.Printf("wrote %d bytes to udp proxy message\n", bytesLeft )
339
342
recvChan := make (chan structs.SocksMsg , 200 )
340
343
addToMapChan <- addToMap {
341
344
ChannelID : channelId ,
342
- Connection : target ,
345
+ Connection : localListen ,
343
346
NewChannel : recvChan ,
344
347
}
345
- go writeToUDPProxy (recvChan , target , channelId , toMythicSocksChannel )
348
+ go writeToUDPProxy (recvChan , localListen , channelId , toMythicSocksChannel )
346
349
for {
347
350
bufIn := make ([]byte , 4096 )
348
351
//fmt.Printf("about to read from udp proxy message\n")
349
- err = target .SetReadDeadline (time .Now ().Add (5 * time .Second ))
352
+ err = localListen .SetReadDeadline (time .Now ().Add (5 * time .Second ))
350
353
if err != nil {
351
- fmt .Printf ("failed to set read deadline: %v\n " , err )
354
+ utils .PrintDebug (fmt .Sprintf ("failed to set read deadline: %v\n " , err ))
355
+ }
356
+ readLength , _ , err := localListen .ReadFromUDP (bufIn )
357
+ if errors .Is (err , os .ErrDeadlineExceeded ) {
358
+ //utils.PrintDebug(fmt.Sprintf("read deadline exceeded\n"))
359
+ continue
352
360
}
353
- readLength , err := target .Read (bufIn )
354
361
if err != nil {
355
- // fmt.Printf ("failed to read from udp: %v\n", err)
362
+ utils . PrintDebug ( fmt .Sprintf ("failed to read from udp: %v\n " , err ) )
356
363
msg := structs.SocksMsg {
357
364
ServerId : channelId ,
358
365
Exit : true ,
@@ -361,6 +368,7 @@ func connectToUDPProxy(channelId uint32, toMythicSocksChannel chan structs.Socks
361
368
removeFromMapChan <- channelId
362
369
return
363
370
}
371
+ //fmt.Printf("remoteAddr: %v\n", remoteAddr)
364
372
if readLength > 0 {
365
373
msg := structs.SocksMsg {}
366
374
msg .ServerId = channelId
@@ -443,22 +451,25 @@ func writeToUDPProxy(recvChan chan structs.SocksMsg, conn net.Conn, channelId ui
443
451
w := bufio .NewWriter (conn )
444
452
for bufOut := range recvChan {
445
453
// Send a response back to person contacting us.
454
+ //fmt.Printf("got message to send to udp proxy: %v\n", bufOut.Data)
446
455
if bufOut .Exit {
447
456
w .Flush ()
457
+ //fmt.Printf("got exit from mythic\n")
448
458
removeFromMapChan <- channelId
449
459
return
450
460
}
451
461
data , err := base64 .StdEncoding .DecodeString (bufOut .Data )
452
462
if err != nil {
453
463
w .Flush ()
464
+ utils .PrintDebug (fmt .Sprintf ("error decoding data from mythic: %v\n " , err ))
454
465
removeFromMapChan <- channelId
455
466
return
456
467
}
457
468
458
469
r := bytes .NewReader (data )
459
470
header := []byte {0 , 0 , 0 }
460
471
if _ , err := r .Read (header ); err != nil {
461
- // fmt.Printf ("failed to connect to read header: %v\n", err)
472
+ utils . PrintDebug ( fmt .Sprintf ("failed to connect to read header: %v\n " , err ) )
462
473
msg := structs.SocksMsg {
463
474
ServerId : channelId ,
464
475
Exit : true ,
@@ -468,7 +479,7 @@ func writeToUDPProxy(recvChan chan structs.SocksMsg, conn net.Conn, channelId ui
468
479
}
469
480
_ , err = ReadAddrSpec (r )
470
481
if err != nil {
471
- // fmt.Printf ("failed to read remote address: %v\n", err)
482
+ utils . PrintDebug ( fmt .Sprintf ("failed to read remote address: %v\n " , err ) )
472
483
msg := structs.SocksMsg {
473
484
ServerId : channelId ,
474
485
Exit : true ,
@@ -478,6 +489,7 @@ func writeToUDPProxy(recvChan chan structs.SocksMsg, conn net.Conn, channelId ui
478
489
}
479
490
_ , err = r .WriteTo (w )
480
491
if err != nil {
492
+ utils .PrintDebug (fmt .Sprintf ("failed to write to proxy: %v\n " , err ))
481
493
removeFromMapChan <- channelId
482
494
return
483
495
}
0 commit comments