grasshopper

package module
v1.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 24 Imported by: 0

README ΒΆ

πŸ¦— grasshopper

GoDoc MIT licensed Created At Go Report Card Release

Grasshopper is a UDP packet forwarder that listens for incoming packets and forwards them to a configured destination. It optionally supports cryptography for both incoming and outgoing packets, using different keys and methods.

Architecture

Grasshopper functions as a chained relay system. Take a chained DNS query For example:

                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                 
                      β”‚ ENCRYPTED  β”‚                 β”‚ RE-ENCRYPTION β”‚                                 
                      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜                 β”‚ AES ───► 3DES β”‚                                 
                             β”‚                       β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                 
                             β”‚                           β”‚                                             
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”β–Ό             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     
                <HOP0>   HOPS(AES)         β”‚  DECRYPTED β”‚β–Ό          <HOP5>      HOPS(FINAL)            
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β””       β”Œβ”€β”€β”€β”€β”           β””  DATA   HOPS(3DES)        β”‚       β”Œβ”€β”΄β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ dig xxx β”œβ”€β–Ί CLEAR TEXT  β”‚HOP1┼── CIPHER ──► PACKET  β”Œβ”€β”΄β”€β”€β”           β”” DNS   β”‚Hop6β”œβ”€β–Ί 8.8.8.8:53 β”‚
β”‚ @hop0   β”‚       β”Œ       β”‚Hop2β”‚   (AES)   β”Œ          β”‚Hop4β”œβ”€ CIPHER ──► QUERY β”‚Hop7β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚  β–²    β”‚HOP3β”‚         <HOP2>  β–²    β”‚Hop5β”‚  (3DES)   β”Œ       β””β”€β”¬β”€β”€β”˜                  
                  β”‚  β”‚    β””β”€β”€β”€β”€β”˜           β”‚     β”‚    β””β”€β”¬β”€β”€β”˜           β”‚         β”‚                     
                  β””β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     
                     β”‚                           β”‚                                                     
                  β”Œβ”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                  β”‚                                                     
                  β”‚           β”‚                  β”‚                                                     
                  β”‚ OPTIONAL  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                     
                  β”‚ PACKET    β”‚                                                                        
                  β”‚ PROCESSOR β”‚                                                                        
                  β”‚           β”‚                                                                        
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                                        

Installation

Install the latest version of Grasshopper using the following command:

go install  github.com/xtaci/grasshopper/cmd/grasshopper@latest     

Parameters

Grasshopper supports the following parameters:

Grasshopper is a UDP packet forwarder that listens for incoming packets and forwards them to a configured destination. It optionally supports cryptography for both incoming and outgoing packets, using different keys and methods.  Optionally, the listener can be configured to apply cryptogrraphy on both the incoming and outgoing packets, with different keys and methods.

