Skip to content

Instantly share code, notes, and snippets.

@tkisason
Created September 29, 2022 22:48
Show Gist options
  • Save tkisason/5a9cf9bedab1679eb250918b68b5e6c3 to your computer and use it in GitHub Desktop.
Save tkisason/5a9cf9bedab1679eb250918b68b5e6c3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import string
import random
import os
import shutil
import glob
challengeText = """Here is a carefully prepared file. You must find the flag within!"""
# you can add more fake flags here, or taunts, your call! :)
fakeFlags = [
"flag{yeah not the flag you are looking for}",
"flag{Nope, not the flag}",
"flag{Totally not the flag}",
"flag{Never gonna give you the flag}",
"flag{These aren't the flags you are looking for}",
"flag{Fake flag! How obnoxious!}",
"flag{This is bait!}",
]
# the real flag.
flag = "flag{h0pe_you_aut0mated_this!}"
# How many fake files we want per directory
maxFakeFiles = 5
# How many rounds of nesting do we want? Set this to a reasonable level, this is the example level that can be manually verified.
maxNestedArchives = 2
def randomString(length):
return ''.join(random.choice(string.ascii_letters) for i in range(length))
# Used to select a random archive type
def randomArchiveFormatName():
return random.choice([i[0] for i in shutil.get_archive_formats()])
def dumpToFile(data,path='./'):
name = randomString(20)
fn = open(path + name,'w')
fn.write(data)
fn.close()
return name
def makeChallenge():
# all dirnames and filenames are random.
# create a directory
dirName = randomString(20)
os.mkdir(dirName)
# stash the flag inside
dumpToFile(flag,f'./{dirName}/')
fileName = randomString(20)
# ok, now we archive it with a random format.
shutil.make_archive(fileName, randomArchiveFormatName(), dirName)
# remove the original dir, so we only have the archive.
shutil.rmtree(dirName)
# We got the flag tucked safely away, now let's rock and roll and wrap this in multiple layers
for layer in range(maxNestedArchives):
# create a random dir and move the compressed archive inside.
dirName = randomString(20)
os.mkdir(dirName)
# we want purty fileNames
shutil.move(glob.glob(fileName+'*')[0], fileName)
shutil.move(fileName, dirName)
# add up to maxFakeFiles random flagfiles
for i in range(random.randint(1,maxFakeFiles)):
dumpToFile(random.choice(fakeFlags),f'./{dirName}/')
# archive all that into a new archive file
fileName = randomString(20)
shutil.make_archive(fileName, randomArchiveFormatName(), dirName)
# remove the old tree
shutil.rmtree(dirName)
if __name__ == '__main__':
print(challengeText)
makeChallenge()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment