-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver_poly.go
More file actions
102 lines (84 loc) · 2.2 KB
/
Copy pathsolver_poly.go
File metadata and controls
102 lines (84 loc) · 2.2 KB
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
package main
import (
"strings"
"unicode"
)
// Common English letter frequency (simplified) for scoring
// E, T, A, O, I, N, S, H, R, D, L, U
var englishFreq = map[byte]float64{
'e': 12.7, 't': 9.1, 'a': 8.2, 'o': 7.5, 'i': 7.0, 'n': 6.7,
's': 6.3, 'h': 6.1, 'r': 6.0, 'd': 4.3, 'l': 4.0, 'u': 2.8,
' ': 15.0, // Space is very common
}
// SolveSingleByteXOR attempts to break single-byte XOR
func SolveSingleByteXOR(input []byte) (string, byte, float64) {
bestScore := 0.0
bestRes := ""
bestKey := byte(0)
for k := 0; k < 256; k++ {
key := byte(k)
decoded := make([]byte, len(input))
score := 0.0
// XOR and Score
for i, b := range input {
dec := b ^ key
decoded[i] = dec
// Scoring
lower := byte(unicode.ToLower(rune(dec)))
if val, ok := englishFreq[lower]; ok {
score += val
} else if dec < 32 || dec > 126 {
// Penalize non-printable chars heavily
if dec != '\n' && dec != '\r' && dec != '\t' {
score -= 10.0
}
}
}
resStr := string(decoded)
// Magic Check: Instant Win
if strings.Contains(resStr, "picoCTF{") || strings.Contains(resStr, "HTB{") {
return resStr, key, 1000.0 // Max confidence
}
if score > bestScore {
bestScore = score
bestRes = resStr
bestKey = key
}
}
return bestRes, bestKey, bestScore
}
// SolveVigenere attempts a dictionary attack on Vigenère cipher
func SolveVigenere(input string) (string, string) {
// Embedded dictionary
keys := []string{"CYLAB", "PICO", "FLAG", "ADMIN", "PASSWORD"}
for _, key := range keys {
decoded := vigenereDecrypt(input, key)
// Check for flag prefix
if strings.Contains(decoded, "picoCTF{") || strings.Contains(decoded, "HTB{") {
return decoded, key
}
}
return "", ""
}
func vigenereDecrypt(input, key string) string {
var result strings.Builder
keyIndex := 0
keyRunes := []rune(strings.ToUpper(key))
for _, r := range input {
if !unicode.IsLetter(r) {
result.WriteRune(r)
continue
}
shift := keyRunes[keyIndex%len(keyRunes)] - 'A'
if unicode.IsUpper(r) {
// (C - K + 26) % 26
dec := 'A' + (r-'A'-shift+26)%26
result.WriteRune(dec)
} else {
dec := 'a' + (r-'a'-shift+26)%26
result.WriteRune(dec)
}
keyIndex++
}
return result.String()
}