Usage:
  grasshopper [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  start       Start a listener for UDP packet forwarding

Flags:
      --ci string          Cryptography method for incoming data. Available options: aes, aes-128, aes-192, qpp, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, sm4, none (default "qpp")
      --co string          Cryptography method for incoming data. Available options: aes, aes-128, aes-192, qpp, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, sm4, none (default "qpp")
  -c, --config string      config file name
  -h, --help               help for grasshopper
      --ki string          Secret key to encrypt and decrypt for the last hop(client-side) (default "it's a secret")
      --ko string          Secret key to encrypt and decrypt for the next hops (default "it's a secret")
  -l, --listen string      Listener address, eg: "IP:1234" (default ":1234")
  -n, --nexthops strings   Servers to randomly forward to (default [127.0.0.1:3000])
      --sockbuf int        Socket buffer size for the listener (default 1048576)
      --timeout duration   Idle timeout duration for a UDP connection (default 1m0s)
  -t, --toggle             Help message for toggle
  -v, --version            version for grasshopper

Use "grasshopper [command] --help" for more information about a command.

Cryptography Support

Cases-β…  Secure Echo

Step 1: Start a UDP Echo Server

Use ncat to start a UDP echo server on port 5000:

ncat -e /bin/cat -k -u -l 5000
Step 2: Start a Level-2 Relayer to the Echo Server

Run the following command to start a relayer:

./grasshopper start --ci aes --co none -l "127.0.0.1:4001" -n "127.0.0.1:5000"
  • --ci aes: Applies cryptography on incoming packets.
  • --co none: Transfers plaintext to the ncat echo server.
Step 3: Start a Level-1 Relayer to the Level-2 Relayer

Run the following command to start another relayer:

./grasshopper start --ci none --co aes -l "127.0.0.1:4000" -n "127.0.0.1:4001"
  • --ci none: No cryptography is applied to incoming packets.
  • --co aes: Encrypts and relays packets to the next hop.
Step 4: Start a Demo Client

Use ncat to send UDP packets and interact with the relayer chain:

ncat -u 127.0.0.1 2132

Case-β…‘ Secure DNS query(random selection)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ YOUR─LAPTOP ──────────────┐           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ CLOUD─SERVER ───────────┐
β”‚                                       β”‚           β”‚                                   β”‚
β”‚                                       β”‚           β”‚                                   β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚           β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚ β”‚                   β”‚   β”‚          β”‚  β”‚           β”‚ β”‚          β”‚   β”‚               β”‚  β”‚
β”‚ β”‚ dig google.com    β”œβ”€β”€β”€β–Ί Level-1  β”‚  β”‚           β”‚ β”‚ Level-2  β”œβ”€β”€β”€β–Ί Google DNS:53 β”‚  β”‚
β”‚ β”‚ @127.0.0.1 -p 4000β”‚   β”‚ Relayer  ┼──┼ ENCRYPTED ┼─► Relayer  β”‚   β”‚ CloudFlare:53 β”‚  β”‚
β”‚ β”‚                   β”‚   β”‚          β”‚  β”‚    UDP    β”‚ β”‚          β”‚   β”‚               β”‚  β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚           β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                       β”‚           β”‚                                   β”‚
β”‚                                       β”‚           β”‚                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Step 1: Start a Level-2 Relayer to the Google DNS Server(On your Cloud ServerπŸ–₯️)
./grasshopper start --ci aes --co none -l "CLOUD_PUBLIC_IP:4000" -n "8.8.8.8:53,1.1.1.1:53"
  • --ci aes: Decrypts the packet from Level-1 Relayer.
  • --co none: Transfers decrypted plaintext DNS query packet to Google DNS.
Step 2: Start a Level-1 Relayer to the Level-2 Relayer(On your LaptopπŸ’»)
./grasshopper start --ci none --co aes -l "127.0.0.1:4000" -n "CLOUD_PUBLIC_IP:4000"
  • --ci none: Since dig command queries in plaintext, we do not need to decrypt the packet.
  • --co aes: Decrypts and relays packets to Level-2 Relayer
Step 3: Query Level-1 Relayer with dig(On your LaptopπŸ’»)
dig google.com @127.0.0.1 -p 4000

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type BlockCrypt ΒΆ

type BlockCrypt interface {
	// Encrypt encrypts the whole block in src into dst.
	// Dst and src may point at the same memory.
	Encrypt(dst, src []byte)

	// Decrypt decrypts the whole block in src into dst.
	// Dst and src may point at the same memory.
	Decrypt(dst, src []byte)
}

BlockCrypt defines encryption/decryption methods for a given byte slice. Notes on implementing: the data to be encrypted contains a builtin nonce at the first 16 bytes

func NewBlowfishBlockCrypt ΒΆ

func NewBlowfishBlockCrypt(key []byte) (BlockCrypt, error)

NewBlowfishBlockCrypt https://en.wikipedia.org/wiki/Blowfish_(cipher)

func NewCast5BlockCrypt ΒΆ

func NewCast5BlockCrypt(key []byte) (BlockCrypt, error)

NewCast5BlockCrypt https://en.wikipedia.org/wiki/CAST-128

func NewSM4BlockCrypt ΒΆ

func NewSM4BlockCrypt(key []byte) (BlockCrypt, error)

NewSM4BlockCrypt https://github.com/tjfoc/gmsm/tree/master/sm4

func NewSalsa20BlockCrypt ΒΆ

func NewSalsa20BlockCrypt(key []byte) (BlockCrypt, error)

NewSalsa20BlockCrypt https://en.wikipedia.org/wiki/Salsa20

func NewTripleDESBlockCrypt ΒΆ

func NewTripleDESBlockCrypt(key []byte) (BlockCrypt, error)

NewTripleDESBlockCrypt https://en.wikipedia.org/wiki/Triple_DES

func NewTwofishBlockCrypt ΒΆ

func NewTwofishBlockCrypt(key []byte) (BlockCrypt, error)

NewTwofishBlockCrypt https://en.wikipedia.org/wiki/Twofish

func NewXTEABlockCrypt ΒΆ

func NewXTEABlockCrypt(key []byte) (BlockCrypt, error)

NewXTEABlockCrypt https://en.wikipedia.org/wiki/XTEA

type Listener ΒΆ

type Listener struct {
	// contains filtered or unexported fields
}

Listener represents a UDP server that listens for incoming connections and relays them to the next hop.

func ListenWithOptions ΒΆ

func ListenWithOptions(laddr string,
	nexthops []string,
	sockbuf int,
	timeout time.Duration,
	crypterIn BlockCrypt, crypterOut BlockCrypt,
	onClientIn OnClientInCallback,
	onNextHopIn OnNextHopInCallback,
	logger *log.Logger) (*Listener, error)

ListenWithOptions initializes a new Listener with the provided options. Parameters: - laddr: Address to listen on. - nexthop: Addresses to forward packets to. - sockbuf: Socket buffer size in bytes. - timeout: Session timeout duration. - crypterIn: Cryptographic handler for decrypting incoming packets. - crypterOut: Cryptographic handler for encrypting outgoing packets. - pre: Prerouting function for processing incoming packets. - post: Postrouting function before forwarding packets to the next hop. - logger: Logger instance for logging.

func (*Listener) Close ΒΆ

func (l *Listener) Close() error

Close terminates the listener, releasing resources.

func (*Listener) Start ΒΆ

func (l *Listener) Start()

Start begins the listener loop, handling incoming packets and forwarding them. It blocks until the listener is closed or encounters an error.

type OnClientInCallback ΒΆ added in v1.0.6

type OnClientInCallback func(client net.Addr, in []byte) (out []byte)

OnClientInCallback is a callback function that processes incoming packets from clients

type OnNextHopInCallback ΒΆ added in v1.0.6

type OnNextHopInCallback func(hop net.Addr, client net.Addr, in []byte) (out []byte)

OnNextHopInCallback is a callback function that processes incoming packets from the next hop.

Directories ΒΆ

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL