Fully-updated for Python 3, the second edition of this worldwide bestseller (over 100,000 copies sold) explores the stealthier side of programming and brings you all new strategies for your hacking projects. When it comes to creating powerful and effective hacking tools, Python is the language of choice for most security analysts. In Black Hat Python, 2nd Edition, you’ll explore the darker side of Python’s capabilities—writing network sniffers, stealing email credentials, brute forcing directories, crafting mutation fuzzers, infecting virtual machines, creating stealthy trojans, and more. The second edition of this bestselling hacking book contains code updated for the latest version of Python 3, as well as new techniques that reflect current industry best practices. You’ll also find expanded explanations of Python libraries such as ctypes, struct, lxml, and BeautifulSoup, and dig deeper into strategies, from splitting bytes to leveraging computer-vision libraries, that you can apply to future hacking projects. You’ll learn how to: • Create a trojan command-and-control using GitHub • Detect sandboxing and automate common malware tasks, like keylogging and screenshotting • Escalate Windows privileges with creative process control • Use offensive memory forensics tricks to retrieve password hashes and inject shellcode into a virtual machine • Extend the popular Burp Suite web-hacking tool • Abuse Windows COM automation to perform a man-in-the-browser attack • Exfiltrate data from a network most sneakily When it comes to offensive security, your ability to create powerful tools on the fly is indispensable. Learn how with the second edition of Black Hat Python. New to this edition: All Python code has been updated to cover Python 3 and includes updated libraries used in current Python applications. Additionally, there are more in-depth explanations of the code and the programming techniques have been updated to current, common tactics. Examples of new material that you'll learn include how to sniff network traffic, evade anti-virus software, brute-force web applications, and set up a command-and-control (C2) system using GitHub. Foreword 16 Preface 18 Acknowledgments 20 Chapter 1: Setting Up Your Python Environment 22 Installing Kali Linux 23 Setting Up Python 3 24 Installing an IDE 26 Code Hygiene 26 Chapter 2: Basic Networking Tools 30 Python Networking in a Paragraph 31 TCP Client 31 UDP Client 32 TCP Server 33 Replacing Netcat 34 Kicking the Tires 38 Building a TCP Proxy 40 Kicking the Tires 45 SSH with Paramiko 47 Kicking the Tires 51 SSH Tunneling 51 Kicking the Tires 55 Chapter 3: Writing a Sniffer 56 Building a UDP Host Discovery Tool 57 Packet Sniffing on Windows and Linux 57 Kicking the Tires 59 Decoding the IP Layer 59 The ctypes Module 60 The struct Module 62 Writing the IP Decoder 64 Kicking the Tires 66 Decoding ICMP 67 Kicking the Tires 71 Chapter 4: Owning the Network with Scapy 74 Stealing Email Credentials 75 Kicking the Tires 78 ARP Cache Poisoning with Scapy 78 Kicking the Tires 83 pcap Processing 84 Kicking the Tires 90 Chapter 5: Web Hackery 92 Using Web Libraries 93 The urllib2 Library for Python 2.x 93 The urllib Library for Python 3.x 94 The requests Library 95 The lxml and BeautifulSoup Packages 95 Mapping Open Source Web App Installations 97 Mapping the WordPress Framework 97 Testing the Live Target 101 Kicking the Tires 102 Brute-Forcing Directories and File Locations 103 Kicking the Tires 106 Brute-Forcing HTML Form Authentication 106 Kicking the Tires 111 Chapter 6: Extending Burp Proxy 114 Setting Up 115 Burp Fuzzing 116 Kicking the Tires 122 Using Bing for Burp 125 Kicking the Tires 129 Turning Website Content into Password Gold 131 Kicking the Tires 134 Chapter 7: GitHub Command and Control 138 Setting Up a GitHub Account 139 Creating Modules 140 Configuring the Trojan 141 Building a GitHub-Aware Trojan 142 Hacking Python’s import Functionality 144 Kicking the Tires 145 Chapter 8: Common Trojaning Tasks on Windows 148 Keylogging for Fun and Keystrokes 149 Kicking the Tires 151 Taking Screenshots 152 Pythonic Shellcode Execution 153 Kicking the Tires 155 Sandbox Detection 156 Chapter 9: Fun with Exfiltration 160 Encrypting and Decrypting Files 161 Email Exfiltration 163 File Transfer Exfiltration 165 Exfiltration via a Web Server 166 Putting It All Together 169 Kicking the Tires 171 Chapter 10: Windows Privilege Escalation 174 Installing the Prerequisites 175 Creating the Vulnerable BlackHat Service 175 Creating a Process Monitor 177 Process Monitoring with WMI 178 Kicking the Tires 179 Windows Token Privileges 180 Winning the Race 182 Kicking the Tires 185 Code Injection 185 Kicking the Tires 187 Chapter 11: Offensive Forensics 190 Installation 191 General Reconnaissance 192 User Reconnaissance 194 Vulnerability Reconnaissance 197 The volshell Interface 198 Custom Volatility Plug-Ins 198 Kicking the Tires 203 Onward! 205 Index 206 "When it comes to creating powerful and effective hacking tools, Python is the language of choice for most security analysts. In this second edition of the bestselling Black Hat Python, you'll explore the darker side of Python's capabilities: everything from writing network sniffers, stealing email credentials, and bruteforcing directories to crafting mutation fuzzers, investigating virtual machines, and creating stealthy trojans. All of the code in this edition has been updated to Python 3.x. You'll also find new coverage of bit shifting, code hygiene, and offensive forensics with the Volatility Framework as well as expanded explanations of the Python libraries ctypes, struct, lxml, and BeautifulSoup, and offensive hacking strategies like splitting bytes, leveraging computer vision libraries, and scraping websites. You'll even learn how to: " Create a trojan command-and-control server using GitHub " Detect sandboxing and automate common malware tasks like keylogging and screenshotting " Extend the Burp Suite web-hacking tool " Escalate Windows privileges with creative process control " Use offensive memory forensics tricks to retrieve password hashes and find vulnerabilities on a virtual machine " Abuse Windows COM automation " Exfiltrate data from a network undetected When it comes to offensive security, you need to be able to create powerful tools on the fly. Learn how with Black Hat Python."--Amazon.com