-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
history.go
92 lines (84 loc) · 2.21 KB
/
history.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
package ffuf
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type ConfigOptionsHistory struct {
ConfigOptions
Time time.Time `json:"time"`
}
func WriteHistoryEntry(conf *Config) (string, error) {
options := ConfigOptionsHistory{
ConfigOptions: conf.ToOptions(),
Time: time.Now(),
}
jsonoptions, err := json.Marshal(options)
if err != nil {
return "", err
}
hashstr := calculateHistoryHash(jsonoptions)
err = createConfigDir(filepath.Join(HISTORYDIR, hashstr))
if err != nil {
return "", err
}
err = os.WriteFile(filepath.Join(HISTORYDIR, hashstr, "options"), jsonoptions, 0640)
return hashstr, err
}
func calculateHistoryHash(options []byte) string {
return fmt.Sprintf("%x", sha256.Sum256(options))
}
func SearchHash(hash string) ([]ConfigOptionsHistory, int, error) {
coptions := make([]ConfigOptionsHistory, 0)
if len(hash) < 6 {
return coptions, 0, errors.New("bad FFUFHASH value")
}
historypart := hash[0:5]
position, err := strconv.ParseInt(hash[5:], 16, 32)
if err != nil {
return coptions, 0, errors.New("bad positional value in FFUFHASH")
}
all_dirs, err := os.ReadDir(HISTORYDIR)
if err != nil {
return coptions, 0, err
}
matched_dirs := make([]string, 0)
for _, filename := range all_dirs {
if filename.IsDir() {
if strings.HasPrefix(strings.ToLower(filename.Name()), strings.ToLower(historypart)) {
matched_dirs = append(matched_dirs, filename.Name())
}
}
}
for _, dirname := range matched_dirs {
copts, err := configFromHistory(filepath.Join(HISTORYDIR, dirname))
if err != nil {
continue
}
coptions = append(coptions, copts)
}
return coptions, int(position), err
}
func HistoryReplayable(conf *Config) (bool, string) {
for _, w := range conf.Wordlists {
if w == "-" || strings.HasPrefix(w, "-:") {
return false, "stdin input was used for one of the wordlists"
}
}
return true, ""
}
func configFromHistory(dirname string) (ConfigOptionsHistory, error) {
jsonOptions, err := os.ReadFile(filepath.Join(dirname, "options"))
if err != nil {
return ConfigOptionsHistory{}, err
}
tmpOptions := ConfigOptionsHistory{}
err = json.Unmarshal(jsonOptions, &tmpOptions)
return tmpOptions, err
}