<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>CTF | Bo Li</title><link>https://www.morvanli.top/tags/ctf/</link><atom:link href="https://www.morvanli.top/tags/ctf/index.xml" rel="self" type="application/rss+xml"/><description>CTF</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>©2020</copyright><lastBuildDate>Wed, 05 Feb 2020 00:00:00 +0000</lastBuildDate><image><url>https://www.morvanli.top/images/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_2.png</url><title>CTF</title><link>https://www.morvanli.top/tags/ctf/</link></image><item><title>Solving Elliptic Curve Discrete Log by CADO-NFS</title><link>https://www.morvanli.top/post/acebearctf2019/</link><pubDate>Wed, 05 Feb 2020 00:00:00 +0000</pubDate><guid>https://www.morvanli.top/post/acebearctf2019/</guid><description>&lt;div class="sect1">
&lt;h2 id="_challenge">Challenge&lt;/h2>
&lt;div class="sectionbody">
&lt;div class="paragraph">
&lt;p>&lt;a href="cotan.pdf">Challenge description&lt;/a> and &lt;a href="cotan.py">script&lt;/a> can be download &lt;a href="cotan.rar">here&lt;/a>.&lt;/p>
&lt;/div>
&lt;/div>
&lt;/div>
&lt;div class="sect1">
&lt;h2 id="_solution">Solution&lt;/h2>
&lt;div class="sectionbody">
&lt;div class="paragraph">
&lt;p>By some magic, &lt;a href="https://efiens.com/member#Core%20member6">@196&lt;/a> is able to convert the base to traditional DLP problem&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>p = 1361129467683753853853498429727072846149
g = 937857192022401732022326285294515252367
h = 71727917161216204087973385053390831556&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Let&amp;#8217;s factor order of the group &lt;code>p&lt;/code>:&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>sage: factor(p-1)
2^2 * 340282366920938463463374607431768211537&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>We call the sub-order of the group is &lt;code>ell&lt;/code>, so we have:&lt;/p>
&lt;/div>
&lt;div class="ulist">
&lt;ul>
&lt;li>
&lt;p>ell_small = 2&lt;/p>
&lt;/li>
&lt;li>
&lt;p>ell_big = 340282366920938463463374607431768211537&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/div>
&lt;/div>
&lt;/div>
&lt;div class="sect1">
&lt;h2 id="_calculate_dlp">Calculate DLP&lt;/h2>
&lt;div class="sectionbody">
&lt;div class="paragraph">
&lt;p>Using CADO-NFS with two parameters like this:&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>./cado-nfs.py -dlp -ell ell_big target=h p -t 6&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="ulist">
&lt;ul>
&lt;li>
&lt;p>&lt;code>-dlp&lt;/code>: mean we calculate Discrete Log&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>-ell&lt;/code>: we input the subgroup order&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>target=h p&lt;/code>: we set target to value &lt;code>h&lt;/code>, modulo &lt;code>p&lt;/code>, which mean $2^x = h$ &lt;code>mod p&lt;/code>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>-t 6&lt;/code>: run on 6 cores&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/div>
&lt;div class="admonitionblock note">
&lt;table>
&lt;tr>
&lt;td class="icon">
&lt;div class="title">Note&lt;/div>
&lt;/td>
&lt;td class="content">
We don&amp;#8217;t specify the base here because we will have to calculate the base by ourselves
&lt;/td>
&lt;/tr>
&lt;/table>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="title">Calculate $log_2h$&lt;/div>
&lt;div class="content">
&lt;pre>./cado-nfs.py -dlp -ell 340282366920938463463374607431768211537 target=71727917161216204087973385053390831556 1361129467683753853853498429727072846149 -t 6&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="title">Output $log_2h$&lt;/div>
&lt;div class="content">
&lt;pre>Info:root: p = 1361129467683753853853498429727072846149
Info:root: ell = 340282366920938463463374607431768211537
Info:root: log2 = 171268190177498693892391393563437542649
Info:root: log3 = 83622131975737922567870551344538854285
Info:root: Also check log(target) vs log(2) ...
Info:root: target = 71727917161216204087973385053390831556
Info:root: log(target) = 306425041562113865430846743034062879086
306425041562113865430846743034062879086&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>So we have &lt;code>log_h = 306425041562113865430846743034062879086&lt;/code>&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="title">Calculate $log_2g$&lt;/div>
&lt;div class="content">
&lt;pre>./cado-nfs.py -dlp -ell 340282366920938463463374607431768211537 target=937857192022401732022326285294515252367 1361129467683753853853498429727072846149 -t 6&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="title">Output $log_2g$&lt;/div>
&lt;div class="content">
&lt;pre>Info:root: p = 1361129467683753853853498429727072846149
Info:root: ell = 340282366920938463463374607431768211537
Info:root: log2 = 171268190177498693892391393563437542649
Info:root: log3 = 83622131975737922567870551344538854285
Info:root: Also check log(target) vs log(2) ...
Info:root: target = 937857192022401732022326285294515252367
Info:root: log(target) = 288756149835421404704013074339152764728
288756149835421404704013074339152764728&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>And we have &lt;code>log_g = 288756149835421404704013074339152764728&lt;/code>&lt;/p>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Like classical logarithm algorithm, to have to logarithm base &lt;code>g&lt;/code>, which mean we are going to find $log_gh$ we do: $log(g)/log(h)$&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>sage: log_h * inverse_mod(log_g, ell) % ell
17393774282928096980960357108851791532&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="admonitionblock note">
&lt;table>
&lt;tr>
&lt;td class="icon">
&lt;div class="title">Note&lt;/div>
&lt;/td>
&lt;td class="content">
we only operate on &lt;code>x modulo ell&lt;/code>, not &lt;code>x modulo (p-1)&lt;/code> as we thought.
&lt;/td>
&lt;/tr>
&lt;/table>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Now we have $x=log_g(h)$, next, we check if $g^x = h$ &lt;code>mod p&lt;/code> or not, if it is then problem solved, otherwise we will do Chinese Reminder Theorem to figure out the full &lt;code>x modulo (p-1)&lt;/code>.&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>sage: p = 1361129467683753853853498429727072846149
....: g = 937857192022401732022326285294515252367
....: h = 71727917161216204087973385053390831556
....:
sage: log_h = 306425041562113865430846743034062879086
sage: log_g = 288756149835421404704013074339152764728
sage: x = log_h * inverse_mod(log_g, ell) % ell
sage: power_mod(g, x, p)
71727917161216204087973385053390831556
sage: h
71727917161216204087973385053390831556
sage: assert power_mod(g, x, p) == h&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Alright, seem like the solution is &lt;code>x = 17393774282928096980960357108851791532&lt;/code>.&lt;/p>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Now we are going to decrypt the flag&lt;/p>
&lt;/div>
&lt;div class="listingblock">
&lt;div class="content">
&lt;pre>from pwn import *
from Crypto.Cipher.AES import AESCipher
x = 17393774282928096980960357108851791532
x = hex(x).lstrip('0x')
key = unhex(x).decode('hex')
enc = '4e8f206f074f895bde336601f0c8a2e092f944d95b798b01449e9b155b4ce5a5ae93cc9c677ad942c32d374419d5512c'.decode('hex')
print(AESCipher(key).decrypt(enc))&lt;/pre>
&lt;/div>
&lt;/div>
&lt;div class="paragraph">
&lt;p>And the flag is &lt;code>AceBear{&lt;em>I_h0p3__y0u_3nj0y3d_1t&lt;/em>}&lt;/code>&lt;/p>
&lt;/div>
&lt;div class="paragraph">
&lt;p>&lt;span class="image">&lt;img src="cotan.png" alt="cotan">&lt;/span>&lt;/p>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Damn this challenge name, I kept writing my name instead of "cotan".&lt;/p>
&lt;/div>
&lt;div class="paragraph">
&lt;p>Thanks for reading.&lt;/p>
&lt;/div>
&lt;/div>
&lt;/div></description></item></channel></rss>