Source code for the book "Violent Python" by TJ O'Connor. The code has been fully converted to Python 3, reformatted to comply with PEP8 standards and refactored to eliminate dependency issues involving the implementation of deprecated libraries.
Python 3 "Violent Python" Source Code
Source code for the book "Violent Python" by TJ O'Connor. The code has been fully converted to Python 3, reformatted to comply with PEP8 standards and refactored to eliminate dependency issues involving the implementation of deprecated libraries.
*A conversion similar to this one has been made available by myself on the source code of the book "Black Hat Python", by Justin Seitz. Check it out here if you haven't done it yet.*
Usage
Simply make a new directory (DIR) for the project, create a new virtual environment orvenv for it (recommended), clone this repository
using git clone and install the requirements using pip install.
user@host:~/DIR$ git clone https://github.com/EONRaider/violent-python3
user@host:~/DIR$ python3 -m venv venv
user@host:~/DIR$ source venv/bin/activate
(venv) user@host:~/DIR$ pip install -r requirements.txt
Notes
- The directories and files have been named in a way that they can be easily
- The frequent use of string concatenation by the author has been replaced by
- Names of files, variables, functions, classes and methods now conform to
- The now deprecated
optparselibrary has been replaced forargparse
main execution scope for each file. All CLI arguments that were
mandatory for the execution of scripts but were treated as optional in the
original code are now implemented as positional. A usage prompt is now
available for all scripts that use argparse by supplying a -h argument to
the CLI. Leaving the responsibilities of both boundary and controller
object to the CLI parser is definitely not the best choice in terms
of software architecture, but was left as-is due to the
necessity of conformity to the original coder's intent.
- All occurrences of
PEP 8: E722 do not use bare exceptviolations have
- The author has a habit of opening files/databases and leaving them in this
close() method on the open file/database objects. For
this
reason all instances of file and database manipulation have been refactored by
using context managers.
- The use of comments making reference to the encoding to be used by the
# -- coding: utf-8 --) was eliminated due to the
standardization of UTF-8 as the default encoding for Python 3 (replacing
ASCII from Python 2).
- Though completely inadequate from the
- The code included in Chapter 5 of the book has been refactored even though in
- The code on Chapter 6 that makes reference to Google and Twitter proved
Refactoring
Files not listed below can be assumed to have been refactored in one way or another as established in the "Notes" section.chapter01/vuln_scanner.pywas structured in such a way that a non-existent file would lead to aOSErrorexception at runtime. For that
check_vulns() was moved into the
conditional statement defined in the main function.
chapter02/nmap_scan.pyimplemented a main function solely for the purpose
optparse library, which has been replaced by
argparse. Because of that the main function was removed. An iteration
control structure that was part of the optparse call in the original
code was implemented in such a way that a new call to nmap was executed
for each port scanned. The iteration was moved into the nmap_scan
function to prevent wasting cycles.
chapter02/ssh_command.pyhad its initialization code moved into the
main execution scope. The names of variables used in the outer scope
that used to conflict with the names of parameters of functions were
changed. The returning prompt information was originally encoded and now
has been decoded in order to afford better readability.
chapter02/ssh_brute.pyimportedpxsshas a standalone library, but in
pexpect library. The bug led to a
ModuleNotFoundError and has been corrected. The code itself as presented in the
book was littered with indentation errors that made it unusable and has
been brought to a functioning state.
chapter02/ssh_brutekey.pyrequired a number of pre-generated keys to work;
chapter02 subdirectory.
chapter02/ssh_botnet.pyhad an unused import statement tooptparsethat
main execution
scope for the sake of standardization. The two commands issued to the bots
have been unified to avoid an unnecessary number of return statements.
chapter02/conficker.pyremoved unused call tosyslibrary.chapter03/discover_networks.pyhad to be reimplemented instead of just
mechanize library but
also interacted with the WiGLE service in a way that is no longer necessary,
once WiGLE now provides an API. For that reason the code has been standardized
and a new wigle_print function was implemented by using the requests
library to send an authenticated HTTP GET request to WiGLE. The
response returns a JSON object that can be directly accessed, making
the use of the re library also unnecessary. Exception handling was
added to make the script capable of dealing with error response
messages sent by the API. Notice that
this script depends on winreg, which only runs on Python installations
under the Microsoft Windows OS, and requires Administrator privileges
during execution for access to the Registry keys. An account has to be
registered on https://wigle.net/account for access to the API.
chapter03/pdf_read.pynow uses thePyPDF4library instead of the
PyPDF. The book refers to a specific PDF file in the text and
it has been added to the chapter03 subdirectory.
chapter03/exif_fetch.pyrequired afeatures="html.parser"argument on
BeautifulSoup object constructor. It was added on line 15
. This script only works on web applications that wrap images between img
HTML tags (a rare practice on modern web applications that rely heavily on
JavaScript).
chapter03/skype_parse.pyuses themain.dbfile as an example. It has been
chapter03/skype_profile subdirectory for convenience.
chapter03/firefox_parse.pyuses several.sqlitefiles as examples. They
chapter03/firefox_profile subdirectory for
convenience.
chapter03/iphone_messages.pyreferences iPhone backup files that were not
chapter04/geo_ip.pyused the deprecatedpygeoiplibrary. As
geoip2 should now be used. An attempt was made to keep the newly
implemented code as similar as possible to the original implementation on
the book, but some changes had to be made to accommodate the new package
structure of geoip2 and its attributes. The database file necessary to
run the script was downloaded from
MaxMind and made
available in the chapter04 directory. A CLI was also implemented
using argparse.
chapter04/print_direction.pyraised aUnicodeDecodeErrorexception when
chapter04/find_ddos.pyprinted the source address of the Hivemind
traffic.pcap that was not made available by the author, so the code has
been refactored but remains untested.
chapter04/testdomainflux.pyreturns that zero unanswered requests were
chapter05/blue_bug.pyuses thePyBluezlibrary, which in turn requests the
BlueZ library and header files as stated in its
installation instructions.
These dependencies must be installed prior to installing PyBluez as a
requirement. In Linux this can be performed by issuing the command
apt install bluetooth libbluetooth-dev. The original code references a
non-existent clientsock object that has been replaced by phonesock.
chapter05/ftp_sniff.pyhad a logic flaw that made it present a sniffed
if... else
statement. It has been corrected by replacing the conditional statement with
a nested if clause.
chapter05/ninja_print.pyrequires theobexftplibrary to work. This
chapter05/init.pyandchapter06/init.pywere added to enable
chapter05/dup.py and chapter06/anon_browser.py,
respectively.
- chapter06/anon_proxy.py was re-implemented with the MechanicalSoup
Python 3 library. It integrates the modifications that were necessary to
proxytest.py, useragenttest.py and print_cookies.py.
chapter06/anon_browser.pywas also re-implemented withMechanicalSoup
cookielib library
was replaced by http.cookiejar in the constructor method for the
AnonBrowser class and the parameter user_agents now accepts a list of
strings instead of a tuple.
chapter06/link_parser.pywas refactored by using new ways to handle the
re and bs4.
Translations
Contributions in other languages can be checked here:- Translated to Turkish by Bedirhan Budak
Contributing
As a matter of common sense, first try to discuss the change you wish to make to this repository via an issue.
- Ensure the modifications you wish to introduce actually lead to a pull
- If necessary, update the README.md file with details relative to changes to
- Make sure the commit messages that include the modifications follow a
- Your request will be reviewed as soon as possible (usually within 48 hours).