nssctf round 17 crypto writeup

nssctf round 17 crypto writeup

cry1

task

#真签到题
from Crypto.Util.number import bytes_to_long, getPrime
from secret import getflag

e1 = getPrime(1024)
e2 = getPrime(1024)
n = e1 * e2
m = bytes_to_long(getflag().encode())
c1 = pow(m, e1, n)
c2 = pow(m, e2, n)
print(n)
print(c1)
print(c2)
print(e1)
print(e2)

# n = 22517647586235353449147432825948355885962082318127038138351524894369583539246623545565501496312996556897362735789505076324197072008392656511657262430676945685471397862981216472634785622155317188784494912316440866051402627470561626691472280850273482836308002341429493460677206562201947000047718275995355772707947408688836667011206588727438261189233517003341094758634490421007907582147392858070623641389171229435187248184443645883661560636995548332475573072064240073037558031928639832259001407585962782698021735648128101459118863015844905452823095147248865104102562991382119836061161756978764495337874807458182581421229
# c1 = 1432393096266401187029059077791766305797845826173887492889260179348416733820890797101745501984437201566364579129066414005659742104885321270122634155922766503333859812540068278962999824043206496595825886026095484801291802992082454776271149083516187121160475839108002133113254134626407840182541809478892306748590016896975053434021666376203540725254480252049443975835307793528287818262102688334515632062552114342619781840154202525919769192765621085008206581226486157149883898548933475155236509073675387541466324512294079413938239828341890576923100769181401944289365386552139418728492565319685207500539721582552448971814
# c2 = 13299679392897297864252207869444022461237574801991239380909482153705185317634241850084078027230394830079554676426505967970943836811048777462696506309466535820372917756458083553031417406403895116557560548183674144457502601887632495739472178857537011190162283185735114683172731936834993707871636782206418680404006299140864001776588991141011500807549645227520128216130966268810165946959810884593793452437010902774726405217517557763322690215690606067996057037379898630878638483268362526985225092000670251641184960698506349245915816808028210142606700394584541282682338561482561343076218115042099753144875658666459825545602
# e1 = 155861690390761931560700906834977917646203451142415617638229284868013723431003139974975998354830978765979365632120896717380895021936387027045347260400512396388028781862427862974453223157509702913026222541667006325100878113871620322023188372501930117363623076837619478555007555970810681502521309925774889678793
# e2 = 144471983652821947847253052623701746810204736865723159569786739658583884214397562204788127484897909964898113250509653721265240138487697822089282456150238116811225975640330930854549232972314642221382625614304415750165289831040623741828600283778523993251940904896081111235859249916040849697146542311990869696453

真签到题。。。

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 28 14:29:00 2024

@author: lenovo
"""
from Crypto.Util.number import *
from gmpy2 import *
n = 22517647586235353449147432825948355885962082318127038138351524894369583539246623545565501496312996556897362735789505076324197072008392656511657262430676945685471397862981216472634785622155317188784494912316440866051402627470561626691472280850273482836308002341429493460677206562201947000047718275995355772707947408688836667011206588727438261189233517003341094758634490421007907582147392858070623641389171229435187248184443645883661560636995548332475573072064240073037558031928639832259001407585962782698021735648128101459118863015844905452823095147248865104102562991382119836061161756978764495337874807458182581421229
c1 = 1432393096266401187029059077791766305797845826173887492889260179348416733820890797101745501984437201566364579129066414005659742104885321270122634155922766503333859812540068278962999824043206496595825886026095484801291802992082454776271149083516187121160475839108002133113254134626407840182541809478892306748590016896975053434021666376203540725254480252049443975835307793528287818262102688334515632062552114342619781840154202525919769192765621085008206581226486157149883898548933475155236509073675387541466324512294079413938239828341890576923100769181401944289365386552139418728492565319685207500539721582552448971814
c2 = 13299679392897297864252207869444022461237574801991239380909482153705185317634241850084078027230394830079554676426505967970943836811048777462696506309466535820372917756458083553031417406403895116557560548183674144457502601887632495739472178857537011190162283185735114683172731936834993707871636782206418680404006299140864001776588991141011500807549645227520128216130966268810165946959810884593793452437010902774726405217517557763322690215690606067996057037379898630878638483268362526985225092000670251641184960698506349245915816808028210142606700394584541282682338561482561343076218115042099753144875658666459825545602
e1 = 155861690390761931560700906834977917646203451142415617638229284868013723431003139974975998354830978765979365632120896717380895021936387027045347260400512396388028781862427862974453223157509702913026222541667006325100878113871620322023188372501930117363623076837619478555007555970810681502521309925774889678793
e2 = 144471983652821947847253052623701746810204736865723159569786739658583884214397562204788127484897909964898113250509653721265240138487697822089282456150238116811225975640330930854549232972314642221382625614304415750165289831040623741828600283778523993251940904896081111235859249916040849697146542311990869696453
phi=(e1-1)*(e2-1)
d1=invert(e1,phi)
d2=invert(e2,phi)
m1=pow(c1,d1,n)
m2=pow(c2,d2,n)
print(long_to_bytes(m1)+long_to_bytes(m2))
#b'NSSCTF{Y0u_Hav3_S01v3d_Crypt0_Leve1_i}NSSCTF{Y0u_Hav3_S01v3d_Crypt0_Leve1_i}'
cry2

task

# 猜猜我是谁 猜对了直接秒出flag喔
from Crypto.Util.number import bytes_to_long, getPrime
from secret import getflag

p = ***
q = getPrime(1024)
e = you guess!
n = p * q
m = bytes_to_long(getflag().encode())
c=pow(m, e, n)
print(q)
print(c)
#p=one of ps
#q=145721736470529261146573065574028992352505611489859183763269215489708531333597694809923949026781460438320576519639268582565188719134157402292313959218961804213310847081787824780075530751842057663327444602428455144829447776271394663729996984613471623158126083062443634493708467568220146024273763894704649472957
#c=17441814714407189483380175736850663249578989775568187792928771544069162420510939242665830363276698262009780462912108642025299275146709817979705069095332726251759039923303627023610865046363171692163473939115438686877494878334016463787558794121885354719336139401336137097548305393030069499625065664884238710759260231321106291200849044147840392021931720902340003746946851806025722944795391356835342258387797980787437188976704677008092850181043891802072500430200735973581081228711070923822341261809453662427341958883142789220800541626034573952425948295446202775198692920613709157662831071515700549093766182579873408465779
#flag=NSSCTF{*}

在这里插入图片描述

根据题目意思

我们只需要从密码本中找到一个p,计算rsa即可,解题脚本如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 28 14:33:05 2024

@author: lenovo
"""

