Skip to content

Commit dea03f8

Browse files
committed
Mar 2019
Add solutions to practice problems of CyberTalents
1 parent 05ac30e commit dea03f8

File tree

7 files changed

+511
-0
lines changed

7 files changed

+511
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def update_str(s, off):\n",
10+
" orig_index = [ord(c)-ord('a') if not (c>='0' and c<='9') else c for c in s]\n",
11+
" modified_index = [chr(ord('a') + ((ind + off) %26)) if (isinstance(ind, int)) else ind for ind in orig_index]\n",
12+
" return ''.join(modified_index)"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"def update_sentence(s, off):\n",
22+
" return ' '.join([update_str(st, off) for st in s.split()])"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"name": "stdout",
32+
"output_type": "stream",
33+
"text": [
34+
"1 hvs tzou wg 2x68mtvezn qh wg dfshhm sogm hc gss hvs tzou pih qob mci gss wh w hccy bsofzm 1 awbihs hc sbqcrs hvwg kwhv zwb13 uccr ziqy wb gczjwbu hvoh\n",
35+
"2 iwt uapv xh 2y68nuwfao ri xh egtiin tphn id htt iwt uapv qji rpc ndj htt xi x iddz ctpgan 1 bxcjit id tcrdst iwxh lxiw axc13 vdds ajrz xc hdakxcv iwpi\n",
36+
"3 jxu vbqw yi 2z68ovxgbp sj yi fhujjo uqio je iuu jxu vbqw rkj sqd oek iuu yj y jeea duqhbo 1 cydkju je udsetu jxyi myjx byd13 weet bksa yd ieblydw jxqj\n",
37+
"4 kyv wcrx zj 2a68pwyhcq tk zj givkkp vrjp kf jvv kyv wcrx slk tre pfl jvv zk z kffb evricp 1 dzelkv kf vetfuv kyzj nzky cze13 xffu cltb ze jfcmzex kyrk\n",
38+
"5 lzw xdsy ak 2b68qxzidr ul ak hjwllq wskq lg kww lzw xdsy tml usf qgm kww al a lggc fwsjdq 1 eafmlw lg wfugvw lzak oalz daf13 yggv dmuc af kgdnafy lzsl\n",
39+
"6 max yetz bl 2c68ryajes vm bl ikxmmr xtlr mh lxx max yetz unm vtg rhn lxx bm b mhhd gxtker 1 fbgnmx mh xgvhwx mabl pbma ebg13 zhhw envd bg lheobgz matm\n",
40+
"7 nby zfua cm 2d68szbkft wn cm jlynns yums ni myy nby zfua von wuh sio myy cn c niie hyulfs 1 gchony ni yhwixy nbcm qcnb fch13 aiix fowe ch mifpcha nbun\n",
41+
"8 ocz agvb dn 2e68taclgu xo dn kmzoot zvnt oj nzz ocz agvb wpo xvi tjp nzz do d ojjf izvmgt 1 hdipoz oj zixjyz ocdn rdoc gdi13 bjjy gpxf di njgqdib ocvo\n",
42+
"9 pda bhwc eo 2f68ubdmhv yp eo lnappu awou pk oaa pda bhwc xqp ywj ukq oaa ep e pkkg jawnhu 1 iejqpa pk ajykza pdeo sepd hej13 ckkz hqyg ej okhrejc pdwp\n",
43+
"10 qeb cixd fp 2g68vceniw zq fp mobqqv bxpv ql pbb qeb cixd yrq zxk vlr pbb fq f qllh kbxoiv 1 jfkrqb ql bkzlab qefp tfqe ifk13 dlla irzh fk plisfkd qexq\n",
44+
"11 rfc djye gq 2h68wdfojx ar gq npcrrw cyqw rm qcc rfc djye zsr ayl wms qcc gr g rmmi lcypjw 1 kglsrc rm clambc rfgq ugrf jgl13 emmb jsai gl qmjtgle rfyr\n",
45+
"12 sgd ekzf hr 2i68xegpky bs hr oqdssx dzrx sn rdd sgd ekzf ats bzm xnt rdd hs h snnj mdzqkx 1 lhmtsd sn dmbncd sghr vhsg khm13 fnnc ktbj hm rnkuhmf sgzs\n",
46+
"13 the flag is 2j68yfhqlz ct is pretty easy to see the flag but can you see it i took nearly 1 minute to encode this with lin13 good luck in solving that\n",
47+
"14 uif gmbh jt 2k68zgirma du jt qsfuuz fbtz up tff uif gmbh cvu dbo zpv tff ju j uppl ofbsmz 1 njovuf up fodpef uijt xjui mjo13 hppe mvdl jo tpmwjoh uibu\n",
48+
"15 vjg hnci ku 2l68ahjsnb ev ku rtgvva gcua vq ugg vjg hnci dwv ecp aqw ugg kv k vqqm pgctna 1 okpwvg vq gpeqfg vjku ykvj nkp13 iqqf nwem kp uqnxkpi vjcv\n",
49+
"16 wkh iodj lv 2m68biktoc fw lv suhwwb hdvb wr vhh wkh iodj exw fdq brx vhh lw l wrrn qhduob 1 plqxwh wr hqfrgh wklv zlwk olq13 jrrg oxfn lq vroylqj wkdw\n",
50+
"17 xli jpek mw 2n68cjlupd gx mw tvixxc iewc xs wii xli jpek fyx ger csy wii mx m xsso rievpc 1 qmryxi xs irgshi xlmw amxl pmr13 kssh pygo mr wspzmrk xlex\n",
51+
"18 ymj kqfl nx 2o68dkmvqe hy nx uwjyyd jfxd yt xjj ymj kqfl gzy hfs dtz xjj ny n yttp sjfwqd 1 rnszyj yt jshtij ymnx bnym qns13 ltti qzhp ns xtqansl ymfy\n",
52+
"19 znk lrgm oy 2p68elnwrf iz oy vxkzze kgye zu ykk znk lrgm haz igt eua ykk oz o zuuq tkgxre 1 sotazk zu ktiujk znoy cozn rot13 muuj raiq ot yurbotm zngz\n",
53+
"20 aol mshn pz 2q68fmoxsg ja pz wylaaf lhzf av zll aol mshn iba jhu fvb zll pa p avvr ulhysf 1 tpubal av lujvkl aopz dpao spu13 nvvk sbjr pu zvscpun aoha\n",
54+
"21 bpm ntio qa 2r68gnpyth kb qa xzmbbg miag bw amm bpm ntio jcb kiv gwc amm qb q bwws vmiztg 1 uqvcbm bw mvkwlm bpqa eqbp tqv13 owwl tcks qv awtdqvo bpib\n",
55+
"22 cqn oujp rb 2s68hoqzui lc rb yancch njbh cx bnn cqn oujp kdc ljw hxd bnn rc r cxxt wnjauh 1 vrwdcn cx nwlxmn cqrb frcq urw13 pxxm udlt rw bxuerwp cqjc\n",
56+
"23 dro pvkq sc 2t68ipravj md sc zboddi okci dy coo dro pvkq led mkx iye coo sd s dyyu xokbvi 1 wsxedo dy oxmyno drsc gsdr vsx13 qyyn vemu sx cyvfsxq drkd\n",
57+
"24 esp qwlr td 2u68jqsbwk ne td acpeej pldj ez dpp esp qwlr mfe nly jzf dpp te t ezzv yplcwj 1 xtyfep ez pynzop estd htes wty13 rzzo wfnv ty dzwgtyr esle\n",
58+
"25 ftq rxms ue 2v68krtcxl of ue bdqffk qmek fa eqq ftq rxms ngf omz kag eqq uf u faaw zqmdxk 1 yuzgfq fa qzoapq ftue iuft xuz13 saap xgow uz eaxhuzs ftmf\n",
59+
"26 gur synt vf 2w68lsudym pg vf cerggl rnfl gb frr gur synt ohg pna lbh frr vg v gbbx arneyl 1 zvahgr gb rapbqr guvf jvgu yva13 tbbq yhpx va fbyivat gung\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"s= 'gur synt vf 2w68lsudym Vg vf cerggl rnfl gb frr gur synt ohg pna lbh frr vg v gbbx arneyl 1 zvahgr gb rapbqr guvf jvgu EBG13 tbbq yhpx va fbyivat gung'\n",
65+
"for offset in range(1, 27):\n",
66+
" print(offset, update_sentence(s, offset))"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 4,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"'the flag is 2j68yfhqlz ct is pretty easy to see the flag but can you see it i took nearly 1 minute to encode this with lin13 good luck in solving that'"
78+
]
79+
},
80+
"execution_count": 4,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"update_sentence(s, 13)"
87+
]
88+
}
89+
],
90+
"metadata": {
91+
"kernelspec": {
92+
"display_name": "Python 3",
93+
"language": "python",
94+
"name": "python3"
95+
},
96+
"language_info": {
97+
"codemirror_mode": {
98+
"name": "ipython",
99+
"version": 3
100+
},
101+
"file_extension": ".py",
102+
"mimetype": "text/x-python",
103+
"name": "python",
104+
"nbconvert_exporter": "python",
105+
"pygments_lexer": "ipython3",
106+
"version": "3.6.6"
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 2
111+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import sys
3+
from struct import pack, unpack
4+
5+
def F(w):
6+
return ((w * 31337) ^ (w * 1337 >> 16)) % 2**32
7+
8+
def encrypt(block):
9+
a, b, c, d = unpack("<4I", block)
10+
for rno in xrange(32):
11+
a, b, c, d = b ^ F(a | F(c ^ F(d)) ^ F(a | c) ^ d), c ^ F(a ^ F(d) ^ (a | d)), d ^ F(a | F(a) ^ a), a ^ 31337
12+
a, b, c, d = c ^ F(d | F(b ^ F(a)) ^ F(d | b) ^ a), b ^ F(d ^ F(a) ^ (d | a)), a ^ F(d | F(d) ^ d), d ^ 1337
13+
return pack("<4I", a, b, c, d)
14+
15+
pt = open(sys.argv[1]).read()
16+
while len(pt) % 16: pt += "#"
17+
18+
ct = "".join(encrypt(pt[i:i+16]) for i in xrange(0, len(pt), 16))
19+
open(sys.argv[1] + ".enc", "w").write(ct)

CyberTalents/cryptography/assets/genfei/flag.enc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
P��ي ܮ��hq�>ޒ�b ��M�S\��j
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
15345857135052644158 * x[0] mod 15914389274045831441 = 10753153698913165324
2+
10107862342967460188 * x[1] mod 12471333000718257439 = 8412981602133999892
3+
15514951512896411 * x[2] mod 11189085043185963643 = 1482464683316586574
4+
4918093547848646552 * x[3] mod 14953553045254805869 = 2025900790652430240
5+
9458472078791077712 * x[4] mod 16969044464796096757 = 5398900907079313999
6+
10389810153032407159 * x[5] mod 12219369241978883401 = 7029759589492702570
7+
10317166141181737080 * x[6] mod 11051035063490494121 = 3468633932944308360
8+
4739071230119101568 * x[7] mod 12446419077417014895 = 8675623443003281737
9+
5602877978471499087 * x[8] mod 17033822019842116078 = 10322234074859615825
10+
6817739495547298027 * x[9] mod 9516363895804911673 = 4437950083761802261
11+
6427678547440599488 * x[10] mod 15079736889469193431 = 3279819731898553304
12+
9457480281190568299 * x[11] mod 10650632135951148921 = 9305609025967996118
13+
3811243068786264022 * x[12] mod 17812647846133398533 = 11526794456945961531
14+
775941643434169870 * x[13] mod 11412472367550805013 = 4738382726559211541
15+
7024610526778714279 * x[14] mod 9778393688778074575 = 5323545124401969226
16+
11651575732234344955 * x[15] mod 16630642531754496501 = 2873213552124115822
17+
6625087805180364133 * x[16] mod 9364964556418467329 = 2787565658998643270
18+
3669801280657684382 * x[17] mod 14097382369878612419 = 7567005815588497736
19+
5443984100737085739 * x[18] mod 10512797518085747507 = 7873334411288369657
20+
12346616364541216766 * x[19] mod 15943449689777265613 = 3904160665454908058
21+
8986565623928863916 * x[20] mod 18324134584075223407 = 188593124624644258
22+
944611705353292388 * x[21] mod 11593718893451959913 = 5953933343058555785
23+
9480419754515455118 * x[22] mod 14797616246348898583 = 13228205597299662061
24+
6357470584650383564 * x[23] mod 10888050221402213645 = 3331190950406564692
25+
4804105847411349449 * x[24] mod 15964574501738046999 = 9808705001033677355
26+
The flag is chr(x[0]%256)+chr(x[1]%256)+...
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# The encryption algorithm"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import sys\n",
17+
"from struct import pack, unpack\n",
18+
"\n",
19+
"def F(w):\n",
20+
" return ((w * 31337) ^ (w * 1337 >> 16)) % 2**32\n",
21+
"\n",
22+
"def encrypt(block):\n",
23+
" a, b, c, d = unpack(\"<4I\", block)\n",
24+
" for rno in xrange(32):\n",
25+
" a, b, c, d = b ^ F(a | F(c ^ F(d)) ^ F(a | c) ^ d), c ^ F(a ^ F(d) ^ (a | d)), d ^ F(a | F(a) ^ a), a ^ 31337\n",
26+
" a, b, c, d = c ^ F(d | F(b ^ F(a)) ^ F(d | b) ^ a), b ^ F(d ^ F(a) ^ (d | a)), a ^ F(d | F(d) ^ d), d ^ 1337\n",
27+
" return pack(\"<4I\", a, b, c, d)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"# Reverse engineering the encryption algorithm\n",
35+
"- First it's noticed that the algorithm works on blocks of 16 bytes\n",
36+
"- Each block is divided into four 4-byte ints (a,b,c,d)\n",
37+
"- 32 Rounds are performed on these ints\n",
38+
"- Each round performs two operations on these 4 ints\n",
39+
"- We will need to figure out a way to reverse these functions"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 2,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"def reverse_second_op(a1, b1, c1, d1):\n",
49+
" # Variables should be decoded in the given sequence d0, a0, b0, c0\n",
50+
" d0 = d1 ^ 1337\n",
51+
" \n",
52+
" fd0 = F(d0 | F(d0) ^ d0)\n",
53+
" a0 = c1 ^ fd0\n",
54+
"\n",
55+
" fad0 = F(d0 ^ F(a0) ^ (d0 | a0))\n",
56+
" b0 = b1 ^ fad0\n",
57+
"\n",
58+
" fabd0 = F(d0 | F(b0 ^ F(a0)) ^ F(d0 | b0) ^ a0)\n",
59+
"\n",
60+
" c0 = a1 ^ fabd0\n",
61+
" \n",
62+
" return a0, b0, c0, d0"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 3,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"def reverse_first_op(a1, b1, c1, d1):\n",
72+
" a0 = d1 ^ 31337\n",
73+
" \n",
74+
" fa0 = F(a0 | F(a0) ^ a0)\n",
75+
" d0 = c1 ^ fa0\n",
76+
" \n",
77+
" fad0 = F(a0 ^ F(d0) ^ (a0 | d0))\n",
78+
" c0 = b1 ^ fad0\n",
79+
" \n",
80+
" facd0 = F(a0 | F(c0 ^ F(d0)) ^ F(a0 | c0) ^ d0)\n",
81+
" b0 = a1 ^ facd0\n",
82+
" \n",
83+
" return a0, b0, c0, d0"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"def decrypt(block):\n",
93+
" a, b, c, d = unpack(\"<4I\", block)\n",
94+
" for rno in range(32):\n",
95+
" # Second operation is reversed first\n",
96+
" a, b, c, d = reverse_second_op(a, b, c, d)\n",
97+
" # First operation is reversed\n",
98+
" a, b, c, d = reverse_first_op(a, b, c, d)\n",
99+
" \n",
100+
" return pack(\"<4I\", a, b, c, d)"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"# How to encrypt a new message?"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 5,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"def encrypt_file(file_name):\n",
117+
" pt = open(file_name).read()\n",
118+
" while len(pt) % 16: pt += \"#\"\n",
119+
"\n",
120+
" ct = \"\".join(encrypt(pt[i:i+16]) for i in xrange(0, len(pt), 16))\n",
121+
" open(file_name + \".enc\", \"w\").write(ct)"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"metadata": {},
127+
"source": [
128+
"# How to decrypt an encrypted message?"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 6,
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"def decrypt_file(file_name):\n",
138+
" pt = open(file_name, 'rb').read()\n",
139+
" # No need to add any padding\n",
140+
"\n",
141+
" # Decode the bytes stream using .decode() before joining the list\n",
142+
" ct = ''.join([decrypt(pt[i:i+16]).decode() for i in range(0, len(pt), 16)])\n",
143+
" print(ct)"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 7,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"FLAG{G3N3R4L123D_F31573L_EZ!}###\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"decrypt_file('assets/genfei/flag.enc')"
161+
]
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "Python 3",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.6.6"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 2
185+
}

0 commit comments

Comments
 (0)