From 09d7d036ab8e5819167b6b565b64ab602d1f7985 Mon Sep 17 00:00:00 2001 From: Nicolas Chatelain Date: Fri, 5 Jul 2024 22:21:28 +0200 Subject: [PATCH] fix ICMP not working with magicIP (#82), revert to old protocol (temp fix #81) --- cmd/agent/main.go | 6 ++ pkg/agent/smartping/pinger.go | 13 +++++ pkg/protocol/decoder.go | 107 +++++++++++++++++++++++++++++++++- pkg/protocol/encoder.go | 13 ++++- pkg/protocol/packets.go | 22 ------- 5 files changed, 136 insertions(+), 25 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 96b76c7..c2ac41a 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -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) @@ -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 { diff --git a/pkg/agent/smartping/pinger.go b/pkg/agent/smartping/pinger.go index d6432b7..659d475 100644 --- a/pkg/agent/smartping/pinger.go +++ b/pkg/agent/smartping/pinger.go @@ -2,6 +2,8 @@ package smartping import ( "github.com/go-ping/ping" + "github.com/sirupsen/logrus" + "net" "os/exec" "runtime" "time" @@ -9,6 +11,17 @@ import ( // 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, diff --git a/pkg/protocol/decoder.go b/pkg/protocol/decoder.go index c6ee175..95340ff 100644 --- a/pkg/protocol/decoder.go +++ b/pkg/protocol/decoder.go @@ -1,7 +1,10 @@ package protocol import ( + "bytes" + "encoding/binary" "encoding/gob" + "errors" "io" ) @@ -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 } diff --git a/pkg/protocol/encoder.go b/pkg/protocol/encoder.go index 669e5a6..78aba0d 100644 --- a/pkg/protocol/encoder.go +++ b/pkg/protocol/encoder.go @@ -2,6 +2,7 @@ package protocol import ( "bytes" + "encoding/binary" "encoding/gob" "io" ) @@ -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()) diff --git a/pkg/protocol/packets.go b/pkg/protocol/packets.go index af9b3fd..13cf5f7 100644 --- a/pkg/protocol/packets.go +++ b/pkg/protocol/packets.go @@ -1,7 +1,6 @@ package protocol import ( - "encoding/gob" "net" ) @@ -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{}) -}