/
antiforensics.go
174 lines (147 loc) · 3.06 KB
/
antiforensics.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
package antiforensics
/*
References:
https://github.com/lemon-mint/wipe-file-go/blob/main/wiper/wiper.go
https://www.kroll.com/en/insights/publications/cyber/anti-forensic-tactics/anti-forensics-tactics-timestomping
*/
import (
"crypto/rand"
"encoding/base32"
"io"
"math"
mrand "math/rand"
"os"
"path/filepath"
"strings"
"time"
files "github.com/D3Ext/maldev/files"
)
const blockSize = 1024 * 1024
func Wipe(src string) error { // Works recursively to delete folders
src = strings.TrimSuffix(src, "/")
check, err := files.IsFile(src)
if err != nil {
return err
}
if check == true {
err = wipeFile(src)
if err != nil {
return err
}
} else if check == false {
directory, err := os.Open(src)
if err != nil {
return err
}
objects, err := directory.Readdir(-1)
if err != nil {
return err
}
for _, obj := range objects {
if obj.IsDir() {
err := Wipe(src + "/" + obj.Name())
if err != nil {
return err
}
} else {
err := wipeFile(src + "/" + obj.Name())
if err != nil {
return err
}
}
}
err = os.Remove(src)
if err != nil {
return err
}
}
return nil
}
func Timestomp(src string, count int) error {
src = strings.TrimSuffix(src, "/")
check, err := files.IsFile(src)
if err != nil {
return err
}
if check == true {
err = timestompFile(src, count)
if err != nil {
return err
}
} else if check == false {
directory, err := os.Open(src)
if err != nil {
return err
}
objects, err := directory.Readdir(-1)
if err != nil {
return err
}
for _, obj := range objects {
if obj.IsDir() {
err := Timestomp(src+"/"+obj.Name(), count)
if err != nil {
return err
}
} else {
err := timestompFile(src+"/"+obj.Name(), count)
if err != nil {
return err
}
}
}
}
return nil
}
func wipeFile(filename string) error {
randbuf := make([]byte, blockSize)
f, err := os.OpenFile(filename, os.O_RDWR, 0666)
if err != nil {
return err
}
stat, err := f.Stat()
if err != nil {
return err
}
size := stat.Size()
blockCount := int(math.Ceil(float64(size)/blockSize)) + 1
for pass := 0; pass < 3; pass++ {
for i := 0; i < blockCount; i++ {
f.WriteAt(randbuf, int64(i*blockSize))
if pass != 0 {
io.ReadFull(rand.Reader, randbuf)
}
}
io.ReadFull(rand.Reader, randbuf)
f.Sync()
}
f.Close()
dir, _ := filepath.Split(filename)
newname := filepath.Join(dir, randb32(max(len(filename), 20)))
for i := 0; i < 3; i++ {
os.Rename(filename, newname)
filename = newname
newname = filepath.Join(dir, randb32(max(len(filename), 20)))
}
return os.Remove(filename)
}
func timestompFile(filename string, count int) error {
for i := 0; i < count; i++ {
err := os.Chtimes(filename, time.Unix(mrand.Int63n(time.Now().Unix()), 0), time.Unix(mrand.Int63n(time.Now().Unix()), 0))
if err != nil {
return err
}
}
return nil
}
func randb32(size int) string {
buf := make([]byte, size)
io.ReadFull(rand.Reader, buf)
return base32.StdEncoding.EncodeToString(buf)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}