Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
goelp14
GitHub Repository: goelp14/easyctf-iv-problems
Path: blob/master/haystack/grader.py
650 views
1
from io import BytesIO
2
import string
3
4
alphabet = string.ascii_letters
5
haystack_size = 1000000
6
with open("20k.txt", "r") as f:
7
words = f.readlines()
8
9
10
def get_problem(random):
11
index = random.randint(0, haystack_size)
12
flag = "".join(random.choice(alphabet) for x in range(25))
13
return index, flag
14
15
16
def create_haystack(random):
17
# Create a flag
18
index, flag = get_problem(random)
19
20
# Create the haystack
21
haystack = ""
22
while len(haystack) < haystack_size:
23
haystack += random.choice(words).strip() + " "
24
25
# Put the flag in the haystack
26
haystack = haystack[:index] + "easyctf{" + flag + "}" + haystack[index + 1:]
27
return BytesIO(haystack.encode())
28
29
30
def generate(random):
31
return dict(files={
32
"haystack.txt": create_haystack(random)
33
})
34
35
36
def grade(random, key):
37
_, flag = get_problem(random)
38
if key.find(flag) >= 0:
39
return True, "Correct!"
40
return False, "Nope."
41
42