-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
325 lines (265 loc) · 6.81 KB
/
main.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"bufio"
"bytes"
"crypto/cipher"
"crypto/des"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"sync/atomic"
"time"
"unicode/utf8"
"golang.org/x/crypto/pbkdf2"
)
func main() {
var CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
sessionFile := flag.String("session", "", "SolarPutty session file path [required]")
wordlistFile := flag.String("wordlist", "", "Wordlist file path")
password := flag.String("password", "", "Password to decrypt the session file")
threads := flag.Int("threads", runtime.NumCPU(), "Number of threads to use")
flag.Parse()
args := os.Args[1:]
if len(args) == 0 {
fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
return
}
if *sessionFile == "" {
fmt.Println("--session is required")
return
}
if *wordlistFile == "" && *password == "" {
fmt.Println("--wordlist or --password is required")
return
}
if *password != "" && *wordlistFile != "" {
fmt.Println("--wordlist and --password cannot be used together")
return
}
if exists, err := fileExists(*sessionFile); !exists {
fmt.Println(err)
return
}
if exists, err := fileExists(*wordlistFile); !exists && *wordlistFile != "" {
fmt.Println(err)
return
}
fmt.Println("-----------------------------------------------------")
fmt.Println("SolarPutty's Sessions Bruteforce Decrypter in go")
fmt.Println("-----------------------------------------------------")
err := runDecrypt(*sessionFile, *wordlistFile, *password, *threads)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("\r-----------------------------------------------------")
}
type cipherStruct struct {
cipherText string
base64Array []byte
salt []byte
iv []byte
encryptedData []byte
}
func newCipherStruct(cipherText string) (cipherStruct, error) {
// Decode base64 input
array, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return cipherStruct{}, err
}
// Extract salt, IV and encrypted data
// Note: TripleDES uses 8-byte block size, so IV must be 8 bytes
salt := array[:24]
iv := array[24:32]
encryptedData := array[48:]
return cipherStruct{
cipherText: cipherText,
base64Array: array,
salt: salt,
iv: iv,
encryptedData: encryptedData,
}, nil
}
func runDecrypt(sessionFile, wordlist, passwordArg string, threads int) error {
fileContent, err := os.ReadFile(sessionFile)
if err != nil {
return err
}
text := string(fileContent)
cipherData, err := newCipherStruct(text)
if err != nil {
return err
}
// Handle single password case
if passwordArg != "" {
decryptedText, err := Decrypt(passwordArg, cipherData)
if err != nil {
return err
}
if output, isValidDecrypt := isValid(decryptedText, passwordArg); isValidDecrypt {
fmt.Println(output)
return nil
}
return errors.New("invalid password unable to decrypt file")
}
// Bruteforce case
passwordsFile, err := os.Open(wordlist)
if err != nil {
return err
}
defer passwordsFile.Close()
totalLines := getLineCount(wordlist)
numWorkers := threads // Use number of CPU threads
passwords := make(chan string, numWorkers)
foundCh := make(chan string)
var attemptCount atomic.Int64
currentPassCh := make(chan string, 1)
startTime := time.Now()
// Start progress printer goroutine
go func() {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
var currentPass string
for range ticker.C {
select {
case pass := <-currentPassCh:
currentPass = pass
default:
}
count := attemptCount.Load()
if count > 0 {
percentComplete := (float64(count) / float64(totalLines)) * 100.0
speed := float64(count) / time.Since(startTime).Seconds()
fmt.Print("\r\033[K")
fmt.Printf("\r[%.2f%% done] [%d/%d] [%.0f p/s] Trying: %s",
percentComplete, count, totalLines, speed, currentPass)
}
}
}()
// Start workers
for i := 0; i < numWorkers; i++ {
go func() {
for pass := range passwords {
select {
case currentPassCh <- pass:
default:
}
decryptedText, err := Decrypt(pass, cipherData)
if err != nil {
continue
}
if output, isValidDecrypt := isValid(decryptedText, pass); isValidDecrypt {
foundCh <- output
return
}
attemptCount.Add(1)
}
}()
}
// Feed passwords to workers
go func() {
scanner := bufio.NewScanner(passwordsFile)
for scanner.Scan() {
select {
case passwords <- scanner.Text():
case <-foundCh: // Exit if password found
return
}
}
close(passwords)
}()
// Wait for success
output := <-foundCh
fmt.Print("\r\033[K\r\n")
fmt.Println(output)
return nil
}
func Decrypt(passPhrase string, cipherData cipherStruct) (string, error) {
// Derive key using PBKDF2
key := pbkdf2.Key([]byte(passPhrase), cipherData.salt, 1000, 24, sha1.New)
// Create triple DES cipher
block, err := des.NewTripleDESCipher(key)
if err != nil {
return "", err
}
// Create CBC decrypter
mode := cipher.NewCBCDecrypter(block, cipherData.iv)
// Create output buffer
decrypted := make([]byte, len(cipherData.encryptedData))
mode.CryptBlocks(decrypted, cipherData.encryptedData)
// Remove PKCS7 padding
paddingLen := int(decrypted[len(decrypted)-1])
if paddingLen > 0 && paddingLen <= len(decrypted) {
decrypted = decrypted[:len(decrypted)-paddingLen]
}
return string(decrypted), nil
}
func isValid(decryptedText, password string) (string, bool) {
if decryptedText[:1] != "{" {
return "", false
}
// Check if the result is valid UTF-8 (meaning the decryption was likely successful)
if !utf8.ValidString(decryptedText) {
return "", false
}
var data map[string]interface{}
err := json.Unmarshal([]byte(decryptedText), &data)
if err != nil {
return "", false
}
prettyData, _ := json.MarshalIndent(data, "", " ")
output := fmt.Sprintf("password: %s \n -> \n%s\n", password, prettyData)
return output, true
}
func getLineCount(file_path string) int {
file, err := os.Open(file_path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
defaultSize := bufio.MaxScanTokenSize
defaultEndLine := "\n"
sepByte := []byte(defaultEndLine)[0]
buf := make([]byte, defaultSize)
var count int
for {
bufferSize, err := file.Read(buf)
if err != nil && err != io.EOF {
return 0
}
var buffPosition int
for {
i := bytes.IndexByte(buf[buffPosition:], sepByte)
if i == -1 || bufferSize == buffPosition {
break
}
buffPosition += i + 1
count++
}
if err == io.EOF {
break
}
}
return count
}
func fileExists(file string) (bool, error) {
fileStat, err := os.Stat(file)
if errors.Is(err, os.ErrNotExist) {
return false, fmt.Errorf("file %s does not exist", file)
}
if err != nil {
return false, err
}
if fileStat.IsDir() {
return false, fmt.Errorf("%s is a directory", file)
}
return true, nil
}