Created
May 29, 2020 21:07
-
-
Save thotypous/f8dbb9d863e4e1c74d926e7cd58a5323 to your computer and use it in GitHub Desktop.
For NIZKCTF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from nizkctf.team import Team | |
from nizkctf.acceptedsubmissions import AcceptedSubmissions | |
from nizkctf.settings import Settings | |
from nizkctf.proof import proof_open | |
import os | |
import sys | |
import requests | |
import datetime | |
import pytz | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
def get_timestamp(pr_id): | |
r = requests.get('https://api.github.com/repos/{}/pulls/{}'.format(Settings.submissions_project, pr_id)) | |
data = r.json() | |
merged_at = data['merged_at'] | |
if merged_at is None: | |
return None | |
ts = int(pytz.timezone('UTC').localize( | |
datetime.datetime.strptime(merged_at, '%Y-%m-%dT%H:%M:%SZ') | |
).timestamp()) | |
logging.info('timestamp for #%d: %d', pr_id, ts) | |
return ts | |
def estimate_timestamp(pr_id): | |
pr_down = pr_id | |
while True: | |
pr_down -= 1 | |
a = get_timestamp(pr_down) | |
if a is not None: | |
break | |
pr_up = pr_id | |
while True: | |
pr_up += 1 | |
b = get_timestamp(pr_up) | |
if b is not None: | |
break | |
return (a + b) // 2 | |
pr_id = int(sys.argv[1]) | |
timestamp = estimate_timestamp(pr_id) | |
r = requests.get('https://api.github.com/repos/{}/pulls/{}/files'.format(Settings.submissions_project, pr_id)) | |
data, = r.json() | |
patch = data['patch'] | |
contents = data['patch'].splitlines() | |
assert contents[0] == '@@ -0,0 +1 @@' | |
assert contents[1].startswith('+') | |
assert contents[2] == '\\ No newline at end of file' | |
assert len(contents) == 3 | |
proof = contents[1][1:].encode('ascii') | |
filename = data['filename'] | |
chall_id, ext = os.path.splitext(os.path.basename(filename)) | |
assert ext == '.csv' | |
team_id = os.path.dirname(filename) | |
team = Team(id=team_id) | |
claimed_chall = proof_open(team, proof) | |
assert claimed_chall.id == chall_id | |
logging.info('at %d, team %s solved %s', timestamp, team['name'], chall_id) | |
AcceptedSubmissions().add(claimed_chall, team, accepted_time=timestamp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment