Jump to content

Python Loops and Zip files

I've been working on a capture the flag and one of the problems is called Zipfinity. 

The CTF works in a VM on ubuntu linux. Here is the info it gives you-- 

 

"Because I like really small files, I zipped the flag 10,000 times!

Unless you want to spend hours clicking stuff, you better make a program that does it for you!

Personally, I recommend python and the zipfile module.

This will likely crash your VM if you aren't careful"

 

I'm very new to programming so forgive me if I have major flaws, but here is the code I wrote. 

#!/bin/env python3 

from zipfile import ZipFile

file_name= zyp + int + .zip 
int = 9999 

while int > 0:
  with ZipFile (file_name, 'r') as zip:
    zip.printdir()
    print('extracting all the files now...')
    zip.extractall()
    print('done')
  int -= 1 

The problem is in the line that starts file_name=. 

Terminal tells me that there is invalid syntax and points to the .zip  

 

The original file name is zyp9999.zip and when extracted turns into zyp9998.zip so my idea was to create some sort of format where only the number changed. I wasn't sure what to search google for either to help me. I tried learning about 

writing over files but I didn't understand. So the file_name = zyp  + int + .zip was kinda just a shot in the dark that didn't work. 

 

I attached the zip if it might help anyone. 


 

zyp9999.zip

Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/env python3 

from zipfile import ZipFile


str(9999)

file_name= 'zyp' + str() + '.zip' 


while str() > 0:
  with ZipFile (file_name, 'r') as zip:
    zip.printdir()
    print('extracting all the files now...')
    zip.extractall()
    print('done')
  str() -= 1
  
 

I did this and got the error saying "can't assign to function call"   referring to str() -= 1

Link to comment
Share on other sites

Link to post
Share on other sites

Update again....Its not saving the num-1 

#!/bin/env python3 

from zipfile import ZipFile

num = 9999

newname = ("zyp" + str(num-1) + ".zip")

while num > 0:
  with ZipFile (newname, 'r') as zip:
    zip.printdir()
    print('extracting all the files now...')
    zip.extractall()
    print('done')
  num -= 1

 

Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/env python3 


from zipfile import ZipFile


num = 10000
while num > 0:
  num -= 1 

  newname = ("zyp" + str(num) + ".zip")
  with ZipFile (newname, 'r') as zip:
    zip.printdir()
    print('extracting all the files now...')
    zip.extractall()
    print('done')

This worked! Case closed. Thanks xD 

Link to comment
Share on other sites

Link to post
Share on other sites

why don't we use
 

range(1000, 0, -1)

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, vorticalbox said:

why don't we use
 


range(1000, 0, -1)

That would probably work. For me, it made it easier to understand using the while loop with inequalities rather than the for loop with range. The last comment I made has code that worked for me, thank you for the suggestion though! 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, WesleyC said:

That would probably work. For me, it made it easier to understand using the while loop with inequalities rather than the for loop with range. The last comment I made has code that worked for me, thank you for the suggestion though! 

I always have the rule of if I k ow how many times I need to do something then use a for loop and if I need to wait until some condition then use a while.

 

Going to have a play with recursive calls for this problem once I get to work

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/env python3 
from zipfile import ZipFile
def unzip(number):
    if number == 0:
        return
    newname = f"zyp{number}.zip"
    with ZipFile (newname, 'r') as zip:
        zip.printdir()
        print('extracting all the files now...')
        zip.extractall()
        print('done')
    return  unzip(number-1)

unzip(10000)

recursion is the new looping :P

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×