defis_valid(board, row, col, num): # 检查行是否合法 for i inrange(9): if board[row][i] == num: returnFalse
# 检查列是否合法 for i inrange(9): if board[i][col] == num: returnFalse
# 检查小九宫格是否合法 start_row = (row // 3) * 3 start_col = (col // 3) * 3 for i inrange(3): for j inrange(3): if board[start_row + i][start_col + j] == num: returnFalse
returnTrue
defsolve_sudoku(board): for row inrange(9): for col inrange(9): if board[row][col] == 0: for num inrange(1, 10): if is_valid(board, row, col, num): board[row][col] = num if solve_sudoku(board): returnTrue board[row][col] = 0# 回溯 returnFalse# 所有数字都尝试过,没有找到合适的数字 returnTrue
defprint_sudoku(board): a = '' for row inrange(9): for col inrange(9): a += str(board[row][col]) a+='\n' return a.strip()
context.log_level = 'debug'
p = remote('47.108.165.60',23479)
p.recv()
for i inrange(7): p.sendline('1')
p.recvuntil('Please select the level:')
p.sendline('5')
a = '---------------------\nnow give me you solve:' content = p.recvuntil(a).decode().split(a)[0][-130:]
sudoku = sudoku.strip() sudoku = sudoku.split('\n') tmp = [] for sudo in sudoku: a = [int(s) for s in sudo] tmp.append(a)
if solve_sudoku(tmp): result = print_sudoku(tmp) log.info(result) for line in result.split('\n'): p.send(line) #content = p.recv().decode() p.interactive()
defpayload(r1,r2): # r1 wrong r2 right payloads = '' for a inrange(9): lines = '' logs = 0 alls = 0 for b inrange(9): if r1[a][b] != r2[a][b]: if b == 0: lines += str(r2[a][b]) else: log_number = (b-logs)*"d" lines += log_number+str(r2[a][b]) alls += (b-logs) logs = b if a != 8: lines += (alls)*"a" + 's' payloads += lines return payloads
由于输入的可以一行完成,那就直接把所有步骤加在一起最后传输过去即可。
脚本
from pwn import *
defis_valid(board, row, col, num): # 检查行是否合法 for i inrange(9): if board[row][i] == num: returnFalse
# 检查列是否合法 for i inrange(9): if board[i][col] == num: returnFalse
# 检查小九宫格是否合法 start_row = (row // 3) * 3 start_col = (col // 3) * 3 for i inrange(3): for j inrange(3): if board[start_row + i][start_col + j] == num: returnFalse
returnTrue
defsolve_sudoku(board): for row inrange(9): for col inrange(9): if board[row][col] == 0: for num inrange(1, 10): if is_valid(board, row, col, num): board[row][col] = num if solve_sudoku(board): returnTrue board[row][col] = 0# 回溯 returnFalse# 所有数字都尝试过,没有找到合适的数字 returnTrue
defprint_sudoku(board): a = [] for row inrange(9): t = [] for col inrange(9): t.append(int(board[row][col])) a.append(t) return a
defpayload(r1,r2): # r1 wrong r2 right payloads = '' for a inrange(9): lines = '' logs = 0 alls = 0 for b inrange(9): if r1[a][b] != r2[a][b]: if b == 0: lines += str(r2[a][b]) else: log_number = (b-logs)*"d" lines += log_number+str(r2[a][b]) alls += (b-logs) logs = b if a != 8: lines += (alls)*"a" + 's' payloads += lines return payloads
defdecode(tmp): if solve_sudoku(tmp): result = print_sudoku(tmp) return result
import re
#context.log_level = 'debug'
p = remote('47.108.179.76',32892)
p.recvuntil('Please input')
p.sendline('1')
p.recvuntil('>')
p.sendline('7')
p.recvuntil(b'-------------------------')
content = p.recv()
log.info(content.decode())
data = content.replace(b'\x1b[1;32m',b'').replace(b'\r\n',b'').replace(b'\x1b[0m',b'').replace(b'\x1b[7;32m',b'').replace(b'-------------------------',b'') sudoku = []
for line in data.split(b'| |'): t = [int(s) for s in line.replace(b'|',b'').replace(b' ',b'').decode()] sudoku.append(t)
r1 = str(sudoku)
result = decode(sudoku)
r1 = eval(r1)
show = '' for a in result: for b in a: show+=str(b) show+='\n' log.info(show)