Mine the Gap [Google Ctf 2023]

writeup by: zenbassi

Challenge Description

Take a break from the other challenges and play a relxing game of Minesweeper I have even solved most of the board for you and marked many of the mines. I am completely sure they are correct so you just need to find the remaining ones.


Looking at the minesweeper map, it’s huge, but I noticed that the patterns look like wires and seem to lead to some structure which intuitively resemble logic gates. Even more, there are multiple mentions in the code of circuits. So I searched on the internet for something related to minesweeper and circuits and found a paper from 2015 1 which proves that Minesweeper is np-complete using circuits which look exactly like the ones in the challenge.

An initial idea that results is that we could convert the map to a logic circuit and input it into a sat-solver like z3. Now… that’s kinda complicated.

The map being partially solved, consistent and built like a circuit means that giving in input to each end of the wire (aka putting a bomb or not there), should result in a unique arrangement for the entire map. Intuitively, this means that the whole map can be converted into a logic formula and fed to as sat solver.


After some more research, I found a github repo with a minesweeper sat solver 2 which structured the map in a very similar way with the one in the given challenge. I modified the code such that it fit the problem:

from pysat.solvers import Solver #for SAT solving
from itertools import combinations #for combinations in a CNF

import itertools
import hashlib

# DIMACS codification
def M(i,j,board_w): return  board_w**1 * i + board_w**0 * j + 1

# find unknown cells next to a cell at i,j
def unknown_neighbours(board,i,j):
  for h in range(i-1,i+2):
    for w in range(j-1,j+2):
      if h<len(board) and w<len(board[0]) and h>=0 and w>=0 and (board[h][w]>8): neighbours.append((h,w))
  return neighbours

# clauses per cell
def cell_clauses(board,i,j):

  #at least n mines
  for combination in combinations([M(elem[0],elem[1],len(board[0])) for elem in neighbours],len(neighbours)-board[i][j]+1):

  #at most n mines
  for combination in combinations([-M(elem[0],elem[1],len(board[0])) for elem in neighbours], board[i][j]+1):

  return cell_clauses

# clauses per board
def board_clauses(board,board_h,board_w):
  for i in range(board_h):
    for j in range(board_w):
      if board[i][j]<9:
        #add self as known safe cell
        #add surrounding constraints
      elif board[i][j]==10:
        #add self as mine cell
  return clauses

#print solution
def show_solution(board,board_h,board_w,model):
  bits = []
  for i in range(board_h):
    for j in range(board_w):
      if board[i][j]==9:
          board[i][j]='m' if M(i, j,board_w) in model else 's'
      if i == 23:
          bit = 1 if board[i][j] == 'm' else 0
      flag = hashlib.sha256(bytes(bits)).hexdigest()
      print(f'Flag: CTF{{{flag}}}')

def solve_board(board,printing=True):
  if printing:
    print("Legend:\n0-8: mines nearby\n9  : unknown\n10 : known mine")


  s = Solver(name='cd')

  if s.solve():
    if printing:
      print("\nSAT - consistent")
      print("Legend:\n0-8: mines nearby\n9  : unknown\n10 : known mine\nm  : found mine\ns  : found safe")

    return True
    print("\nUNSAT - inconsistent")
    return False

#board encoding:
#0-8: mines nearby
#9  : unknown space
#10 : known mine

with open('gameboard.txt', 'r') as fin:
    circuit = fin.read()
    circuit = circuit.replace(' ', '0')
    circuit = [list(line) for line in circuit.split('\n') if len(line) > 0]

board_width = len(circuit[0])
board_height = len(circuit)
mine_count=0 #smaller than width*height
unknown_cells=0 #smaller than width*height

board = [[None for x in range(board_width)] for y in range(board_height)]

for i, (x, y) in enumerate(itertools.product(range(board_width), range(board_height))):
    val = int(circuit[y][x], 16)
    if val == 11:
        val = 10
    if val == 10:
        mine_count += 1
    elif val == 9:
        unknown_cells += 1

    board[y][x] = val


Running this for 1 minute gets the flag.