-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalipo.py
169 lines (135 loc) · 9.03 KB
/
alipo.py
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
#a = "0.0004756 0.0001972 0.0036732 0.001252 0.0001824 0.0107628 0.003634 0.0001748 0.0322912 0.0012704 0.0036524 0.0001884 0.0001948 0.0108268 0.0107328 0.0107456 0.0036696 0.0004688 0.0004568 0.0106984 0.0004276 0.0036256 0.0036364 0.0036476 0.11784 0.0002028 0.0012032 0.0035936 0.000494 0.0012448 0.0036452 0.003594 0.0107352 0.001268 0.0013044 0.0012648 0.0035912 0.0001876 0.0004312 0.0004276 0.0109616 0.0004616 0.0004644 0.0004588 0.0004564 0.0106816 0.0035708 0.0036572 0.0004572 0.0036068 0.0001808 0.003606 0.0108044 0.0036356 0.0001792 0.0107628 0.0001936 0.0012232 0.003646 0.001232 0.0036284 0.0012596 0.0036596 0.0004792 0.0012812 0.001272 0.001262 0.003654 0.003608 0.0012568 0.0012188 0.001238 0.010862 0 0.0107972 0.003556 0.0012616 0.0004708 0.0004396 0.0012424 0.00019 0.0012324 0.000202 0.0012972 0.0004596 0.001218 0.0037032 0.0002036 0.0004832 0.000448 0.0107764 0.00046 0.0004476 0.0004592 0.0012036 0.001274 0.0036804 0.0001896 0.03208 0.0012456 0.0012112 0.0317612 0.000456 0.0012264 0.0001948 0.0004672 0.0004484 0.0108816 0.0012044 0.0004624 0.0004444 0.0036108 0.0035884 0.0036568 0.0012496 0.0036908 0.00365 0.0004628 0.0004564 0.0001972 0.0001912 0.0012632 0.0001032 0.001216 0.0004492 0.0324204 0.0035908 0.0108068 0.001264 0.0319412 0.003682 0.0002004 0.0012456 0.0012208 0.000454 0.0012636 0.001238 0.0001908 0.0012632 0.0036716 0.0036044 0.0107588 0.0036068 0.0004776 0.0004704 0.001256 0.0004804 0.0012416 0.0012752 0.010778 0.0012592 0.0004652 0.001222 0.0004764 0.0036224 0.0001032 0.0108044 0.0108916 0.0012404 0.003664 0.0036388 0.0004688 0.0004352 0.0036552 0.0012716 0.000108 0.0012736 0.0036244 0.001272 0.001256 0.001244 0.003632 0.0001184 0.0108424 0.0036464 0.000444 0.000456 0.0037008 0.0036964 0.00367 0.0012732 0.0012044 0.0107352 0.0004496 0.003716 0.00123 0.0004488 0.0322992 0.0012456 0.000446 0.0001804 0.0012472 0.0001988 0.0004416 0.0037188 0.0004464 0.001274 0.003628 0.001264 0.0012264 0.001262 0.0012304 0.000102 0.0004464 0.000424 0.0001912 0.0107724 0.0036556 0.0001104 0.000446 0.0012776 0.0036672 0.0036308 0.0036388 0.0108488 0.0106552 0.0001884 0.0004592 0.0001856 0.0004604 0.003616 0.000116 0.0004692 0.001248 0.0012292 0.000198 0.00045 0.0106788 0.0012344 0.0001904 9.52e-05 0.000436 0.0013084 0.001258 0.0108164 0.0001892 0.001252 0.032064 0.0004536 0.0004684 0.0036496 0.0004308 0.0012524 0.0001936 0.0106916 0.0004496 0.0107804 0.0036628 0.0012644 0.0012244 0.0322612 0.0012484 0.0035888 0.001234 0.001292 0.0036768"
a = "0.0003036 9.172e-05 0.00322404 0.001118 0.00010168 0.0107917 0.00331576 9.564e-05 0.0334059 0.00107656 0.00330068 8.732e-05 9.876e-05 0.0102379 0.0102611 0.010604 0.0033968 0.00033584 0.00032132 0.0107152 0.00030628 0.00309592 0.00335464 0.0032688 0.146775 0.0001016 0.0010324 0.00335216 0.00032524 0.00106712 0.00336308 0.00330932 0.0108183 0.00101264 0.00102948 0.00103852 0.00337588 8.996e-05 0.00031524 0.0003356 0.0102015 0.00031564 0.00029828 0.00031148 0.00032824 0.0102971 0.00338084 0.00317452 0.00033624 0.00321776 9.584e-05 0.00337348 0.0107552 0.0032136 8.496e-05 0.0107848 8.696e-05 0.00102664 0.00338868 0.00101204 0.00326036 0.00107888 0.00337068 0.00032816 0.00101728 0.00101784 0.00105016 0.00339968 0.00340452 0.001014 0.0010706 0.00098736 0.0105357 0 0.0106899 0.00321872 0.0010168 0.000311 0.00031448 0.00107336 9.224e-05 0.00098808 9.608e-05 0.00105412 0.00032364 0.00100616 0.00337316 9.672e-05 0.00032304 0.00033148 0.0108401 0.0003152 0.00032792 0.0003192 0.00104368 0.00100428 0.00323392 8.46e-05 0.0348223 0.00104224 0.00107752 0.0353048 0.00032232 0.0010896 8.82e-05 0.00032356 0.00033808 0.0106143 0.00099376 0.00033828 0.0003374 0.0032 0.00336672 0.00336356 0.0010522 0.00320504 0.00332084 0.00033256 0.00032164 8.716e-05 9.372e-05 0.00101776 1.012e-05 0.00105092 0.00032196 0.0349643 0.00320608 0.0101653 0.0010716 0.0351942 0.00337708 9.344e-05 0.00102092 0.00098908 0.00034692 0.00102312 0.00102056 9.704e-05 0.00100672 0.00317604 0.0033974 0.0108422 0.0033128 0.00033308 0.00034364 0.001055 0.000312 0.000992 0.00105592 0.0108088 0.00104168 0.00032252 0.00100424 0.00031816 0.00318376 1.628e-05 0.0107656 0.0101614 0.0010022 0.00323104 0.00335532 0.00031368 0.00031492 0.00317516 0.00100208 1.456e-05 0.00100904 0.00338556 0.0010676 0.001038 0.0010518 0.00336264 1.68e-05 0.010656 0.00318088 0.00033972 0.00032808 0.00320164 0.00328968 0.0033406 0.00108304 0.00102972 0.010166 0.00031708 0.00337288 0.00108192 0.00031236 0.0347774 0.00098728 0.00033996 9.036e-05 0.00100456 9.064e-05 0.0003226 0.00314176 0.00031476 0.00100496 0.00328152 0.00108544 0.00104472 0.00102684 0.0010164 1.508e-05 0.00031372 0.00031552 8.668e-05 0.0106805 0.003357 1.468e-05 0.00033572 0.00108204 0.00331476 0.00333768 0.003382 0.0106388 0.0105384 8.68e-05 0.0003192 9.112e-05 0.00031408 0.00322884 1.612e-05 0.0003282 0.00101892 0.00106312 8.86e-05 0.00033624 0.0107809 0.00104324 9.796e-05 1.532e-05 0.00032236 0.0010612 0.00106916 0.0105344 8.512e-05 0.00104048 0.0351028 0.00031824 0.00031704 0.00317456 0.00031872 0.001069 8.212e-05 0.0107833 0.00032728 0.0108354 0.00334072 0.00106444 0.00104728 0.0352092 0.00106364 0.00330352 0.00099364 0.00106384 0.00325924"
b = [float(s) for s in a.split(" ")]
c = [a for a in b]
c.sort()
c = c[::-1]
chrs = "abcdefghijklmnopqrstuvwxyz_{}"
solvec = """for (int i = 0; i < 256; i++)
num[i] = 0;
for (int i = 0; i < 1000000; i++)
{
Byte buf[25];
memset(buf, 0, 25);
make_key(key);
enc(key, buf, 25);
for (int i = 0; i < 25; i++)
num[buf[i]] += 1;
}
for (int i = 0; i < 256; i++)
std::cout<<num[i]/(25 * 1000000)<<" ";"""
def getnth(num, cnt):
i = 0
cnt += 1
while cnt:
if b[i] == num:
cnt -= 1
i += 1
return i-1
def trysolve(s):
outl = []
for byte in s:
out = ""
skipfrq = []
for f in c:
char = getnth(f, skipfrq.count(f))
skipfrq.append(f)
pt = char ^ byte
if chr(pt) in chrs:
out += chr(pt)
outl.append(out)
return outl
path = "./bin/flag"
ls = []
files = ["bin/flag2.txt.enc","bin/flag3.txt.enc"]
for i in files:
ls.append(open(i, "rb").read())
slv = [trysolve(l) for l in ls]
ranks = [[0 for j in range(29)] for i in range(70)]
for l in ls:
ranked = trysolve(l)
for pos in range(len(ranked)):
for letter in ranked[pos]:
ranks[pos][chrs.index(letter)] += ranked[pos].index(letter)
words = open("10k.txt", "r").read().lower().split("\n")
words = [word+"_" for word in words if len(word) >= 3]
#words.append("tjctf{")
#words.append("_")
words.append("of_")
words.append("and_")
words.append("but_")
words.append("it}")
words.remove("ppc_")
words.remove("sat_")
words.remove("cod_")
words.remove("gis_")
words.remove("bed_")
print(ranks)
print("".join([chrs[rank.index(min(rank))] for rank in ranks]))
def rank_guess(ranks, guessed_word):
if len(ranks) < len(guessed_word):
return -1
currRank = 0
for charI in range(len(guessed_word)):
char = guessed_word[charI]
currRank += ranks[charI][chrs.index(char)]
return currRank
def rank_guess_N(ranks, guessed_word, n, prev_rank):
if len(ranks) < len(guessed_word):
return -1
currRank = 0
for charI in range(n, len(guessed_word)):
char = guessed_word[charI]
currRank += ranks[charI][chrs.index(char)]
return currRank + prev_rank * n
nodes = 0
from queue import PriorityQueue
from dataclasses import dataclass, field
from typing import Any
@dataclass(order=True)
class guess:
priority: float
item: str=field(compare=False)
q = PriorityQueue()
def make_rank_table(ranks):
table = []
for n in range(0, 69):
r_guesses = []
for word in words:
rank = 9999099 if (len(word) + n >= 68) else rank_guess_N(ranks, "0"*n + word, n, 0)/(n + len(word))
r_guesses.append(guess(rank, word))
r_guesses.sort()
if (r_guesses[0].priority == 9999099):
table.append([])
else:
table.append(r_guesses)
return table
def getrank(n, des_idx, rank_table):
return rank_table[n][des_idx]
def top_n(c_guess, rank_table, n = 8):
return rank_table[len(c_guess.item)][:n]
N = 8
rank_table = make_rank_table(ranks)
#known = "tjctf{"
#known = "tjctf{this_code_is_"
#known = "tjctf{this_code_is_very_bad_because_you_can_see_penguins_"
known = "tjctf{this_code_is_very_bad_because_you_can_see_penguins_through"
[q.put(guess(f.priority, known + f.item)) for f in top_n(guess(0, known),rank_table, N)]
print(top_n(guess(0, known), rank_table, 32))
ctr = 0
#If you run this, it'll display a fuzzed version of what you might want...
#human can then go in and correct words based on grammer, cahnge the known variable
#etcetc
while q:
curr = q.get()
if (len(curr.item) > 68):
continue
guesses = top_n(curr, rank_table, N)
#print(guesses)
for gue in guesses:
newscore = ((curr.priority * len(curr.item)) + (gue.priority * len(gue.item)))/(len(curr.item) + len(gue.item))
newg = guess(newscore, curr.item + gue.item)
q.put(newg)
ctr +=1
if (ctr % 1000 ==0):
print(newg.item)