-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
63 lines (49 loc) · 1.24 KB
/
find.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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
)
func isComFile(path string, info os.FileInfo) bool {
return !info.IsDir() && strings.EqualFold(filepath.Ext(path), ".com")
}
func isInfected(path string, info os.FileInfo) (int, error) {
virusMark := []byte{0x49, 0x56}
file, err := os.Open(path)
if err != nil {
return virusNotFound, err
}
content := make([]byte, virusGenerationOffset+1)
_, err = file.Read(content)
if err != nil {
return virusNotFound, err
}
file.Close()
fileMark := content[virusMarkOffset:virusMarkEndOffset]
if !bytes.Equal(virusMark, fileMark) {
// Not infected
return virusNotFound, nil
}
fileGeneration := int(content[virusGenerationOffset])
fmt.Printf("Infected file found: %s, generation %d\n", path, fileGeneration)
return fileGeneration, nil
}
func find(root string) []string {
fmt.Printf("Finding viruses in %s\n", root)
var infectedFiles []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if isComFile(path, info) {
gen, err := isInfected(path, info)
if err == nil && gen != virusNotFound {
infectedFiles = append(infectedFiles, path)
}
}
return nil
})
if err != nil {
panic(err)
}
return infectedFiles
}