量子安全quantumctfGlobalHyperlinkZoneHackthebox

admin 2026-01-01 05:06:42 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文深入分析了某量子计算CTF题目的server.py源码,阐述了利用H门和CX门构造量子纠缠态以满足特定量子态验证条件的解题思路。文章详细讲解了代码逻辑与量子门原理,并提供了使用pwntools发送特定指令序列以获取Flag的实战脚本。 综合评分: 93 文章分类: CTF,代码审计,实战经验


cover_image

量子安全 quantum ctf Global Hyperlink Zone Hack the box

枫林路大砍刀

看雪学苑

2025年12月31日 17:59 上海

代码分析server.py

首先,拿到了一个server.py文件,我们先来分析代码:

一上来是定义了一个类,然后引用了包和设置aer

from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer

class Hyperlink:
    def __init__(self):
        self.backend = Aer.get_backend("qasm_simulator")#设置AER

generate_circuit

接下来是第一个方法,生成量子线路,也就是mai函数中调用到的:

参数是instructions,对应不同的量子操作例如他代码中的样例:H:0;CX:0,1

这段主要是进行了格式检查和量子线路的数量检查

    def generate_circuit(self, instructions: str):
        circuit = QuantumCircuit(5)

        instructions = instructions.split(";")
        for instr in instructions:
            parts = instr.split(":")

            if len(parts) != 2:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Invalid instruction:&nbsp;{instr}. Expected format: <gate>:<params>")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("Examples: H:<target> | CX:<control>,<target>")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gate, params = parts

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params = [&nbsp;int(p)&nbsp;for&nbsp;p&nbsp;in&nbsp;params.split(",") ]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;except:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("Quantum gate input parameters must be integers.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("Examples: H:0 | CX:0,1")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;any(n >= circuit.num_qubits&nbsp;for&nbsp;n&nbsp;in&nbsp;params):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Qubit indexes must be less than&nbsp;{circuit.num_qubits}")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

接下来是判断输入的算符,每道题能用到的算符是不一样的,这道题能用的是H,S,T,X这四个单量子比特算符和CX,CY,CZ这三个受控双量子比特,受控门的参数设置一般为(控制位,目标位),比如cx(0,1)的意思就是根据0号电路来对于1号电路进行受控x

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;len(params) ==&nbsp;1:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp; &nbsp;gate ==&nbsp;"H": circuit.h(params[0])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;gate ==&nbsp;"S": circuit.s(params[0])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;gate ==&nbsp;"T": circuit.t(params[0])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;gate ==&nbsp;"X": circuit.x(params[0])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Quantum gate '{gate}' is invalid or unexpected with 1 parameter.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;len(params) ==&nbsp;2:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;params[0] == params[1]:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("Control and target qubits must be different.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp; &nbsp;gate ==&nbsp;"CX": circuit.cx(params[0], params[1])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;gate ==&nbsp;"CY": circuit.cy(params[0], params[1])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;elif&nbsp;gate ==&nbsp;"CZ": circuit.cz(params[0], params[1])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Quantum gate '{gate}' is invalid or unexpected with 2 parameters.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Unsupported number of parameters ({len(params)}) for quantum gate '{gate}'.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;None

&nbsp; &nbsp; &nbsp; &nbsp; circuit.measure_all()
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;circuit

这个是一个量子状态检查的代码,在main函数被调用用来检查量子是否处于题目要求的状态,如果返回值为true那么我们就可以获得flag了。

首先他会创建一个电路,然后使用刚才的生成电路的函数来制造电路,也就是会加入我们输入的操作。

之后获取每一条量子线路的测量结果,要求满足

q0=q1=q3,

q2=q4,

q4!=q2

(qn表示第n条线路)

满足这个条件即可为true

&nbsp; &nbsp;&nbsp;def&nbsp;initialize_hyperlink(self, instructions, shots =&nbsp;256):
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;len(instructions) ==&nbsp;0:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("len false")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;False

&nbsp; &nbsp; &nbsp; &nbsp; circuit = self.generate_circuit(instructions)

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;not&nbsp;circuit:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("circuit false")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;False

&nbsp; &nbsp; &nbsp; &nbsp; compiled = transpile(circuit, self.backend)
&nbsp; &nbsp; &nbsp; &nbsp; results = self.backend.run(compiled, shots = shots, memory =&nbsp;True).result()

&nbsp; &nbsp; &nbsp; &nbsp; shares = [""] *&nbsp;5

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;bits&nbsp;in&nbsp;results.get_memory():
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;i, bit&nbsp;in&nbsp;enumerate(bits[::-1]):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shares[i] += bit

&nbsp; &nbsp; &nbsp; &nbsp; shares = [&nbsp;int(share,&nbsp;2).to_bytes(32, byteorder =&nbsp;"big")&nbsp;for&nbsp;share&nbsp;in&nbsp;shares ]

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;any(set(share)&nbsp;in&nbsp;({0}, {255})&nbsp;for&nbsp;share&nbsp;in&nbsp;shares):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("shareswrong",shares)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;False
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(shares)
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shares[0] == shares[1]&nbsp;and
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shares[1] == shares[3]&nbsp;and
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shares[2] == shares[4]&nbsp;and
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shares[4] != shares[0]
&nbsp; &nbsp; &nbsp; &nbsp; ):
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;True

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;False

