Skip to content

Commit 43be702

Browse files
authored
Add checking for leaked flags in distributed files (#87)
1 parent 5275dcf commit 43be702

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ctfcli/utils/challenge.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import yaml
66

77
from .config import generate_session
8+
from .tools import strings
89

910

1011
class Yaml(dict):
@@ -387,4 +388,19 @@ def lint_challenge(path):
387388
if errored:
388389
exit(1)
389390

391+
# Check that files don't have a flag in them
392+
files = challenge.get("files", [])
393+
errored = False
394+
for f in files:
395+
fpath = Path(path).parent / f
396+
for s in strings(fpath):
397+
# TODO make flag format customizable
398+
if "flag" in s:
399+
print(
400+
f"Potential flag {s} found in distributed file {fpath.absolute()}"
401+
)
402+
errored = True
403+
if errored:
404+
exit(1)
405+
390406
exit(0)

ctfcli/utils/tools.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import string
2+
3+
4+
def strings(filename, min=4):
5+
"""
6+
Python implementation of strings
7+
https://stackoverflow.com/a/17197027
8+
"""
9+
with open(filename, errors="ignore") as f:
10+
result = ""
11+
for c in f.read():
12+
if c in string.printable:
13+
result += c
14+
continue
15+
if len(result) >= min:
16+
yield result
17+
result = ""
18+
if len(result) >= min: # catch result at EOF
19+
yield result

0 commit comments

Comments
 (0)