Python zipfile module and optparse module brute force hack zip file

Source: Internet
Author: User
Tags dname

Code
#-*-Coding:utf-8-*-# @Author: Lan126ImportOptparseImportZipFile fromThreadingImportThreaddefExtractfile (Zfile, passwd):Try: Zfile.extractall (pwd=Str. Encode (passwd))Print("[+] Found password"+passwd+"\ n")except:PassdefMain ():"""the use of Optparse    """Parser=Optparse. Optionparser (the usage%prog " + "-F <zipfile>-D <dictionary>") Parser.add_option ('-F ', dest=' Zname ',type=' String ', Help=' Specify zip file ') Parser.add_option ('-d ', dest=' Dname ',type=' String ', Help=' Specify dictionary file ') options, args=Parser.parse_args ()if(Options.zname is None)|(Options.dname is None):"""the difference between IS and = =        """        Print(Parser.usage) Exit (0)Else: Zname=Options.zname dname=Options.dname Zfile=ZipFile. ZipFile (Zname) passfile= Open(dname) forLineinchPassfile.readlines (): passwd=Line.strip ('\ n') T=Thread (target=Extractfile, args=(Zfile, passwd)) T.start ()if __name__ == "__main__": Main ()
Optparse Module

Optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module.

As the official document says, the first step in using Optparse is to create an Optionparser instance, We first created a Optionparser instance with a usage string
The string is used as a summary and will be printed when it is not running correctly or using the Help option

Next is the Add_option function, which is the function used to define optional arguments

Each option instance represents a set of synonymous command-line Option strings, e.g. F and--file. You can specify any number of short or long option strings, and you must specify at least one overall option string.

We only know that when parsing a command-line argument, it uses the type to cast the argument that follows the optional parameter to type and to dest, which can be used to construct the complete help message

-F foo.txt-p 1-3.5 4-fbar.txt

Get

OPTIONS.F = "Foo.txt"
Options.point = (1.0,-3.5, 4.0)
OPTIONS.F = "Bar.txt"

Use the Parser.parse_args () function to get two values, one is an object containing values for all of the your options, and one is the list of positional arguments L Eftover after parsing options

The difference between the = = Symbol and is

The simple difference here is that the is comparison ID is equivalent to comparing the address of two variables, and = = compares the value

ZipFile module

First create a ZipFile object with the zip file you want to crack
Then open the dictionary file
Create a thread for each password in the dictionary extractall operation

Extract all members from the archive to the current working directory. path specifies a different directory to extract. Members are optional and must are a subset of the list returned by NameList (). PWD is the password used for encrypted files.

The try expect structure here guarantees that the wrong password is ignored

Threading Module

Target is the callable object to being invoked by the run () method. Defaults to None, meaning nothing is called.
Args is the argument tuple for the target invocation. Defaults to ()

The script for a brute-force zip file is complete.

Python zipfile module and optparse module brute force hack zip file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.