main

main函数主要是先读取我们的操作,然后使用初始化判断来判断是否满足条件,满足即可获得flag。

&nbsp; &nbsp; hyperlink = Hyperlink()

&nbsp; &nbsp;&nbsp;while&nbsp;True:
&nbsp; &nbsp; &nbsp; &nbsp; instructions =&nbsp;input('Specify the instructions : ')

&nbsp; &nbsp; &nbsp; &nbsp; init = hyperlink.initialize_hyperlink(instructions)

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;not&nbsp;init:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print("Incorrect Hyperlink connection pattern. Try again.")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;continue

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print(f"Hyperlink initialized successfully! Connection ID:&nbsp;{open('flag.txt').read()}")

#

总结量子计算相关知识

量子电路

类似经典电路每条线为0或者1,量子电路用态来表示,每条电路有基态|0>,激发态|1>,也会有叠加态。

一条电路上的态可以用这个式子来表示,即为量子态|k>=a|0>+b|1>,

其中a和b为振幅,振幅的平方表示观测时得到该态的概率,当然如果不观测就不知道具体是0还是1,处于叠加态。上式a,b还满足:

即为概率和为1。

如下图,有五条量子线路,每个初始态都默认为0态

量子算符,量子门

这里先只介绍这道题要用到电路门,其他的之后遇到再补充。

H

图示为他以线性代数的表达方式,所有量子门运算都可以用线性代数进行推导。即为矩阵相乘

H门的作用普通的如下

也就是让0和1态变成叠加态,同时也可以发现作用两次H相当于没作用,还会复原成原貌。

X

X门也是相位翻转门,即它可以把0变成1,1变成0

对于叠加态也是如此

CX(CNOT)

控制比特通常用点(●)表示,目标比特用⊕表示,两者用线连接

| | | | | | — | — | — | — | | 控制位 | 目标位 | 输出控制位 | 输出目标位 | | 0 | 0 | 0 | 0 | | 0 | 1 | 0 | 1 | | 1 | 0 | 1 | 1 | | 1 | 1 | 1 | 0 |

当控制比特处于叠加态时,CX门可以使得两个量子比特的状态相互关联,从而制备出纠缠态,这是经典计算无法实现的现象

比如初始q0为1/根2(|0>+|1>),此时|q0q1>为1/根2(|0>+|1>)直积|0>也就是1/根2(|00>+|10>),此时以q0作为控制比特,q1作为目标比特使用cx(0,1),那么结果会变成|q0q1>为1/根2(|00>+|11>)如果观测到一个为0,那么会坍缩到|00>,反之坍缩到|11>

解题

根据上面的三个门,为了满足目标条件:

q0=q1=q3,

q2=q4,

q4!=q2

我们可以想到,让0,1,3使用cx相互纠缠,2,4使用cx相互纠缠。

那么即可让0,1,3相等,2,4相等。

推导如下:

最后因为我原先是打pwn的最近在搞量子计算,所以我选择使用pwntools进行交互。

from pwn import *
context(log_level="debug",os="linux")

io&nbsp;= process(['python',&nbsp;'server.py'])

io.recvuntil(b"instructions : ")
io.sendline(b"H:0;CX:0,1;CX:0,3;H:2;CX:2,4")

io.interactive()

#

看雪ID:枫林路大砍刀

https://bbs.kanxue.com/user-home-1008959.htm

*本文为看雪论坛优秀文章,由 枫林路大砍刀 原创,转载请注明来自看雪社区

往期推荐

从ANGR-CTF项目入手ANGR和符号执行技术

AI时代-逆向工作者该如何用好这一利器

EXIF解析缓冲区溢出漏洞分析与利用

从C到Pwn:栈溢出漏洞利用实战入门

Android-ARM64的VMP分析和还原

球分享

球点赞

球在看

点击阅读原文查看更多


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:看雪学苑 枫林路大砍刀《量子安全 quantum ctf Global Hyperlink Zone Hack the box》

评论:0   参与:  0