Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
282 lines (228 loc) · 6.71 KB

File metadata and controls

282 lines (228 loc) · 6.71 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// SPDX-License-Identifier: MIT
package processor
import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/gosuri/uiprogress"
)
// Global Version
var Version = "1.5.0 (beta)"
// Verbose enables verbose logging output
var Verbose = false
// VeryVerbose enables verbose logging output
var VeryVerbose = false
// Debug enables debug logging output
var Debug = false
// Trace enables trace logging output which is extremely verbose
var Trace = false
// MTime enable mtime calculation and output
var MTime = false
// Progress uses ui bar to display the progress of files
var Progress = false
// Recursive to walk directories
var Recursive = false
// Do not print out results as they are processed
var NoStream = false
// If data is being piped in using stdin
var StandardInput = false
// Should the application print all hashes it knows about
var Hashes = false
// List of hashes that we want to process
var Hash = []string{}
// Format sets the output format of the formatter
var Format = ""
// FileOutput sets the file that output should be written to
var FileOutput = ""
// AuditFile sets the file that we want to audit against similar to hashdeep
var AuditFile = ""
// DirFilePaths is not set via flags but by arguments following the flags for file or directory to process
var DirFilePaths = []string{}
// FileListQueueSize is the queue of files found and ready to be processed
var FileListQueueSize = 1000
// Number of bytes in a size to enable memory maps or streaming
var StreamSize int64 = 1_000_000
// FileInput indicates we have a file passed in which consists of a
var FileInput = ""
// GitIgnore set false to enable .gitignore checks
var GitIgnore = true
// GitModuleIgnore set false to enable .gitmodules checks
var GitModuleIgnore = true
// Ignore set false to enable ignore file checks
var Ignore = true
// HashIgnore set true to enable hashignore file checks
var HashIgnore = false
// SkipHidden set to false to ignore hidden folders and files
var SkipHidden = true
// PathDenyList sets the paths that should be skipped
var PathDenyList = []string{}
// Exclude is a regular expression which is used to exclude files from being processed
var Exclude = []string{}
var NoThreads = runtime.NumCPU()
// String mapping for hash names
var HashNames = Result{
CRC32: "crc32",
XxHash64: "xxhash64",
MD4: "md4",
MD5: "md5",
SHA1: "sha1",
SHA256: "sha256",
SHA512: "sha512",
Blake2b256: "blake2b256",
Blake2b512: "blake2b512",
Blake3: "blake3",
Sha3224: "sha3224",
Sha3256: "sha3256",
Sha3384: "sha3384",
Sha3512: "sha3512",
Ed2k: "ed2k",
}
// Process is the main entry point of the command line it sets everything up and starts running
func Process() {
// Display the supported hashes then bail out
if Hashes {
printHashes()
return
}
// Check if we are accepting data from stdin
if len(DirFilePaths) == 0 {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
StandardInput = true
}
}
// If nothing was supplied as an argument to run against, assume run against everything in the
// current directory recursively
if len(DirFilePaths) == 0 {
DirFilePaths = append(DirFilePaths, ".")
}
// If a single argument is supplied, enable recursive as if it's a file no problem
// but if its a directory the user probably wants to hash everything in that directory
if len(DirFilePaths) == 1 {
Recursive = true
}
// Clean up hashes by setting all input to lowercase
Hash = formatHashInput()
// Where audit file is set we only want to process the hashes that hashdeep supports for this
// NB we will need to expand this in the future when supporting our own format
if AuditFile != "" {
Hash = []string{"md5", "sha256"}
}
// If format is set to hashdeep
if strings.ToLower(Format) == "hashdeep" {
// if hashdeep, we need to ensure md5 is set or sha256, or both, and if neither are set both
if !hasHash("md5") || !hasHash("sha256") {
printError("hashdeep format without supported hash - setting md5 and sha256")
Hash = []string{"md5", "sha256"}
}
}
// Results ready to be printed
fileSummaryQueue := make(chan Result, FileListQueueSize)
if StandardInput {
if MTime {
printError("cannot use --mtime option with standard input, ignoring flag")
}
go processStandardInput(fileSummaryQueue)
} else {
// Files ready to be read from disk
fileListQueue := make(chan string, FileListQueueSize)
if FileInput == "" {
// Spawn routine to start finding files on disk
go func() {
// Check if the paths or files added exist and inform the user if they don't
for _, f := range DirFilePaths {
fp := filepath.Clean(f)
fi, err := os.Stat(fp)
// If there is an error which is usually does not exist then exit non zero
if err != nil {
printError(fmt.Sprintf("file or directory issue: %s %s", fp, err.Error()))
os.Exit(1)
} else {
if fi.IsDir() {
if Recursive {
walkDirectoryWithIgnore(fp, fileListQueue)
}
} else {
fileListQueue <- fp
}
}
}
close(fileListQueue)
}()
} else {
// Open the file
go func() {
file, err := os.Open(FileInput)
if err != nil {
printError(fmt.Sprintf("failed to open input file: %s, %s", FileInput, err.Error()))
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// Read the file line by line
for scanner.Scan() {
line := scanner.Text()
fileListQueue <- line
}
close(fileListQueue)
// Check for errors during scanning
if err := scanner.Err(); err != nil {
printError(fmt.Sprintf("error reading input file: %s, %s", FileInput, err.Error()))
os.Exit(1)
}
}()
}
if Progress {
uiprogress.Start() // start rendering of progress bars
}
var wg sync.WaitGroup
for i := 0; i < NoThreads; i++ {
wg.Add(1)
go func() {
fileProcessorWorker(fileListQueue, fileSummaryQueue)
wg.Done()
}()
}
go func() {
wg.Wait()
close(fileSummaryQueue)
}()
}
result, valid := fileSummarize(fileSummaryQueue)
if FileOutput == "" {
fmt.Print(result)
if !valid {
os.Exit(1)
}
} else {
// we don't write out sqlite
if strings.ToLower(Format) != "sqlite" {
_ = os.WriteFile(FileOutput, []byte(result), 0600)
}
fmt.Println("results written to " + FileOutput)
}
}
// ToLower all of the input hashes so we can match them easily
func formatHashInput() []string {
h := []string{}
for _, x := range Hash {
h = append(h, strings.ToLower(x))
}
return h
}
// Check if a hash was supplied to the input so we know if we should calculate it
func hasHash(hash string) bool {
for _, x := range Hash {
if x == "all" {
return true
}
if x == hash {
return true
}
}
return false
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.