Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Remove the Password from a Zip File?
Next article icon

How to Brute Force ZIP File Passwords in Python?

Last Updated : 23 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see a Python program that will crack the zip file's password using the brute force method.

The ZIP file format is a common archive and compression standard. It is used to compress files. Sometimes, compressed files are confidential and the owner doesn't want to give its access to every individual. Hence, the zip file is protected with a password. If the password is common then it's easily crack-able. Here, we'll use the brute force method to crack the zip file's password.

How to Brute Force ZIP File Passwords in Python?

A brute force method is a method where a set of predefined values are used to crack a password until successful. This is basically a "hit and try" method. This method might take a long time if the set of values are high, but its success rate is high. The more the number of values, the greater the chances of cracking passwords. Here we'll be using "rockyou" text file of size 133 MB with over 14 million sets of passwords to try. Download the text file here.

Approach:

  • First, import the zipfile module.
  • Initialize the ZipFile object which helps in extracting the contents of the zip file.
  • Count the number of words present in "rockyou.txt" file and display it on the terminal.
  • Call the "crack_password" function which returns true if a password is found else returns false. Pass the name of the text file and the ZipFile object as parameters.
  • idx variable is used to keep track of line numbers.
  • Open the text file "rockyou.txt" in "rb" mode in order to handle file contents in binary form. This is because the file contains some special symbols which can't be handled if the file is opened in "r" mode and will generate UnicodeDecodeError.
  • After opening the file, extract the line from the file and then split the word from it.
  • In the try block, extract the contents of the zip file by giving the password to pwd field of extractall method. extractall() method will extract all the contents of the zip file to the current working directory. The above program extracts a zip file named “gfg.zip” in the same directory as this Python script.
  • If the password is incorrect, an exception will be generated. In except block, continue the loop to check other words in the file.
  • If the password is found return true else at last return false and display the desired message.

Below is the full implementation:

Python3
import zipfile   def crack_password(password_list, obj):     # tracking line no. at which password is found     idx = 0      # open file in read byte mode only as "rockyou.txt"     # file contains some special characters and hence     # UnicodeDecodeError will be generated     with open(password_list, 'rb') as file:         for line in file:             for word in line.split():                 try:                     idx += 1                     obj.extractall(pwd=word)                     print("Password found at line", idx)                     print("Password is", word.decode())                     return True                 except:                     continue     return False   password_list = "rockyou.txt"  zip_file = "gfg.zip"  # ZipFile object initialised obj = zipfile.ZipFile(zip_file)  # count of number of words present in file cnt = len(list(open(password_list, "rb")))  print("There are total", cnt,       "number of passwords to test")  if crack_password(password_list, obj) == False:     print("Password not found in this file") 

Output:


Next Article
How to Remove the Password from a Zip File?
author
sarthak_eddy
Improve
Article Tags :
  • Python
  • python-utility
Practice Tags :
  • python

Similar Reads

  • How to Remove the Password from a Zip File?
    ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a single file containing one or more compressed files, offering an
    2 min read
  • How to Create a Password Protected ZIP File in Linux?
    Linux provides Zip command to work with a file like compressing the file and decompressing with a password. It's not come with built-in you need to install from an external source. The Zip command has two different utility(zip and unzip). zip is used for compressing the file and unzip is used for de
    2 min read
  • How To Hash Passwords In Python
    In this article, we are going to know how to hash passwords in python. A strong password provides safety. Plain text passwords are extremely insecure, so we need to strengthen the passwords by hashing the password. Hashing passwords is a cheap and secure method that keeps the passwords safe from mal
    4 min read
  • How to Unpack a PKL File in Python
    Unpacking a PKL file in Python is a straightforward process using the pickle module. It allows you to easily save and load complex Python objects, making it a useful tool for many applications, especially in data science and machine learning. However, always be cautious about the security implicatio
    3 min read
  • Create Password Protected Zip of a file using Python
    ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a single file containing one or more compressed files, offering an
    2 min read
  • Hashing Passwords in Python with BCrypt
    In this article, we will see how to hash passwords in Python with BCrypt. Storing passwords in plain text is a bad practice as it is vulnerable to various hacking attempts. That's why it is recommended to keep them in a hashed form.  What is hashing? It's a process of converting one string to anothe
    4 min read
  • Storing passwords with Python keyring
    In this article, we will see how to store and retrieve passwords securely using Python's keyring package. What is a keyring package? keyring package is a module specifically designed to securely store and retrieve passwords. It is like the keychain of MacOS, using this module and Python code we can
    2 min read
  • How to Read from a File in Python
    Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently. Example File: geeks.txt Hello World Hello Geeksfor
    5 min read
  • How to iterate over files in directory using Python?
    Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories. Usin
    3 min read
  • Generating Strong Password using Python
    Having a weak password is not good for a system that demands high confidentiality and security of user credentials. It turns out that people find it difficult to make up a strong password that is strong enough to prevent unauthorized users from memorizing it. This article uses a mixture of numbers,
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences