Skip to content

Commit

Permalink
fix ICMP not working with magicIP (#82), revert to old protocol (temp f…
Browse files Browse the repository at this point in the history
…ix #81)
  • Loading branch information
Nicolas Chatelain committed Jul 5, 2024
1 parent 1448c27 commit 09d7d03
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 25 deletions.
6 changes: 6 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
var socksPass = flag.String("socks-pass", "", "socks5 password")
var serverAddr = flag.String("connect", "", "connect to proxy (domain:port)")
var bindAddr = flag.String("bind", "", "bind to ip:port")
var versionFlag = flag.Bool("version", false, "show the current version")

flag.Usage = func() {
fmt.Printf("Ligolo-ng %s / %s / %s\n", version, commit, date)
Expand All @@ -46,6 +47,11 @@ func main() {

flag.Parse()

if *versionFlag {
fmt.Printf("Ligolo-ng %s / %s / %s\n", version, commit, date)
return
}

logrus.SetReportCaller(*verbose)

if *verbose {
Expand Down
13 changes: 13 additions & 0 deletions pkg/agent/smartping/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ package smartping

import (
"github.com/go-ping/ping"
"github.com/sirupsen/logrus"
"net"
"os/exec"
"runtime"
"time"
)

// TryResolve tries to discover if the remote host is up using ICMP
func TryResolve(address string) bool {
// Always return true for localhost
magicNet := net.IPNet{
IP: net.IPv4(240, 0, 0, 0),
Mask: []byte{0xf0, 0x00, 0x00, 0x00},
}

if magicNet.Contains(net.ParseIP(address)) {
logrus.Debug("MagicIP Ping detected, returning true")
// Magic IP detected
return true
}
methods := []func(string) (bool, error){
RawPinger,
CommandPinger,
Expand Down
107 changes: 105 additions & 2 deletions pkg/protocol/decoder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package protocol

import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"
"io"
)

Expand All @@ -18,11 +21,111 @@ func NewDecoder(reader io.Reader) LigoloDecoder {

// Decode read content from the reader and fill the Envelope
func (d *LigoloDecoder) Decode() error {
gobdecoder := gob.NewDecoder(d.reader)
if err := binary.Read(d.reader, binary.LittleEndian, &d.Envelope.Type); err != nil {
return err
}

if err := gobdecoder.Decode(&d.Envelope); err != nil {
if err := binary.Read(d.reader, binary.LittleEndian, &d.Envelope.Size); err != nil {
return err
}

payload := make([]byte, d.Envelope.Size)

if _, err := d.reader.Read(payload); err != nil {
return err
}

gobdecoder := gob.NewDecoder(bytes.NewReader(payload))

// Kind of dirty, but it's the only way I found to satisfy gob
switch d.Envelope.Type {
case MessageInfoRequest:
p := InfoRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageInfoReply:
p := InfoReplyPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageConnectRequest:
p := ConnectRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageConnectResponse:
p := ConnectResponsePacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageHostPingRequest:
p := HostPingRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageHostPingResponse:
p := HostPingResponsePacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerRequest:
p := ListenerRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerResponse:
p := ListenerResponsePacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerBindRequest:
p := ListenerBindPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerBindResponse:
p := ListenerBindReponse{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerSockRequest:
p := ListenerSockRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerSockResponse:
p := ListenerSockResponsePacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerCloseRequest:
p := ListenerCloseRequestPacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
case MessageListenerCloseResponse:
p := ListenerCloseResponsePacket{}
if err := gobdecoder.Decode(&p); err != nil {
return err
}
d.Envelope.Payload = p
default:
return errors.New("invalid message type")
}

return nil
}
13 changes: 12 additions & 1 deletion pkg/protocol/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package protocol

import (
"bytes"
"encoding/binary"
"encoding/gob"
"io"
)
Expand All @@ -20,7 +21,17 @@ func NewEncoder(writer io.Writer) LigoloEncoder {
func (e *LigoloEncoder) Encode(envelope Envelope) error {
var payload bytes.Buffer
encoder := gob.NewEncoder(&payload)
if err := encoder.Encode(envelope); err != nil {
if err := encoder.Encode(envelope.Payload); err != nil {
return err
}

if err := binary.Write(e.writer, binary.LittleEndian, envelope.Type); err != nil {
return err
}
if envelope.Size == 0 {
envelope.Size = int32(payload.Len())
}
if err := binary.Write(e.writer, binary.LittleEndian, envelope.Size); err != nil {
return err
}
_, err := e.writer.Write(payload.Bytes())
Expand Down
22 changes: 0 additions & 22 deletions pkg/protocol/packets.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package protocol

import (
"encoding/gob"
"net"
)

Expand Down Expand Up @@ -158,24 +157,3 @@ type HostPingRequestPacket struct {
type HostPingResponsePacket struct {
Alive bool
}

func init() {
// grep -Po 'type .* struct {' pkg/protocol/packets.go | cut -d ' ' -f2 | awk '{ print "gob.Register("$0"{})" }'
gob.Register(Envelope{})
gob.Register(InfoRequestPacket{})
gob.Register(InfoReplyPacket{})
gob.Register(ListenerSockRequestPacket{})
gob.Register(ListenerSockResponsePacket{})
gob.Register(ListenerRequestPacket{})
gob.Register(ListenerResponsePacket{})
gob.Register(ListenerBindPacket{})
gob.Register(ListenerBindReponse{})
gob.Register(ListenerUDPPacket{})
gob.Register(ListenerCloseRequestPacket{})
gob.Register(ListenerCloseResponsePacket{})
gob.Register(NetInterface{})
gob.Register(ConnectRequestPacket{})
gob.Register(ConnectResponsePacket{})
gob.Register(HostPingRequestPacket{})
gob.Register(HostPingResponsePacket{})
}

0 comments on commit 09d7d03

Please sign in to comment.