如何使用recvline读取服务器返回的指定第二行内容?
问题解决:Pwn库获取服务器多行输出中的指定行
问题场景
服务器输出内容:
Ok then! Let's go!
GORGE, FIRE, GORGE, GORGE
What do you do?
使用recvline().strip().decode()只能读取第一行,无法获取第二行GORGE, FIRE, GORGE, GORGE,现有代码逻辑错误导致需求无法实现。
现有代码问题
代码中错误使用sendlineafter("(y/n) ", "y"),但服务器输出里不存在(y/n) 这个交互提示,这会导致程序阻塞或接收错误内容,进而无法拿到目标行。
修复方案
方案1:逐行读取目标内容
直接连续调用recvline()获取多行输出,按顺序拿到目标行:
import pwn p = pwn.remote("83.136.254.233", "56584") # 读取并忽略第一行(若不需要保留) p.recvline() # 读取目标第二行 target_line = p.recvline().strip().decode() print(target_line) # 输出:GORGE, FIRE, GORGE, GORGE # 处理后续交互:根据目标行生成对应回复 things = ['GORGE', 'FIRE', 'PHREAK'] reply = ['STOP', 'DROP', 'ROLL'] commands = target_line.split(', ') response = [reply[things.index(cmd)] for cmd in commands if cmd in things] # 发送回复到服务器 p.sendline(' '.join(response)) # 接收并打印flag(根据实际场景调整) flag = p.recvline().strip().decode() print(flag)
方案2:通过特征定位目标行
若不确定输出顺序,可使用recvuntil定位目标行的特征字符,再完整读取该行:
import pwn p = pwn.remote("83.136.254.233", "56584") # 接收直到出现目标行的起始特征 p.recvuntil(b"GORGE,") # 回退缓冲区指针,确保能读取完整行 p.unrecv(b"GORGE,") target_line = p.recvline().strip().decode() print(target_line) # 后续回复逻辑同方案1 things = ['GORGE', 'FIRE', 'PHREAK'] reply = ['STOP', 'DROP', 'ROLL'] commands = target_line.split(', ') response = [reply[things.index(cmd)] for cmd in commands if cmd in things] p.sendline(' '.join(response)) flag = p.recvline().strip().decode() print(flag)
关键要点
recvline()每次仅读取一行内容,需连续调用才能获取多行输出。- 必须移除代码中不符合服务器交互流程的
sendlineafter("(y/n) ", "y")语句,避免程序阻塞。
内容的提问来源于stack exchange,提问作者sjj
相关产品推荐
相关产品推荐

