-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
204 lines (175 loc) · 5.46 KB
/
session.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
package hashcat
import (
"bufio"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/unclesp1d3r/cipherswarmagent/shared"
"github.com/duke-git/lancet/convertor"
"github.com/duke-git/lancet/fileutil"
"github.com/duke-git/lancet/validator"
"github.com/hpcloud/tail"
)
type Session struct {
proc *exec.Cmd // The hashcat process
hashFile *os.File // The file containing the hashes to crack
outFile *os.File // The file to write cracked hashes to
charsetFiles []*os.File // Charset files for mask attacks
shardedCharsetFile *os.File // Sharded charset file for mask attacks
CrackedHashes chan Result // Channel to send cracked hashes to
StatusUpdates chan Status // Channel to send status updates to
StderrMessages chan string // Channel to send stderr messages to
StdoutLines chan string // Channel to send stdout lines to
DoneChan chan error // Channel to send the done signal to
SkipStatusUpdates bool // Whether to skip sending status updates
}
// Start starts the hashcat session by attaching the stdout and stderr pipes,
// starting the hashcat process, and setting up goroutines to handle the output.
// It returns an error if any of the steps fail.
func (sess *Session) Start() error {
pStdout, err := sess.proc.StdoutPipe()
if err != nil {
return fmt.Errorf("couldn't attach stdout to hashcat: %w", err)
}
pStderr, err := sess.proc.StderrPipe()
if err != nil {
return fmt.Errorf("couldn't attach stderr to hashcat: %w", err)
}
shared.Logger.Debug("Running hashcat command", "command", sess.proc.String())
err = sess.proc.Start()
if err != nil {
return fmt.Errorf("couldn't start hashcat: %w", err)
}
tailer, err := tail.TailFile(sess.outFile.Name(), tail.Config{Follow: true, Logger: shared.Logger.StandardLog()})
if err != nil {
err = sess.Kill()
if err != nil {
shared.Logger.Error("couldn't kill hashcat process", "error", err)
}
return fmt.Errorf("couldn't tail outfile %q: %w", sess.outFile.Name(), err)
}
// Read the tailer output in a separate goroutine
go func() {
for tLine := range tailer.Lines {
line := tLine.Text
values := strings.Split(line, ":")
if len(values) < 3 {
shared.Logger.Error("unexpected line contents", "line", line)
continue
}
// First
timestamp := values[0]
// Last
plainHex := values[len(values)-1]
bs, err := hex.DecodeString(plainHex)
if err != nil {
shared.Logger.Error("couldn't decode hex string", "hex", plainHex, "error", err)
continue
}
plain := string(bs)
// Everything in the middle
hashParts := values[1 : len(values)-1]
hash := strings.Join(hashParts, ":")
timestampI, err := convertor.ToInt(timestamp)
if err != nil {
shared.Logger.Error("couldn't parse hashcat timestamp.", "timestamp", timestamp, "error", err)
continue
}
sess.CrackedHashes <- Result{
Timestamp: time.Unix(timestampI, 0),
Hash: hash,
Plaintext: plain,
}
}
}()
// Read the stdout and stderr pipes in separate goroutines
go func() {
scanner := bufio.NewScanner(pStdout)
for scanner.Scan() {
line := scanner.Text()
sess.StdoutLines <- line
if len(line) == 0 {
continue
}
if !sess.SkipStatusUpdates {
if validator.IsJSON(line) {
var status Status
err := json.Unmarshal([]byte(line), &status)
if err != nil {
shared.Logger.Error("WARN: couldn't unmarshal hashcat status", "error", err)
continue
}
sess.StatusUpdates <- status
} else {
shared.Logger.Error("Unexpected stdout line", "line", line)
}
}
}
done := sess.proc.Wait()
time.Sleep(time.Second)
sess.DoneChan <- done
tailer.Kill(nil)
}()
go func() {
scanner := bufio.NewScanner(pStderr)
for scanner.Scan() {
shared.Logger.Error("read stderr", "text", scanner.Text())
sess.StderrMessages <- scanner.Text()
}
}()
return nil
}
// Kill terminates the running process associated with the session.
// If the session process is already terminated or not started, it returns nil.
// If an error occurs while terminating the process, it returns the error.
func (sess *Session) Kill() error {
if sess.proc == nil || sess.proc.Process == nil {
return nil
}
err := sess.proc.Process.Kill()
if errors.Is(err, os.ErrProcessDone) {
return nil
}
return err
}
// Cleanup removes any temporary files associated with the session.
// It deletes the hash file, output file, charset files, and sharded charset file (if present).
func (sess *Session) Cleanup() {
if sess.outFile != nil {
if fileutil.IsExist(sess.outFile.Name()) {
err := fileutil.RemoveFile(sess.outFile.Name())
if err != nil {
shared.Logger.Error("couldn't remove outfile", "file", sess.outFile.Name(), "error", err)
}
}
sess.outFile = nil
}
for _, f := range sess.charsetFiles {
if f != nil {
if fileutil.IsExist(f.Name()) {
err := fileutil.RemoveFile(f.Name())
if err != nil {
shared.Logger.Error("couldn't remove charset file", "file", f.Name(), "error", err)
}
}
}
}
if sess.hashFile != nil {
if fileutil.IsExist(sess.hashFile.Name()) {
err := fileutil.RemoveFile(sess.hashFile.Name())
if err != nil {
shared.Logger.Error("couldn't remove hash file", "file", sess.hashFile.Name(), "error", err)
}
}
sess.hashFile = nil
}
}
// CmdLine returns the command line string used to start the session.
func (sess *Session) CmdLine() string {
return sess.proc.String()
}