1. Required Libraries:
**import ZipFile
**import Optparse
**from Threading Import Thread
(1) ZipFile:
1.1 ZipFile. ZipFile (filename[, mode[, compression[, AllowZip64]])
There is no doubt about filename.
mode and normal file operation, ' R ' means to open an existing read-only zip file, ' W ' means to empty and open a write-only zip file, or to create a write-only zip file; ' A ' means to open a zip file and add content.
compression represents a compressed format with an optional compression format of only 2: Zip_store; Zip_deflated. Zip_store is the default, means no compression, zip_deflated means compression, if you do not know what is deflated, then it is recommended that you go to fill up the lessons.
when ALLOWZIP64 is true, 64-bit compression is supported, in general, this option is used when the compressed file is larger than 2G, which is false by default because UNIX systems do not support it.
1.2 zipfile.close ()
seriously, there's nothing to say, if anything, it's that any file you write will not actually be written to disk until it is closed.
1.3 zipfile.write (filename[, arcname[, Compress_type])
Acrname is the name of the file in the compressed file, which, by default, is the same as filename
Compress_type exists because zip files allow compressed files to have different compression types.
1.4 Zipfile.extractall ([path[, member[, password]])
path to unzip the directory, nothing to say
member list of filenames that need to be decompressed
password This is required when the zip file has a passwordOptions
For simple applications, so much is enough.
(2) Optparse:
Python has two built-in modules for handling command-line arguments:
One is getopt, "deep in Python" also mentioned in the book, can only simple processing command line parameters;
The other is Optparse, which is powerful and easy to use, and makes it easy to generate standard, UNIX/POSIX-compliant command-line instructions.
Simple process
* * First, you must import the Optionparser class to create a Optionparser object:
Python code
- From Optparse import Optionparser
- [...]
- Parser = Optionparser ()
* * Then, use add_option to define command-line arguments:
Python code
- Parser.add_option (Opt_str, ...,
- Attr=value, ...)
* * Each command-line argument consists of the parameter name string and the parameter attribute. such as- f or –file are the long and short parameter names:
Python code
- Parser.add_option ("-F", "--file", ...)
* * Finally, once you have defined all the command-line arguments, call Parse_args () to resolve the program's command line:
Python code
- (options, args) = Parser.parse_args ()
* * Note: You can also pass a command-line argument list to Parse_args (); otherwise, the default is to use Sys.argv[:1].
Two values returned by Parse_args ():
- Options, which is an object (Optpars. Values), which holds the command-line parameter value. As long as you know the command line parameter name, such as file, you can access its corresponding value: Options.file.
- Args, which is a list made up of positional arguments.
Actions
Action is one of the parameters of the Parse_args () method, which indicates how optparse is handled when resolving to a command-line argument. Actions have a fixed set of values to choose from, the default is 'store ', which means that the command line parameter values are saved in the Options object.
Example
Python code
- Parser.add_option ("-F", "--file",
- action="Store", type="string", dest="filename")
- args = ["-F", "Foo.txt"]
- (options, args) = Parser.parse_args (args)
- Print Options.filename
Finally, "Foo.txt" will be printed out.
When optparse resolves to '-f ', it will continue to parse the following ' Foo.txt ' and then save ' foo.txt ' to options.filename. When Parser.args () is called, the value of Options.filename is ' foo.txt '.
You can also specify that the type parameter in the Add_option () method is a different value, such as int or float, and so on:
Python code
- Parser.add_option ("-n", type="int", dest="num")
By default, type is ' string '. As shown above, long parameter names are also optional. In fact, the dest parameter is also optional. If the Dest parameter is not specified, the value of the options object is accessed using the command line's parameter name.
The store also has two other forms: store_true and Store_false , which are used to handle cases with no values after the command line arguments. command-line parameters such as-v,-q:
Python code
- Parser.add_option ("-V", action="Store_true", dest="verbose")
- Parser.add_option ("-Q", action="Store_false", dest="verbose")
In this case, when parsing to '-V ', the options.verbose will be given a True value, and conversely, parsing to '-Q ' will be given a value of False.
Other actions are:store_const ,append ,count ,callback .
(3) Thread: Multithreaded processing
The thread class depicts a separate running control thread activity, and we have two ways to specify this activity, either through a callable object's constructor, or by overriding the subclass run () method. There are no other methods that should be overridden in subclasses. In other words, only the __init__ () and run () methods of this class are overturned.
Once the thread object is created, the object's activity must be started by the thread's start () method. This will evoke the run () method in a separate thread control.
Once the thread's activity begins, the state of the thread is "alive". The "Alive" state is not ended until the run () method finishes. We can also run the Is_alive () method directly to determine the status of the process "alive"
One thread can invoke the join () method of another thread. This is called blocking the calling thread until its join () method is called thread termination.
The name of a thread is passed through the constructor, or it can be modified or read by the Name property.
A thread can be marked as a daemon thread (daemon thread), a thread that is flagged when it exits when the Python program exits. Its initial value is inherited when the thread is created. This flag can be set by daemon or Daemone constructor variables.
Tip: The daemon thread is immediately interrupted when the program is closed. It will not be released when it is appropriate. If you want your thread to stop as appropriate. You can make it non-daemonic or use some proper signaling mechanisms.
Methods and variables commonly used in Thread:
1.start ()
Initiates thread activity.
2.run ()
This method describes the activity of the thread, and we can override this method in the subclass.
3.join ()
The join () method is also available in the thread class of Python so that one thread can wait for another thread to execute before it runs. This method can also set a timeout parameter to avoid endless waiting. Because two threads are finished sequentially, it looks like a thread, so it is called a merge of threads.
The timeout is set by passing a parameter to the join, that is, the join is not in the blocking process over the specified time. In the actual application of the test, it is found that not all the threads end in the time-out period, but the order of the execution of the test is timed out in time_out time, for example, the time-out is set to 2s, the previous thread in the absence of the completion of the case, A subsequent thread execution join sets a 2s timeout from the end of the previous thread.
4.name ()
5.getname (0
6.setname ()
7.ident ()
8.is_alive ()
9.daemon
A Boolean value that indicates whether the thread is Damemon thread (TRUE) or not (false). The start () must be set before this is called, otherwise the run-time error occurs. Its initial value is inherited from the creation thread, and the main thread is not a daemon thread, so all threads created by default in the Daemon = False main thread.
The entire Python program exits without the surviving non-daemon thread leaving.
10.isDaemon ()
Setdaemon ()
ZIP file password cracking machine source code:
Import ZipFile
Import Optparse
From threading Import Thread
def extractfile (Zfile,password):
Try
Zfile.extractall (Pwd=password)
Print (' [+] Found password ' +password+ ' \ n ')
Except
Pass
def main ():
Parser=optparse. Optionparser ("Usage%prog" +\
"-F <zipfile>-D <dictionary>")
Parser.add_option ('-f ', dest= ' zname ', type= ' string ', \
help= ' Specify dictionary file ')
Parser.add_option ('-d ', dest= ' dname ', type= ' string ', \
help= ' Specify diction file ')
(Options,args) =parser.parse_args ()
if (Options.zname==none) | (Options.dname==none):
Print (Parser.usage)
Exit (0)
Else
Zname=options.zname
Dname=options.dname
Zfile=zipfile. ZipFile (Zname)
Passfile=open (dname)
For line in Passfile.readline ():
Password=line.strip (' \ n ')
T=thread (target=extractfile,args= (Zfile,password))
T.start ()
If __name__== ' __main__ ':
Main ()
Python and hack zip file password hack