Skip to content

Commit c1a5b45

Browse files
mzfrmzfr
authored andcommitted
Solution for hackover
1 parent bd1b96c commit c1a5b45

File tree

5 files changed

+6975
-0
lines changed

5 files changed

+6975
-0
lines changed

INShAck-2019/hackover/Luca.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import random
2+
3+
with open("routes.txt") as f:
4+
routes = f.readlines()
5+
6+
with open("sol.txt") as f:
7+
sol = [l.strip() for l in f.readlines()]
8+
9+
routes = [set(r.strip().split(',')) for r in routes]
10+
11+
routes.sort(key = lambda s: len(s))
12+
13+
def fix_cover(cover):
14+
for r in routes:
15+
if len(cover & r) == 0:
16+
chosen = random.sample(r, 1)[0]
17+
cover.add(chosen)
18+
return cover
19+
20+
cover = set(sol)
21+
22+
def compute_importance(cover, routes):
23+
nodes = list(cover)
24+
importances = []
25+
for n in nodes:
26+
importance = 0
27+
for r in routes:
28+
if n in r:
29+
importance += 1.0 / 10**(len(cover & r))
30+
importances.append(importance)
31+
32+
nodes = [[nodes[i], importances[i]] for i in range(len(nodes))]
33+
nodes.sort(key = lambda s: s[1])
34+
35+
new_cover = set()
36+
for n in nodes[30:]:
37+
new_cover.add(n[0])
38+
return new_cover
39+
40+
41+
score = len(cover)
42+
while True:
43+
new_cover = compute_importance(cover, routes)
44+
new_cover = fix_cover(new_cover)
45+
new_score = len(new_cover)
46+
if new_score < score:
47+
print(new_score)
48+
score = new_score
49+
cover = new_cover
50+
if score == 126:
51+
with open("sol126.txt", "w") as f:
52+
for c in cover:
53+
f.write(c+"\n")
54+
break

INShAck-2019/hackover/Readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# HackCode
2+
3+
__Description__
4+
5+
We have a little budgeting issue with our latest red team campaign. Please help us figure it out :
6+
7+
https://hack-code.ctf.insecurity-insa.fr
8+
9+
This challenge has 4 different flags, better solutions will grant you more flags.
10+
11+
12+
__SOLUTION__
13+
14+
I didn't solve this challenge this my actually solved by __@nullpointer__ and __@luca__.
15+
16+
Basically we need to write an algorithm that is efficient. The more efficient the algorith the more flag you'll get.
17+
18+
This challenge had 4 flags.
19+
20+
First __@NullPointer__ wrote a [script](nullpointer.py) with which we was able to get 128 taps which gave us 3 flags.
21+
22+
```
23+
First flag is INSA{N0t_bad_f0r_a_start}. The next flag will be awarded at <= 135.
24+
```
25+
26+
```
27+
INSA{135_is_pretty_g0Od_but_how_l0w_c4n_u_gO}. Get your next flag at <= 128
28+
```
29+
30+
```
31+
INSA{Getting_cl0ser}. The last flag is waiting for you at 126 !
32+
```
33+
34+
After that lot of people started to optimize the algorithm and finally __@luca__ was able to optimize the [script](luca.py) it to give us 126 taps and we got the final flag.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
with open( 'routes.txt', 'r') as f:
2+
data=f.read().splitlines()
3+
tap=[]
4+
while len(data) > 0:
5+
print( "There are %d routes"%len(data))
6+
conn_stat = {}
7+
conn_route = {}
8+
conn_nr_route={}
9+
i=0
10+
for datum in data:
11+
#print("Analysis of %s"%datum)
12+
connections=datum.split(',')
13+
for connection in connections:
14+
if len(connection) < 5:
15+
continue
16+
#if connection == "e5a10f35":
17+
# print(datum)
18+
#print(connection)
19+
if connection not in conn_stat:
20+
conn_stat[connection]=1
21+
conn_route[connection]=[]
22+
conn_nr_route[connection]=0
23+
else:
24+
#print("Extend")
25+
conn_stat[connection]=conn_stat[connection]+1
26+
if datum not in conn_route[connection]:
27+
conn_route[connection].append( datum )
28+
conn_nr_route[connection] = conn_nr_route[connection] + len( connections )
29+
#conn_route[connection].append( i )
30+
i=i+1
31+
w=0
32+
d=None
33+
n = 0
34+
for k in conn_stat.keys():
35+
if w < conn_stat[k] or d == None:
36+
if d == None or conn_nr_route[k] > conn_nr_route[d]:
37+
d=k
38+
w=conn_stat[k]
39+
print( "Best tap point is %s and it lead to remove %d routes"% (d, len(conn_route[d])) )
40+
#print( conn_route[d] )
41+
tap.append(d)
42+
#print( "Current tap list is ", tap)
43+
for k in conn_route[d]:
44+
data.remove(k)
45+
print( "Finished with %d"%len(tap))
46+
for t in tap:
47+
print(t)

0 commit comments

Comments
 (0)