From fbc55aa0754cf6c1f8ead80b40e1374266688754 Mon Sep 17 00:00:00 2001 From: Souvlakia <77302709+souvlakias@users.noreply.github.com> Date: Tue, 2 Jul 2024 01:18:21 +0300 Subject: [PATCH] (crypto) added 2 Challs (beginner&easy) Added beginner: rotten-salad easy: babyrsa --- crypto/babyrsa/challenge.yml | 27 ++++++++++++++++++++ crypto/babyrsa/public/output.txt | 5 ++++ crypto/babyrsa/public/source.py | 37 ++++++++++++++++++++++++++++ crypto/babyrsa/setup/output.txt | 5 ++++ crypto/babyrsa/setup/secret.py | 1 + crypto/babyrsa/setup/source.py | 37 ++++++++++++++++++++++++++++ crypto/babyrsa/sol/README.md | 30 ++++++++++++++++++++++ crypto/babyrsa/sol/solve.py | 24 ++++++++++++++++++ crypto/rotten-salad/challenge.yml | 26 +++++++++++++++++++ crypto/rotten-salad/public/salad.txt | 5 ++++ crypto/rotten-salad/setup/salad.md | 1 + crypto/rotten-salad/sol/README.md | 1 + 12 files changed, 199 insertions(+) create mode 100644 crypto/babyrsa/challenge.yml create mode 100644 crypto/babyrsa/public/output.txt create mode 100644 crypto/babyrsa/public/source.py create mode 100644 crypto/babyrsa/setup/output.txt create mode 100644 crypto/babyrsa/setup/secret.py create mode 100644 crypto/babyrsa/setup/source.py create mode 100644 crypto/babyrsa/sol/README.md create mode 100644 crypto/babyrsa/sol/solve.py create mode 100644 crypto/rotten-salad/challenge.yml create mode 100644 crypto/rotten-salad/public/salad.txt create mode 100644 crypto/rotten-salad/setup/salad.md create mode 100644 crypto/rotten-salad/sol/README.md diff --git a/crypto/babyrsa/challenge.yml b/crypto/babyrsa/challenge.yml new file mode 100644 index 0000000..2d2a875 --- /dev/null +++ b/crypto/babyrsa/challenge.yml @@ -0,0 +1,27 @@ +name: "babyrsa" +author: "souvlakia" +category: crypto + +description: | + Fundamentals of RSA. Can you decrypt the message? + +value: 500 +type: dynamic +extra: + initial: 500 + minimum: 50 + decay: 25 + +flags: + - GTBQ{g0tt4_l0v3_GCD_&_mult1_Pr1me_rs4} + +files: + - public/source.py + - public/output.txt + +tags: + - crypto + - beginner + +state: visible +version: "0.1" \ No newline at end of file diff --git a/crypto/babyrsa/public/output.txt b/crypto/babyrsa/public/output.txt new file mode 100644 index 0000000..0918e34 --- /dev/null +++ b/crypto/babyrsa/public/output.txt @@ -0,0 +1,5 @@ +n = 136129039934906799245883537600820362325696322589710411590186897335243686579104762065743976573291702750635108717889410733229115423708058508480236948810672363347991651420293334839308943729580241028725894733461551038888847568419901086597954491530529868885698368322552732418869647636401840431554294300907359920151 +v = 145336505618051732550698474645594181799147590575859902257619187994378559013363268655950104864569764056670639713801017855616689453650198563048002837724039970159502938997236965318015229229549970355881337278281585150146306096405515477715640494716527445680613383929607132555875322057128548552200870650696580132521 +w = 20612907913751940701434586234377070995512938354700166339929030278278014092311579879497093885185859184642701850534088712075806695131108284229597532840569152216986793187613838685954549359681582763903398538637932366747956519457275203090897430583637500207802799220582108236286968780561616121115054842835633027356029114516812021084434276111757860109305675350998033334000574804620501872783275490902197769254918685821157170228694510593644625793650591298037557438014165246072463794441871325588124086650937138575432899096783647585355882931580034904138718605575594187984051923588891709878571484103146803169259247375536859708763 +c2 = 40999996801873802742121997119730707835375659336586118409913738383370584468803123294259818676510856232905858635994052230030123318196378107746038600276544711492946157137781760844236859181993407369879174109744518026199170698597057199969985775158762539533164627602726855551347620475705167278122876349988039969692 +c3 = 3139944035622906500446122298196738357051827709726750721775633031452787107263389042610582535666920737943985088630120852223041567466431979061050093583701100302275054497243398106197255754467807652090694088800711075375412435711585892522866917133467033013010802142851893437909298773235587564677457793658655780592766302182404628855729480554049343600184461607330760299602586313139207785995916257882342396276534435829220426449740533729325143603438108068736676574488481496262952017763353301317940181413229422681271004713688641645933690837569274536827363723993636423240827193221091820001434531563287145275345624041360425385072 diff --git a/crypto/babyrsa/public/source.py b/crypto/babyrsa/public/source.py new file mode 100644 index 0000000..b51cb5a --- /dev/null +++ b/crypto/babyrsa/public/source.py @@ -0,0 +1,37 @@ +from Crypto.Util.number import getPrime, bytes_to_long, long_to_bytes +from secret import FLAG + +def encrypt(m:str, e, n): # encrypt message with public key + m=m.encode() # convert to bytes + m=bytes_to_long(m) # convert to integer + return pow(m, e, n) + +def decrypt(c:int, n, prime_factors:list): # decrypt message if we know the prime factors of n + phi=1 + for p in prime_factors: + phi*=(p-1) + d=pow(e, -1, phi) + m=pow(c, d, n) + return long_to_bytes(m).decode() + + +if __name__ == '__main__': + e=65537 + p=getPrime(512) + q=getPrime(512) + n=p*q + + + r=getPrime(512) + v=q*r + + k=getPrime(512) + w=p*q*r*k + + print(f'{n = }') + print(f'{v = }') + print(f'{w = }') + + # print(f'c1 = {encrypt(FLAG, e, n)}') + print(f'c2 = {encrypt(FLAG[:20], e, v)}') + print(f'c3 = {encrypt(FLAG[20:], e, w)}') \ No newline at end of file diff --git a/crypto/babyrsa/setup/output.txt b/crypto/babyrsa/setup/output.txt new file mode 100644 index 0000000..0918e34 --- /dev/null +++ b/crypto/babyrsa/setup/output.txt @@ -0,0 +1,5 @@ +n = 136129039934906799245883537600820362325696322589710411590186897335243686579104762065743976573291702750635108717889410733229115423708058508480236948810672363347991651420293334839308943729580241028725894733461551038888847568419901086597954491530529868885698368322552732418869647636401840431554294300907359920151 +v = 145336505618051732550698474645594181799147590575859902257619187994378559013363268655950104864569764056670639713801017855616689453650198563048002837724039970159502938997236965318015229229549970355881337278281585150146306096405515477715640494716527445680613383929607132555875322057128548552200870650696580132521 +w = 20612907913751940701434586234377070995512938354700166339929030278278014092311579879497093885185859184642701850534088712075806695131108284229597532840569152216986793187613838685954549359681582763903398538637932366747956519457275203090897430583637500207802799220582108236286968780561616121115054842835633027356029114516812021084434276111757860109305675350998033334000574804620501872783275490902197769254918685821157170228694510593644625793650591298037557438014165246072463794441871325588124086650937138575432899096783647585355882931580034904138718605575594187984051923588891709878571484103146803169259247375536859708763 +c2 = 40999996801873802742121997119730707835375659336586118409913738383370584468803123294259818676510856232905858635994052230030123318196378107746038600276544711492946157137781760844236859181993407369879174109744518026199170698597057199969985775158762539533164627602726855551347620475705167278122876349988039969692 +c3 = 3139944035622906500446122298196738357051827709726750721775633031452787107263389042610582535666920737943985088630120852223041567466431979061050093583701100302275054497243398106197255754467807652090694088800711075375412435711585892522866917133467033013010802142851893437909298773235587564677457793658655780592766302182404628855729480554049343600184461607330760299602586313139207785995916257882342396276534435829220426449740533729325143603438108068736676574488481496262952017763353301317940181413229422681271004713688641645933690837569274536827363723993636423240827193221091820001434531563287145275345624041360425385072 diff --git a/crypto/babyrsa/setup/secret.py b/crypto/babyrsa/setup/secret.py new file mode 100644 index 0000000..32677a7 --- /dev/null +++ b/crypto/babyrsa/setup/secret.py @@ -0,0 +1 @@ +FLAG='GTBQ{g0tt4_l0v3_GCD_&_mult1_Pr1me_rs4}' \ No newline at end of file diff --git a/crypto/babyrsa/setup/source.py b/crypto/babyrsa/setup/source.py new file mode 100644 index 0000000..b51cb5a --- /dev/null +++ b/crypto/babyrsa/setup/source.py @@ -0,0 +1,37 @@ +from Crypto.Util.number import getPrime, bytes_to_long, long_to_bytes +from secret import FLAG + +def encrypt(m:str, e, n): # encrypt message with public key + m=m.encode() # convert to bytes + m=bytes_to_long(m) # convert to integer + return pow(m, e, n) + +def decrypt(c:int, n, prime_factors:list): # decrypt message if we know the prime factors of n + phi=1 + for p in prime_factors: + phi*=(p-1) + d=pow(e, -1, phi) + m=pow(c, d, n) + return long_to_bytes(m).decode() + + +if __name__ == '__main__': + e=65537 + p=getPrime(512) + q=getPrime(512) + n=p*q + + + r=getPrime(512) + v=q*r + + k=getPrime(512) + w=p*q*r*k + + print(f'{n = }') + print(f'{v = }') + print(f'{w = }') + + # print(f'c1 = {encrypt(FLAG, e, n)}') + print(f'c2 = {encrypt(FLAG[:20], e, v)}') + print(f'c3 = {encrypt(FLAG[20:], e, w)}') \ No newline at end of file diff --git a/crypto/babyrsa/sol/README.md b/crypto/babyrsa/sol/README.md new file mode 100644 index 0000000..21c8547 --- /dev/null +++ b/crypto/babyrsa/sol/README.md @@ -0,0 +1,30 @@ +# Solution: +- We can find all the factors one by one by getting the GCD of the moduli. + + +```python +from Crypto.Util.number import bytes_to_long, long_to_bytes, GCD +n = ... +v = ... +w = ... +c2 = ... +c3 = ... +e = 65537 + +def decrypt(c:int, n, prime_factors:list): # decrypt message if we know the prime factors of n + phi=1 + for p in prime_factors: + phi*=(p-1) + d=pow(e, -1, phi) + m=pow(c, d, n) + return long_to_bytes(m).decode() + + +q= GCD(n, v) +p= n//q +r= v//q +k= w//(p*q*r) + +print(decrypt(c2, v, [q,r]), end='') +print(decrypt(c3, w, [p,q,r,k])) +``` \ No newline at end of file diff --git a/crypto/babyrsa/sol/solve.py b/crypto/babyrsa/sol/solve.py new file mode 100644 index 0000000..fa4a258 --- /dev/null +++ b/crypto/babyrsa/sol/solve.py @@ -0,0 +1,24 @@ +from Crypto.Util.number import bytes_to_long, long_to_bytes, GCD +n = 136129039934906799245883537600820362325696322589710411590186897335243686579104762065743976573291702750635108717889410733229115423708058508480236948810672363347991651420293334839308943729580241028725894733461551038888847568419901086597954491530529868885698368322552732418869647636401840431554294300907359920151 +v = 145336505618051732550698474645594181799147590575859902257619187994378559013363268655950104864569764056670639713801017855616689453650198563048002837724039970159502938997236965318015229229549970355881337278281585150146306096405515477715640494716527445680613383929607132555875322057128548552200870650696580132521 +w = 20612907913751940701434586234377070995512938354700166339929030278278014092311579879497093885185859184642701850534088712075806695131108284229597532840569152216986793187613838685954549359681582763903398538637932366747956519457275203090897430583637500207802799220582108236286968780561616121115054842835633027356029114516812021084434276111757860109305675350998033334000574804620501872783275490902197769254918685821157170228694510593644625793650591298037557438014165246072463794441871325588124086650937138575432899096783647585355882931580034904138718605575594187984051923588891709878571484103146803169259247375536859708763 +c2 = 40999996801873802742121997119730707835375659336586118409913738383370584468803123294259818676510856232905858635994052230030123318196378107746038600276544711492946157137781760844236859181993407369879174109744518026199170698597057199969985775158762539533164627602726855551347620475705167278122876349988039969692 +c3 = 3139944035622906500446122298196738357051827709726750721775633031452787107263389042610582535666920737943985088630120852223041567466431979061050093583701100302275054497243398106197255754467807652090694088800711075375412435711585892522866917133467033013010802142851893437909298773235587564677457793658655780592766302182404628855729480554049343600184461607330760299602586313139207785995916257882342396276534435829220426449740533729325143603438108068736676574488481496262952017763353301317940181413229422681271004713688641645933690837569274536827363723993636423240827193221091820001434531563287145275345624041360425385072 +e = 65537 + +def decrypt(c:int, n, prime_factors:list): # decrypt message if we know the prime factors of n + phi=1 + for p in prime_factors: + phi*=(p-1) + d=pow(e, -1, phi) + m=pow(c, d, n) + return long_to_bytes(m).decode() + + +q= GCD(n, v) +p= n//q +r= v//q +k= w//(p*q*r) + +print(decrypt(c2, v, [q,r]), end='') +print(decrypt(c3, w, [p,q,r,k])) \ No newline at end of file diff --git a/crypto/rotten-salad/challenge.yml b/crypto/rotten-salad/challenge.yml new file mode 100644 index 0000000..61900f6 --- /dev/null +++ b/crypto/rotten-salad/challenge.yml @@ -0,0 +1,26 @@ +name: "Rotten Salad" +author: "souvlakia" +category: crypto + +description: | + Caesar came up with a cipher to protect the salad recipe his cyber Chef made. Can you help Brutus decrypt it? + +value: 500 +type: dynamic +extra: + initial: 500 + minimum: 50 + decay: 25 + +flags: + - GTBQ{3t_Tu_bRut3?} + +files: + - public/salad.txt + +tags: + - crypto + - beginner + +state: visible +version: "0.1" \ No newline at end of file diff --git a/crypto/rotten-salad/public/salad.txt b/crypto/rotten-salad/public/salad.txt new file mode 100644 index 0000000..74664f1 --- /dev/null +++ b/crypto/rotten-salad/public/salad.txt @@ -0,0 +1,5 @@ +Vtx(t' ft!tw exv|%xM +e$"t|#x !x))*vx +ct'"x(t# v{xx(x +V'|(% v'$*)$#( +ZgUd0F)rg*rue*)FR2 \ No newline at end of file diff --git a/crypto/rotten-salad/setup/salad.md b/crypto/rotten-salad/setup/salad.md new file mode 100644 index 0000000..a1eaf5b --- /dev/null +++ b/crypto/rotten-salad/setup/salad.md @@ -0,0 +1 @@ +[salad](https://gchq.github.io/CyberChef/#recipe=ROT47(19)&input=Q2Flc2FyIFNhbGFkIFJlY2lwZToKUm9tYWluZSBsZXR0dWNlClBhcm1lc2FuIGNoZWVzZQpDcmlzcCBjcm91dG9ucwpHVEJRezN0X1R1X2JSdXQzP30&oenc=65001) \ No newline at end of file diff --git a/crypto/rotten-salad/sol/README.md b/crypto/rotten-salad/sol/README.md new file mode 100644 index 0000000..7057916 --- /dev/null +++ b/crypto/rotten-salad/sol/README.md @@ -0,0 +1 @@ +[salad](https://gchq.github.io/CyberChef/#recipe=ROT47_Brute_Force(100,0,true,'GTBQ%7B')&input=VnR4KHQnIGZ0IXR3IGV4dnwleE0KZSQidHwjeCAheCkpKnZ4CmN0JyJ4KHQjIHZ7eHgoeApWJ3woJSB2JyQqKSQjKApaZ1VkMEYpcmcqcnVlKilGUjI&oenc=65001) \ No newline at end of file