from tqdm import tqdm
from Crypto.Util.number import *
import gmpy2
# 打开文件 ps.txt
with open('ps.txt', 'r') as file:
    # 逐行读取文件内容
    lines = file.readlines()
e=65537
q=145721736470529261146573065574028992352505611489859183763269215489708531333597694809923949026781460438320576519639268582565188719134157402292313959218961804213310847081787824780075530751842057663327444602428455144829447776271394663729996984613471623158126083062443634493708467568220146024273763894704649472957
c=17441814714407189483380175736850663249578989775568187792928771544069162420510939242665830363276698262009780462912108642025299275146709817979705069095332726251759039923303627023610865046363171692163473939115438686877494878334016463787558794121885354719336139401336137097548305393030069499625065664884238710759260231321106291200849044147840392021931720902340003746946851806025722944795391356835342258387797980787437188976704677008092850181043891802072500430200735973581081228711070923822341261809453662427341958883142789220800541626034573952425948295446202775198692920613709157662831071515700549093766182579873408465779

# 循环处理每一行的 p 值
for line in tqdm(lines, desc="Processing lines", unit="line"):
    p = int(line.strip())  # 将字符串转换为整数
  
    n = p * q
    phi = (p - 1) * (q - 1)
    d = gmpy2.invert(e, phi)  # 替换为你的获取 flag 的方式
    m = pow(c, d, n)

    # 检查明文的前缀是否以 "NSSCTF" 开头
    flag = long_to_bytes(m)
    if b'NSSCTF' in flag:
        # 输出结果
        print(flag)

在这里插入图片描述

cry3

连接靶机效果如下:

在这里插入图片描述

查看源代码

在这里插入图片描述

利用pwntools

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 28 16:21:29 2024

@author: lenovo
"""

from pwn import *
import gmpy2

io = remote('node2.anna.nssctf.cn', 28912)
count=0
while 1:
    while 1:
        test=io.recvline(keepends = True)
        if b'py?' in test:
            break
    n=int(io.recvline(keepends = True)[2:].strip())
    e1=int(io.recvline(keepends = True)[3:].strip())
    e2=int(io.recvline(keepends = True)[3:].strip())
    c1=int(io.recvline(keepends = True)[3:].strip())
    c2=int(io.recvline(keepends = True)[3:].strip())
    s,s1,s2 = gmpy2.gcdext(e1, e2)
    m1 = (pow(c1,s1,n) * pow(c2 ,s2 ,n)) % n
    x = gmpy2.gcd(e1, e2)
    k = 0
    while 1:
        m, s = gmpy2.iroot(m1 + k * n, x)
        if s:
            flag=bytes.fromhex(hex(m)[2:])
            break
        k += 1
    io.sendline(flag)
    count+=1
    print(count)
    if count==666:
        print(io.recvline())
        print(io.recv())

在这里插入图片描述

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值