-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSiteFirstProtection.go
More file actions
executable file
·739 lines (672 loc) · 19.8 KB
/
WebSiteFirstProtection.go
File metadata and controls
executable file
·739 lines (672 loc) · 19.8 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
package main
import (
"fmt"
"os"
"encoding/json"
"io/ioutil"
"path/filepath"
"flag"
"bufio"
//"strings"
"regexp"
"github.com/fsnotify/fsnotify"
"time"
//"net/url"
"net/http"
"bytes"
"mime/multipart"
"io"
"github.com/fatih/color"
"crypto/md5"
"encoding/hex"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/tiff"
"github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
)
type FileSignature struct {
Signature string `json:"Signature"`
Text string `json:"Text"`
}
type Filedata struct {
Name string `json:"Filename"`
Malware string `json:"Malware_Name"`
Signature []FileSignature `json:"Description"`
}
type Malware struct {
Name string `json:"Malware_Name"`
Signatures []string `json:"Malware_Signatures"`
Method string `json:"Rule_Method"`
Status string `json:"Rule_Status"`
}
type Database struct {
DBName string `json:"Database_Name"`
DBFileType string `json:"Database_File_Type"`
List []Malware `json:"Database_Signatures"`
}
type Whitelist struct {
Name string `json:"name"`
Hash string `json:"md5"`
}
type FinalReport struct {
Path string `json:"Path"`
TotalFiles int64 `json:"Total"`
Positives int64 `json:"Positives"`
Date string `json:"Date"`
Files []Filedata `json:"Infected_Files"`
}
type VTScan struct {
File string
Result ScanResult
Request ScanRequest
}
type ScanResult struct {
Response int64 `json:"response_code"`
ScanDate string `json:"scan_date"`
Total int64 `json:"total"`
Positives int64 `json:"positives"`
Permalink string `json:"permalink"`
Message string `json:"verbose_msg"`
Sha256 string `json:"sha256"`
Md5 string `json:"md5"`
}
type ScanRequest struct {
Permalink string `json:"permalink"`
Resource string `json:"resource"`
Response int64 `json:"response_code"`
ScanId string `json:"scan_id"`
//Message string `json:"verbose_msg"`
Sha256 string `json:"sha256"`
//ScanDate string `json:"scan_date"`
//UrlAddress string `json:"url"`
}
var DBSignaturesPHP Database
var DBSignaturesJS Database
var DBSignaturesIMG Database
const APIKEY = ""
const TelegramKEY = ""
var watcher *fsnotify.Watcher
var logOption bool
var debugOption bool
var telegramOption bool
const layout = "2006-01-02T15:04:05"
var whitelist []Whitelist
var finalReport FinalReport
var TelegramUsers []int64 //`json:"users"`
/*
TELEGRAM FUNCTIONS
*/
//LoadTelegramUsers load users ID from telegram.users file and fill TelegramUsers slice.
//Users in telegram.users are one by line
func LoadTelegramUsers() {
fd, err := os.Open("telegram.users")
if err != nil {
return
}
defer fd.Close()
if err != nil {
return
}
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
x, err := strconv.Atoi(scanner.Text())
if err != nil {
return
}
TelegramUsers = append(TelegramUsers, int64(x))
}
}
func SendTelegramAlert(event string, file string, malware string) {
bot, err := tgbotapi.NewBotAPI(TelegramKEY)
if err != nil {
return
}
bot.Debug = true
message := "<b>"+event+"</b>\n<b>FILE:</b> <i>"+file+"</i>\n<b>MALWARE:</b><i> "+malware+"</i>"
for _,user := range TelegramUsers {
msg := tgbotapi.NewMessage(user, message)
msg.ParseMode = "html"
bot.Send(msg)
}
}
//SendTelegramMessage receive a type of message and the text of the message to send to all users
func SendSummaryReport() {
bot, err := tgbotapi.NewBotAPI(TelegramKEY)
if err != nil {
return
}
bot.Debug = false
message := "<strong>Summary Report</strong>\n<b>Date:</b><i> "+finalReport.Date+"</i>\n<b>Total Scaned Files:</b> "+strconv.Itoa(int(finalReport.TotalFiles))+"\n<b>Infected Files:</b> "+strconv.Itoa(int(finalReport.Positives))+"\nCheck Infected Files using /lastreport command"
message += "\n<b>END</b>"
for _,user := range TelegramUsers {
msg := tgbotapi.NewMessage(user, message)
msg.ParseMode = "html"
bot.Send(msg)
}
}
/*
VIRUS TOTAL FUNCTIONS Functions
*/
// Scan manage VirtusTotal send and request files. Return the number of positives or -1 for error
func (vt *VTScan) Scan(path string) int64 {
vt.File = path
if vt.request() {
for vt.result() {
if vt.Result.Response == 1 {
break
} else if vt.Result.Response == 0 {
break
}
time.Sleep(25 * time.Second)
//scanfileresult = vt.result()
}
if vt.Request.Response == 1 {
return vt.Result.Positives
}
}
return -1
}
//result result from Virtus Total
func (vt *VTScan) result() bool {
//var scanresult ScanResult
vt.Result.Response = - 10
//scanresult.Response = -10
hc := http.Client{}
//form := url.Values{}
//form.Add("apikey", APIKEY)
//form.Add("resource", vt.Request.Resource)
if debugOption {
color.Magenta("Resource\n%s",vt.Request.Resource)
}
//req, err := http.NewRequest("POST", "https://www.virustotal.com/vtapi/v2/file/report", strings.NewReader(form.Encode()))
req, err := http.NewRequest("GET", "https://www.virustotal.com/vtapi/v2/file/report",nil)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
q := req.URL.Query()
q.Add("apikey",APIKEY)
q.Add("resource",vt.Request.Resource)
req.URL.RawQuery = q.Encode()
if debugOption {
color.Magenta("%s",req.URL.String())
}
res, err := hc.Do(req)
if err != nil {
return false
}
// Check the response
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("bad status: %s", res.Status)
return false
} else {
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&vt.Result)
}
return true
}
func (vt *VTScan) request() bool {
//var scanrVTResult.vequest ScanRequest
vt.Request.Response=-10
var b bytes.Buffer
w := multipart.NewWriter(&b)
f, err := os.Open(vt.File)
if err != nil {
return false
}
defer f.Close()
h := md5.New()
if _, err = io.Copy(h,f); err != nil {
if logOption {
color.Red("%s | Error | Copy File | %s",(time.Now()).Format(layout),vt.File)
} else {
color.Red("%s",err)
}
return false
}
if InWhiteList(vt.File) {
vt.Request.Response = -9
return false
}
f.Seek(0,io.SeekStart)
fw, err := w.CreateFormFile("file", vt.File)
if err != nil {
return false
}
if _, err = io.Copy(fw, f); err != nil {
return false
}
if fw, err = w.CreateFormField("apikey"); err != nil {
return false
}
if _, err = fw.Write([]byte(APIKEY)); err != nil {
return false
}
w.Close()
req, err := http.NewRequest("POST", "https://www.virustotal.com/vtapi/v2/file/scan", &b)
if err != nil {
return false
}
req.Header.Set("Content-Type", w.FormDataContentType())
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return false
}
// Check the response
if res.StatusCode != http.StatusOK {
if debugOption {
color.Magenta("%s",res.Body)
}
if logOption {
color.Red("%s | Bad Status Code | %s",(time.Now()).Format(layout),res.Status)
} else {
err = fmt.Errorf("bad status: %s", res.Status)
}
if debugOption {
color.Magenta("%s",res.Body)
}
} else {
color.Green("%s | VT Status Code | %s",(time.Now()).Format(layout),res.Status)
decoder := json.NewDecoder(res.Body)
//requestaux := ScanRequest{}
err = decoder.Decode(&vt.Request)
//vt.Request.Resource = requestaux.Resource
if debugOption {
color.Magenta("Resource returned")
color.Magenta("%s",vt.Request.Resource)
}
return true
}
return false
}
func ScanFileOnVt(path string) (bool, string) {
var vt VTScan
vt.Result.Response = -2
if ! logOption {
fmt.Printf("\tVirus total check\n")
}
if vt.Scan(path) < 0 {
if logOption {
color.Red("%s | Error | Request Virustotal",(time.Now()).Format(layout))
} else {
color.Red("\tError on request\n")
}
return false,""
}
if vt.Result.Positives > 0 {
// if logOption {
// color.Red("%s | Warning | File infected | VirusTotal | %s | %s",(time.Now()).Format(layout),path,vt.Result.Permalink)
// } else {
// color.Red("\t\tWarning: File infected\n")
// color.Red("\t\tMore info %s\n",vt.Result.Permalink)
// }
return true, "Virtus Total Detection"
// } else {
// if logOption {
// color.Green("%s | Ok | VirusTotal | %s",(time.Now()).Format(layout),vt.Result.Permalink)
// } else {
// color.Green("\t\tOK\n")
// }
}
return false, ""
}
/*
WHITE LIST FUNCTIONS
*/
func LoadWhitelist() {
if _,err := os.Stat("whitelist.json"); err == nil {
cg,_:= ioutil.ReadFile("whitelist.json")
_= json.Unmarshal([]byte(cg),&whitelist)
} else if os.IsNotExist(err) {
if logOption {
color.Red("%s | Notice | There isn't Whitelist file",(time.Now()).Format(layout))
} else {
color.Red("There isn't Whitelist file")
}
} else {
if logOption {
color.Red((time.Now()).Format(layout),"| Alert | Something goes wrong")
} else {
color.Red("Something goes wrong")
color.Red("%s",err)
}
}
}
func InWhiteList(path string) bool {
f,_ := ioutil.ReadFile(path)
filename := filepath.Base(path)
h := md5.New()
if _, err := io.WriteString(h,string(f)); err != nil {
if logOption {
color.Red("%s | Error | Copying file | %s",(time.Now()).Format(layout),path)
} else {
color.Red("%s",err)
}
return false
}
md5sum := hex.EncodeToString(h.Sum(nil))
for _, item := range whitelist {
if item.Name == filename && item.Hash == md5sum {
if logOption {
color.Green("%s | Notice | Whitelist | %s",(time.Now()).Format(layout),filename)
} else {
color.Green("\tWhitelist for %s",filename)
}
return true
}
}
return false
}
func CheckFileText(path string, database Database, vt bool) (bool, string) {
if len(database.List) == 0 && vt == false {
return false, ""
} else if InWhiteList(path) {
return false, ""
} else if vt == true {
return ScanFileOnVt(path)
}
f, err := ioutil.ReadFile(path)
if err != nil {
if logOption {
color.Red("%s | Error | Open File | %s",(time.Now()).Format(layout),path)
} else {
color.Red("%s",err)
}
return false, ""
}
f_nolines := string(f)
var filedata Filedata
filedata.Name = path
for _, malware := range database.List {
founds := 0
if malware.Status == "enabled" {
for _, signature := range malware.Signatures {
re := regexp.MustCompile(signature)
occ := re.FindStringSubmatch(f_nolines)
if len(occ) > 0 {
founds += 1
filedata.Signature = append(filedata.Signature, FileSignature{ Signature: signature, Text: occ[0]})
if malware.Method != "and" {
founds = len(malware.Signatures)
break
}
}
}
if founds == len(malware.Signatures) {
finalReport.Positives += 1
filedata.Name = path
filedata.Malware = malware.Name
finalReport.Files = append(finalReport.Files, filedata)
return true, malware.Name
}
}
}
return false, ""
}
func LoadDatabase(extension string) Database {
var database Database
switch extension {
case "php":
if len(DBSignaturesPHP.List) != 0 {
return DBSignaturesPHP
} else {
//db = & DBSignaturesPHP
}
case "js":
if len(DBSignaturesJS.List) != 0 {
return DBSignaturesJS
} else {
//db = & DBSignaturesPHP
}
case "img":
if len(DBSignaturesIMG.List) != 0 {
return DBSignaturesIMG
} else {
//db = & DBSignaturesPHP
}
default:
}
if logOption {
color.Green("%s | Notice | Loading DB Signatures for %s files",(time.Now()).Format(layout),extension)
} else {
color.Green("\tLoading DB Signatures for %s files\n",extension)
}
if _,err := os.Stat("Signatures/signatures_"+extension+".json"); err == nil {
cg,_:= ioutil.ReadFile("Signatures/signatures_"+extension+".json")
//database := Database{}
_= json.Unmarshal([]byte(cg),&database)
} else if os.IsNotExist(err) {
if logOption {
color.Red("%s | Alert | Can't find signatures file signatures_%s.json",(time.Now()).Format(layout),extension)
} else {
color.Red("I can't find signatures_%s.json",extension)
}
} else {
if logOption {
color.Red("%s | Alert | Something goes wrong",(time.Now()).Format(layout))
} else {
color.Red("Something goes wrong")
color.Red("%s",err)
}
}
return database
}
func ScanFile(file string, vt bool) (bool, string) {
// var malware string
// var positive bool
extension := filepath.Ext(file)
switch extension {
case ".php":
DBSignaturesPHP = LoadDatabase("php")
return CheckFileText(file,DBSignaturesPHP, vt)
case ".js":
DBSignaturesJS = LoadDatabase("js")
return CheckFileText(file,DBSignaturesJS, vt)
case ".jpg":
DBSignaturesIMG = LoadDatabase("img")
return CheckFileImage(file, DBSignaturesIMG,vt)
default:
//positive, malware = CheckFile(file,Database{},vt)
return CheckFileText(file, Database{}, vt)
}
return false,""
}
type walkFunc func(exif.FieldName, *tiff.Tag) error
func (f walkFunc) Walk(name exif.FieldName, tag *tiff.Tag) error {
return f(name, tag)
}
func CheckFileImage(path string, database Database, vt bool) (bool, string) {
raw, err := ioutil.ReadFile(path)
filedata := Filedata{}
if err != nil {
color.Red("%s",err)
}
x, err := exif.Decode(bytes.NewReader(raw))
if err != nil {
color.Red("%s",err)
}
positive := false
malwareName := ""
err = x.Walk(walkFunc(func(name exif.FieldName, tag *tiff.Tag) error {
if positive {
return nil
}
for _, malware := range database.List {
if malware.Status == "enabled" {
founds := 0
for _, signature := range malware.Signatures {
re := regexp.MustCompile(signature)
str := fmt.Sprint(tag)
matches := re.FindAllString(str, -1)
if len(matches) > 0 {
founds += 1
filedata.Signature = append(filedata.Signature, FileSignature{ Signature: signature, Text: str})
if malware.Method == "and" {
if founds == len(malware.Signatures) {
finalReport.Positives += 1
filedata.Name = path
filedata.Malware = malware.Name
finalReport.Files = append(finalReport.Files, filedata)
positive = true
malwareName = malware.Name
return nil
}
} else {
filedata.Malware = malware.Name
filedata.Name = path
finalReport.Positives += 1
finalReport.Files = append(finalReport.Files, filedata)
positive = true
malwareName = malware.Name
return nil
}
}
}
}
}
return nil
}))
return positive,malwareName
}
func ScanDirectory(path string, vt bool) error {
if ! logOption {
color.Green("Scanning dir: %s",path)
}
err := filepath.Walk(path, func(file string, info os.FileInfo, err error) error {
if !info.IsDir() {
finalReport.TotalFiles += 1
infected, malware := ScanFile(file, vt)
if infected {
if logOption {
color.Red("%s | Warning | File infected | %s | %s",(time.Now()).Format(layout),malware,file)
} else {
color.Red("\tWARNING: %s\n",malware)
color.Blue("\t\tFILE: %s\n",file)
}
}
}
return nil
})
return err
}
func watchDir(path string, fi os.FileInfo, err error) error {
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}
func MonitoringDirectory(path string,vt bool) {
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
if err := filepath.Walk(path, watchDir); err != nil {
if logOption {
color.Red("%s | Error | Directory",(time.Now()).Format(layout))
} else {
color.Red("ERROR", err)
}
}
done := make(chan bool)
go func() {
for {
select {
// watch for events
case event := <-watcher.Events:
if event.Op&fsnotify.Chmod == fsnotify.Chmod || event.Op&fsnotify.Create == fsnotify.Create {
if logOption {
color.Green("%s | Event | %s | %s",(time.Now()).Format(layout),event.Op,event.Name,)
}
infected, malware := ScanFile(event.Name,vt)
if infected {
if logOption {
color.Red("%s | Warning | File infected | %s | %s",(time.Now()).Format(layout),malware,event.Name)
} else {
color.Green("\tEvent: %s\n",event.Op)
color.Red("\tWARNING: %s\n",malware)
color.Blue("\tFILE: %s\n",event.Name)
if telegramOption {
SendTelegramAlert("WARNING", event.Name, malware)
}
}
}
}
// watch for errors
case err := <-watcher.Errors:
if logOption {
color.Red("%s | Error | Read Directory",(time.Now()).Format(layout))
} else {
color.Red("ERROR", err)
}
}
}
}()
<-done
}
func SaveReport(report FinalReport) {
color.Green("Save Report")
file,_ := json.MarshalIndent(report,""," ")
_ = ioutil.WriteFile("LastReport.json",file,0644)
}
func main() {
color.Magenta("Debug Messages")
color.Red("Warnings and Errors Messages")
color.Green("Safe Messages")
scanCmd := flag.NewFlagSet("scan",flag.ExitOnError)
monitorCmd := flag.NewFlagSet("monitor",flag.ExitOnError)
if len(os.Args) < 3 {
color.Red("expected scan or monitor parameter.")
color.Red("\twebsite_scan scan -path/var/www/html\n")
color.Red("\twebsite_scan monitor -path=/var/www/html -vt -log\n")
} else {
LoadWhitelist()
LoadTelegramUsers()
finalReport.Date = (time.Now()).Format(layout)
switch os.Args[1] {
case "scan":
path := scanCmd.String("path","","path to the directory")
vt := scanCmd.Bool("vt",false,"virus total option")
out := scanCmd.Bool("log",false,"Output Option")
tel := scanCmd.Bool("telegram",false,"Output Option")
debug := scanCmd.Bool("debug",false,"Output Option")
scanCmd.Parse(os.Args[2:])
logOption = *out
debugOption = *debug
telegramOption = *tel
finalReport.Path = *path
if logOption {
color.Green("%s | Notice | Start Scan |",(time.Now()).Format(layout),*path)
} else {
color.Green("Starting scanning")
}
ScanDirectory(*path,*vt)
if logOption {
color.Green("%s | Notice | End Scan | %s | %s",(time.Now()).Format(layout),finalReport.TotalFiles, finalReport.Positives)
} else {
color.Green("End Scan")
color.Green("Total Files Scanned: ",finalReport.TotalFiles)
color.Green("Total Positives: ",finalReport.Positives)
if telegramOption {
SendSummaryReport()
}
SaveReport(finalReport)
}
case "monitor":
path := monitorCmd.String("path","","path to the directory")
vt := monitorCmd.Bool("vt",false,"virus total option")
out := monitorCmd.Bool("log",false,"Output Option")
tel := monitorCmd.Bool("telegram",false,"Output Option")
debug := monitorCmd.Bool("debug",false,"Output Option")
monitorCmd.Parse(os.Args[2:])
logOption = *out
telegramOption = *tel
debugOption = *debug
if logOption {
color.Green("%s | Notice | Start Monitor | %s",(time.Now()).Format(layout),*path)
} else {
color.Green("Start Monitor")
}
MonitoringDirectory(*path,*vt)
default:
color.Red("There are scan or monitor option")
}
}
return
}