def run(url, username, password): hostname = urlparse(url).hostname # Login login(url, get_nonce(url), username, password) # Create hostname directory if not exist if not path.exists(hostname): os.mkdir(hostname) # Get Challenges for challenge in iter_challenges(url): category_path = path.join( hostname, re.sub('[^\w\-_ ]', '', challenge['category'].strip())) if not path.exists(category_path): os.mkdir(category_path) challenge_path = path.join( category_path, re.sub('[^\w\-_ ]', '', challenge['name'].strip())) if not path.exists(challenge_path): os.mkdir(challenge_path) with codecs.open(path.join(challenge_path, "ReadMe.md"), "wb", encoding='utf-8') as f: f.write(u"Name: %s\n" % challenge['name']) f.write(u"Value: %d\n" % challenge['value']) f.write(u"Description: %s\n" % challenge['description']) logger.info("Creating Challenge [%s] %s" % (challenge['category'] or "No Category", challenge['name'])) file_names = re.findall("https?://\w+(?:\.\w+)+(?:/[\w._-]+)+", challenge['description'], re.DOTALL) for file_name in file_names: fn = path.basename(file_name) download_file(file_name, path.join(challenge_path, fn)) for file_name in challenge['files']: fn = path.basename(urlparse(file_name).path) if not file_name.startswith('/files/'): file_name = "/files/%s" % file_name download_file(urljoin(url, file_name), path.join(challenge_path, fn)) # Logout logout(url)
def run(url, username, password): hostname = urlparse(url).hostname # Login login(url, get_nonce(url), username, password) # Create hostname directory if not exist if not path.exists(hostname): os.mkdir(hostname) # Get Challenges for challenge in iter_challenges(url): category_path = path.join(hostname, slugify(challenge['category'])) if not path.exists(category_path): os.mkdir(category_path) challenge_path = path.join(category_path, slugify(challenge['name'])) if not path.exists(challenge_path): os.mkdir(challenge_path) with codecs.open(path.join(challenge_path, "README.md"), "wb", encoding='utf-8') as f: logger.info("Creating Challenge [%s] %s" % (challenge['category'] or "No Category", challenge['name'])) f.write("# %s\n\n" % challenge['name']) f.write("## %s - Points: %d\n\n" % (challenge['category'], challenge['value'])) for line in challenge['description'].splitlines(): f.write("> %s\n>\n" % line) for file_name in challenge['files']: fn = path.basename(urlparse(file_name).path) # FIXME url sometimes with prefix /files/ download_file(urljoin(url, file_name), path.join(challenge_path, fn)) f.write("> [%s](%s)\n>\n" % (fn, fn)) file_names = re.findall("https?://\w+(?:\.\w+)+(?:/[\w._-]+)+", challenge['description'], re.DOTALL) for file_name in file_names: fn = path.basename(file_name) download_file(file_name, path.join(challenge_path, fn)) f.write("> [%s](%s)\n>\n" % (fn, fn)) # Logout logout(url)
def run(url, username, password): hostname = urlparse(url).hostname res = session.get(urljoin(url, "/login"), params={'next': 'challenges'}) nonce = re.search('<input type="hidden" name="nonce" value="(.*?)">', res.content) # Login session.post(urljoin(url, "/login"), params={'next': 'challenges'}, data={ 'name': username, 'password': password, 'nonce': nonce.group(1) }) # Get Challenges res = session.get(urljoin(url, "/chals")) # Create hostname directory if not exist if not path.exists(hostname): os.mkdir(hostname) res_json = res.json() for challenge in res_json['game']: category_path = path.join(hostname, challenge['category']) if not path.exists(category_path): os.mkdir(category_path) challenge_path = path.join( category_path, re.sub('[^\w\-_. ]', '_', challenge['name'].strip())) if not path.exists(challenge_path): os.mkdir(challenge_path) with codecs.open(path.join(challenge_path, "ReadMe.md"), "wb", encoding='utf-8') as f: f.write(u"Name: %s\n" % challenge['name']) f.write(u"Value: %d\n" % challenge['value']) f.write(u"Description: %s\n" % challenge['description']) logger.info("Creating Challenge [%s] %s" % (challenge['category'] or "No Category", challenge['name'])) file_names = re.findall("https?://\w+(?:\.\w+)+(?:/[\w._-]+)+", challenge['description'], re.DOTALL) for file_name in file_names: fn = path.basename(file_name) download_file(file_name, path.join(challenge_path, fn)) for file_name in challenge['files']: fn = path.basename(file_name) download_file(urljoin(url, "/files/%s" % file_name), path.join(challenge_path, fn)) session.get(urljoin(url, "/logout"))
def login(self, username, password): next_url = '/challenges' res = self.session.post(url=urljoin(self.url, "/login"), params={'next': next_url}, data={ 'name': username, 'password': password, 'nonce': self.__get_nonce() }) if res.ok and urlparse(res.url).path == next_url: return True return False
def dump(self): # Create challenge directory if not exist challenge_path = path.join( self.escape_filename(urlparse(self.url).hostname), self.escape_filename(self.category), self.escape_filename(self.name)) os.makedirs(challenge_path, exist_ok=True) with codecs.open(path.join(challenge_path, "ReadMe.md"), "wb", encoding='utf-8') as f: f.write(f"Name: {self.name}\n") f.write(f"Value: {self.value}\n") f.write(f"Description: {self.description}\n") self.logger.info( f"Creating Challenge [{self.category or 'No Category'}] {self.name}" ) for file_url in self.files: file_path = path.join( challenge_path, self.escape_filename(path.basename(urlparse(file_url).path))) self.download_file(file_url, file_path)