-
Notifications
You must be signed in to change notification settings - Fork 240
/
hashcat_engine.go
248 lines (214 loc) · 6.77 KB
/
hashcat_engine.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
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
package hashcat
import (
"fmt"
"os"
"path/filepath"
"unsafe"
"github.com/fireeye/gocrack/gocat"
"github.com/fireeye/gocrack/gocat/hcargp"
"github.com/fireeye/gocrack/gocat/restoreutil"
"github.com/fireeye/gocrack/opencl"
"github.com/fireeye/gocrack/server/rpc"
"github.com/fireeye/gocrack/server/storage"
"github.com/fireeye/gocrack/shared"
"github.com/fireeye/gocrack/worker"
"github.com/rs/zerolog/log"
)
type HashcatEngine struct {
TaskID string
HashcatSharedPath string
SessionPath string
// Task Options
TaskFilePath string
DictionaryFile string
MasksFile string
RulesFile string
Options shared.HashcatUserOptions
Upstream rpc.GoCrackRPC
CLDevices storage.CLDevices
engine *gocat.Hashcat
// if isBruteForce is true, we'll allow for a checkpoint
isBruteForce bool
}
// Initialize the hashcat engine
func (s *HashcatEngine) Initialize() error {
hc, err := gocat.New(gocat.Options{
SharedPath: s.HashcatSharedPath,
}, s.callback())
if err != nil {
return err
}
s.engine = hc
return nil
}
func (s *HashcatEngine) callback() gocat.EventCallback {
return func(hc unsafe.Pointer, payload interface{}) {
switch pl := payload.(type) {
case gocat.LogPayload:
fmt.Printf("LOG [%s] %s\n", pl.Level, pl.Message)
case gocat.ActionPayload:
fmt.Printf("ACTION [%d] %s\n", pl.HashcatEvent, pl.Message)
case gocat.CrackedPayload:
s.Upstream.SavedCrackedPassword(rpc.CrackedPasswordRequest{
TaskID: s.TaskID,
Hash: pl.Hash,
Value: pl.Value,
CrackedAt: pl.CrackedAt,
})
case gocat.FinalStatusPayload:
if pl.AllHashesCracked {
if err := s.Upstream.ChangeTaskStatus(rpc.ChangeTaskStatusRequest{
TaskID: s.TaskID,
NewStatus: storage.TaskStatusFinished,
}); err != nil {
log.Error().Err(err).Msg("All hashes cracked but failed to set final status")
}
return
}
// Safeguard us from panic'ing here when the task fails to start
if pl.Status == nil {
return
}
var tstatus storage.TaskStatus
// https://github.com/hashcat/hashcat/blob/master/src/status.c#L22-L35
switch pl.Status.Status {
case "Cracked":
tstatus = storage.TaskStatusFinished
case "Exhausted":
tstatus = storage.TaskStatusExhausted
case "Unknown! Bug!":
tstatus = storage.TaskStatusError
case "Quit", "Aborted", "Paused", "Aborted (Checkpoint)", "Aborted (Runtime)":
fallthrough
default:
tstatus = storage.TaskStatusStopped
}
if err := s.Upstream.ChangeTaskStatus(rpc.ChangeTaskStatusRequest{
TaskID: s.TaskID,
NewStatus: tstatus,
}); err != nil {
log.Error().Err(err).Str("new_status", string(tstatus)).Msg("Failed to change task status")
}
if err := s.Upstream.SendTaskStatus(rpc.TaskStatusUpdate{
TaskID: s.TaskID,
Payload: pl.Status,
Engine: storage.WorkerHashcatEngine,
Final: true,
}); err != nil {
log.Error().Err(err).Msg("Failed to send task status update to server")
}
}
}
}
// Start the hashcat engine
func (s *HashcatEngine) Start() error {
var opts hcargp.HashcatSessionOptions
restoreFilePath := filepath.Join(s.SessionPath, fmt.Sprintf("%s.restore", s.TaskID))
outFilePath := filepath.Join(s.SessionPath, fmt.Sprintf("%s.outfile", s.TaskID))
// Get a list of active devices on this machine
machDevices, err := worker.GetAvailableDevices()
if err != nil {
return err
}
// Grab the checkpoint from the server if one exists
if err := getCheckpoint(s.Upstream, s.TaskID, restoreFilePath); err != nil && err != rpc.ErrNoCheckpoint {
return err
}
// If the restore file exists, set the restore flag
if _, err := os.Stat(restoreFilePath); !os.IsNotExist(err) {
// When restoring, we can really only set restore related options
opts = hcargp.HashcatSessionOptions{
RestoreSession: hcargp.GetBoolPtr(true),
SessionName: hcargp.GetStringPtr(s.TaskID),
RestoreFilePath: hcargp.GetStringPtr(restoreFilePath),
}
checkpoint, err := restoreutil.ReadRestoreFile(restoreFilePath)
if err != nil {
return err
}
log.Debug().
Uint32("version", checkpoint.Version).
Uint32("argc", checkpoint.ArgCount).
Strs("args", checkpoint.Args).
Msg("Parsed Restore File")
changed, err := ModifyRestoreFileDevices(&checkpoint, s.CLDevices, machDevices)
if err != nil {
return err
}
if changed {
if err := changeLocalCheckpoint(checkpoint, restoreFilePath); err != nil {
return err
}
}
} else {
// Not a restore
opts = hcargp.HashcatSessionOptions{
AttackMode: hcargp.GetIntPtr(int(s.Options.AttackMode)),
HashType: hcargp.GetIntPtr(s.Options.HashType),
PotfileDisable: hcargp.GetBoolPtr(true),
InputFile: s.TaskFilePath,
SessionName: hcargp.GetStringPtr(s.TaskID),
RestoreFilePath: hcargp.GetStringPtr(restoreFilePath),
OutfilePath: hcargp.GetStringPtr(outFilePath),
}
if s.MasksFile != "" && s.Options.AttackMode == shared.AttackModeBruteForce {
opts.DictionaryMaskDirectoryInput = hcargp.GetStringPtr(s.MasksFile)
s.isBruteForce = true
}
if s.RulesFile != "" && s.Options.AttackMode == shared.AttackModeStraight {
opts.RulesFile = hcargp.GetStringPtr(s.RulesFile)
}
if s.DictionaryFile != "" && s.Options.AttackMode == shared.AttackModeStraight {
opts.DictionaryMaskDirectoryInput = hcargp.GetStringPtr(s.DictionaryFile)
}
if len(s.CLDevices) > 0 {
var devTypes []int
var dGPUAdded, dCPUAdded bool
for _, requestedDevice := range s.CLDevices {
dev, ok := machDevices[requestedDevice]
if !ok {
return fmt.Errorf("`%d` is not an available device on this platform", requestedDevice)
}
switch dev.Type {
case opencl.DeviceTypeCPU:
if !dCPUAdded {
devTypes = append(devTypes, 1)
dCPUAdded = true
}
case opencl.DeviceTypeGPU:
if !dGPUAdded {
devTypes = append(devTypes, 2)
dGPUAdded = true
}
}
}
opts.OpenCLDevices = hcargp.GetStringPtr(s.CLDevices.String())
opts.OpenCLDeviceTypes = hcargp.GetStringPtr(shared.IntSliceToString(devTypes))
}
}
defer saveCheckpoint(s.Upstream, s.TaskID, restoreFilePath)
return s.engine.RunJobWithOptions(opts)
}
// Stop the hashcat engine. If the engine is brute forcing, we attempt to stop at a checkpoint otherwise we abort
func (s *HashcatEngine) Stop() error {
if s.engine == nil {
return nil
}
if s.isBruteForce {
// Attempt to checkpoint... if we cant, immediately abort
if err := s.engine.StopAtCheckpoint(); err != nil {
s.engine.AbortRunningTask()
return err
}
}
s.engine.AbortRunningTask()
return nil
}
// GetStatus returns the status of the engine
func (s *HashcatEngine) GetStatus() interface{} {
return s.engine.GetStatus()
}
// Cleanup releases the engine and cleans up any allocated resources
func (s *HashcatEngine) Cleanup() {
s.engine.Free()
}