Getting Started With Networking Scripting , And Security In Kali Occupy .

Transcription

ASPIRINGHACKER?START HERE.COVERSKALI LINUXAND PYTHON 3 Keep your internet activity stealthy using Tor, proxyservers, VPNs, and encrypted emailFirst, you’ll learn how to install Kali on a virtual machineand get an introduction to basic Linux concepts. Next,you’ll tackle broader Linux topics like manipulating text,controlling file and directory permissions, and managinguser environment variables. You’ll then focus in on foundational hacking concepts like security and anonymityand learn scripting skills with bash and Python. Build your own hacking tools, such as a remote videospy camera and a password crackerPractical tutorials and exercises throughout will reinforceand test your skills as you learn how to: Cover your tracks by changing your network information and manipulating the rsyslog logging utility Write a tool to scan for network connections, andconnect and listen to wireless networks Write a bash script to scan open ports for potentialtargets Use and abuse services like MySQL, Apache webserver, and OpenSSHHacking is complex, and there is no single way in. Whynot start at the beginning with Linux Basics for Hackers?ABOUT THE AUTHOROccupyTheWeb is an infosec consultant, forensicinvestigator, and trainer with more than 20 years inthe industry. He maintains the Hackers-Arise trainingsite (https://www.hackers-arise.com/) and trains USmilitary personnel, Department of Defense contractors,and federal employees in information security andhacking.w w w.nostarch.com 34.95 ( 45.95 CDN)SHELVE IN:COMPUTERS/SECURITYOCCUPYTHEWEBT H E F I N E ST I N G E E K E N T E RTA I N M E N T LINU X BASICS FOR HACKERSIf you’re getting started along the exciting path ofhacking, cybersecurity, and pentesting, Linux Basicsfor Hackers is an excellent first step. Using Kali Linux,an advanced penetration testing distribution of Linux,you’ll learn the basics of using the Linux operatingsystem and acquire the tools and techniques you’llneed to take control of a Linux environment.LINUX BASICSFOR H ACKERSN E T W O R K I N G,S C R I P T I N G, A N D S E C U R I T Y I N K A L IG E T T I N GS T A R T E DW I T HOCCUPY THE WEB

Linux Basics for Hackers

Linux Basicsfor HackersGetting Started withNetworking, Scripting,a n d  S e c u r i t y i n K a l ib y O c cu p y T h e W e bSan Francisco

LINUX BASICS FOR HACKERS. Copyright 2019 by OccupyTheWeb.All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-10: 1-59327-855-1ISBN-13: 978-1-59327-855-7Publisher: William PollockProduction Editors: Serena Yang and Meg SneeringerCover Illustration: Josh EllingsonInterior Design: Octopod StudiosDevelopmental Editor: Liz ChadwickTechnical Reviewer: Cliff JanzenCopyeditor: Barton D. ReedCompositors: Serena Yang and Meg SneeringerProofreader: Paula L. FlemingIndexer: JoAnne BurekFor information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:No Starch Press, Inc.245 8th Street, San Francisco, CA 94103phone: 1.415.863.9900; info@nostarch.comwww.nostarch.comLibrary of Congress Cataloging-in-Publication DataNames: OccupyTheWeb, author.Title: Linux basics for hackers : getting started with networking, scripting,and security in Kali / OccupyTheWeb.Description: First edition. San Francisco : No Starch Press, Inc., [2018].Identifiers: LCCN 2018030544 (print) LCCN 2018032646 (ebook) ISBN9781593278564 (epub) ISBN 159327856X (epub) ISBN 9781593278557 (print) ISBN 1593278551 (print) ISBN 9781593278564 (ebook) ISBN 159327856X(ebook)Subjects: LCSH: Penetration testing (Computer security) Kali Linux. Hackers. Operating systems (Computers)Classification: LCC QA76.9.A25 (ebook) LCC QA76.9.A25 O325 2018 (print) DDC 005.8--dc23LC record available at https://lccn.loc.gov/2018030544No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Otherproduct and company names mentioned herein may be the trademarks of their respective owners. Ratherthan use a trademark symbol with every occurrence of a trademarked name, we are using the names onlyin an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of thetrademark.The information in this book is distributed on an “As Is” basis, without warranty. While every precautionhas been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have anyliability to any person or entity with respect to any loss or damage caused or alleged to be caused directly orindirectly by the information contained in it.

I dedicate this book to my three incredible daughters.You mean the world to me.

About the AuthorOccupyTheWeb (OTW) is the pseudonym for the founder and primarywriter for the hacker and pentester training website, https://www.hackersarise.com/. He is a former college professor and has over 20 years of experience in the information technology industry. He has trained hackersthroughout the US, including branches of the US military (Army, Air Force,and Navy) and the US intelligence community (CIA, NSA, and DNI). He isalso an avid mountain biker and snow boarder.About the Technical ReviewerSince the early days of Commodore PET and VIC-20, technology has beena constant companion (and sometimes an obsession!) to Cliff Janzen. Cliffdiscovered his career passion when he moved to information security in2008 after a decade of IT operations. Since then, Cliff has had the greatfortune to work with and learn from some of the best people in the industryincluding OccupyTheWeb and the fine people at No Starch during theproduction of this book. He is happily employed as a security consultant,doing everything from policy review to penetration tests. He feels lucky tohave a career that is also his favorite hobby and a wife that supports him.

Brief ContentsAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiChapter 1: Getting Started with the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Chapter 2: Text Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Chapter 3: Analyzing and Managing Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Chapter 4: Adding and Removing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Chapter 5: Controlling File and Directory Permissions . . . . . . . . . . . . . . . . . . . . . . . . . 49Chapter 6: Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Chapter 7: Managing User Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Chapter 8: Bash Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Chapter 9: Compressing and Archiving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Chapter 10: Filesystem and Storage Device Management . . . . . . . . . . . . . . . . . . . . . 101Chapter 11: The Logging System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Chapter 12: Using and Abusing Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Chapter 13: Becoming Secure and Anonymous . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Chapter 14: Understanding and Inspecting Wireless Networks . . . . . . . . . . . . . . . . . 153Chapter 15: Managing the Linux Kernel and Loadable Kernel Modules . . . . . . . . . . . . 165Chapter 16: Automating Tasks with Job Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . 173Chapter 17: Python Scripting Basics for Hackers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Conte nt s in De ta ilAcknowledgmentsxixIntroductionxxiWhat’s in This Book . . . . . . . . . . . . . . . . . . . . . .What Is Ethical Hacking? . . . . . . . . . . . . . . . . . . .Penetration Testing . . . . . . . . . . . . . . . . .Military and Espionage . . . . . . . . . . . . . .Why Hackers Use Linux . . . . . . . . . . . . . . . . . . . .Linux Is Open Source . . . . . . . . . . . . . . .Linux Is Transparent . . . . . . . . . . . . . . . .Linux Offers Granular Control . . . . . . . . .Most Hacking Tools Are Written for Linux .The Future Belongs to Linux/Unix . . . . . . .Downloading Kali Linux . . . . . . . . . . . . . . . . . . . .Virtual Machines . . . . . . . . . . . . . . . . . . . . . . . . .Installing VirtualBox . . . . . . . . . . . . . . . .Setting Up Your Virtual Machine . . . . . . .Installing Kali on the VM . . . . . . . . . . . . .Setting Up Kali . . . . . . . . . . . . . . . . . . . . . . . . . .xxiixxiiixxiiixxiiixxivxxivxxivxxivxxivxxiv. xxvxxvixxvixxviixxixxxxi1Getting Started with the BasicsIntroductory Terms and Concepts . . . . . . . . . . . . . . . . . . .A Tour of Kali . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . .The Linux Filesystem . . . . . . . . . . . . . . . . . . . . . .Basic Commands in Linux . . . . . . . . . . . . . . . . . . . . . . . . .Finding Yourself with pwd . . . . . . . . . . . . . . . . . .Checking Your Login with whoami . . . . . . . . . . . .Navigating the Linux Filesystem . . . . . . . . . . . . . .Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . .Referencing Manual Pages with man . . . . . . . . . .Finding Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Searching with locate . . . . . . . . . . . . . . . . . . . . .Finding Binaries with whereis . . . . . . . . . . . . . . .Finding Binaries in the PATH Variable with which .Performing More Powerful Searches with find . . . .Filtering with grep . . . . . . . . . . . . . . . . . . . . . . .Modifying Files and Directories . . . . . . . . . . . . . . . . . . . . .Creating Files . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a Directory . . . . . . . . . . . . . . . . . . . . . .Copying a File . . . . . . . . . . . . . . . . . . . . . . . . . .1. 1. 3. 4. 4. 5. 6. 6. 6. 8. 9. 9101010111213131515

Renaming a File . . . . .Removing a File . . . . .Removing a Directory .Go Play Now! . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . .2Text Manipulation19Viewing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Taking the Head . . . . . . . . . . . . . . . . . . . . . . . .Grabbing That Tail . . . . . . . . . . . . . . . . . . . . . . .Numbering the Lines . . . . . . . . . . . . . . . . . . . . . .Filtering Text with grep . . . . . . . . . . . . . . . . . . . . . . . . . .Hacker Challenge: Using grep, nl, tail, and head .Using sed to Find and Replace . . . . . . . . . . . . . . . . . . . . .Viewing Files with more and less . . . . . . . . . . . . . . . . . . . .Controlling the Display with more . . . . . . . . . . . . .Displaying and Filtering with less . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3Analyzing and Managing Networks.4Adding and Removing Softwarexii   Contents in Detail.20202122222323242525262729Analyzing Networks with ifconfig . . . . . . . . . . . . . . . . . . . . . .Checking Wireless Network Devices with iwconfig . . . . . . . . . .Changing Your Network Information . . . . . . . . . . . . . . . . . . . .Changing Your IP Address . . . . . . . . . . . . . . . . . . . .Changing Your Network Mask and Broadcast Address .Spoofing Your MAC Address . . . . . . . . . . . . . . . . . .Assigning New IP Addresses from the DHCP Server . . .Manipulating the Domain Name System . . . . . . . . . . . . . . . . .Examining DNS with dig . . . . . . . . . . . . . . . . . . . . . .Changing Your DNS Server . . . . . . . . . . . . . . . . . . .Mapping Your Own IP Addresses . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using apt to Handle Software . . . . . . . . . . .Searching for a Package . . . . . . . .Adding Software . . . . . . . . . . . . .Removing Software . . . . . . . . . . .Updating Packages . . . . . . . . . . .Upgrading Packages . . . . . . . . . .Adding Repositories to Your sources.list File .Using a GUI-based Installer . . . . . . . . . . . .Installing Software with git . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . 424345464747

5Controlling File and Directory PermissionsDifferent Types of Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Granting Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Granting Ownership to an Individual User . . . . . . . . . . .Granting Ownership to a Group . . . . . . . . . . . . . . . . . .Checking Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing Permissions with Decimal Notation . . . . . . . . .Changing Permissions with UGO . . . . . . . . . . . . . . . . . .Giving Root Execute Permission on a New Tool . . . . . . . .Setting More Secure Default Permissions with Masks . . . . . . . . . . .Special Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Granting Temporary Root Permissions with SUID . . . . . . .Granting the Root User’s Group Permissions SGID . . . . . .The Outmoded Sticky Bit . . . . . . . . . . . . . . . . . . . . . . . .Special Permissions, Privilege Escalation, and the Hacker .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49.6Process ManagementViewing Processes . . . . . . . . . . . . . . . . . . . . . . .Filtering by Process Name . . . . . . . . . . .Finding the Greediest Processes with top .Managing Processes . . . . . . . . . . . . . . . . . . . . .Changing Process Priority with nice . . . .Killing Processes . . . . . . . . . . . . . . . . .Running Processes in the Background . . .Moving a Process to the Foreground . . . .Scheduling Processes . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505050515152525455565757585858606061.7Managing User Environment VariablesViewing and Modifying Environment Variables . . . . . .Viewing All Environment Variables . . . . . . . .Filtering for Particular Variables . . . . . . . . . .Changing Variable Values for a Session . . . .Making Variable Value Changes Permanent .Changing Your Shell Prompt . . . . . . . . . . . . . . . . . . .Changing Your PATH . . . . . . . . . . . . . . . . . . . . . . .Adding to the PATH Variable . . . . . . . . . . . .How Not to Add to the PATH Variable . . . . .Creating a User-Defined Variable . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ontents in Detail   xiii

8Bash Scripting81A Crash Course in Bash . . . . . . . . . . . . . . . . . . . . . . . . . . .Your First Script: “Hello, Hackers-Arise!” . . . . . . . . . . . . . . .Setting Execute Permissions . . . . . . . . . . . . . . . . . .Running HelloHackersArise . . . . . . . . . . . . . . . . . .Adding Functionality with Variables and User Input .Your Very First Hacker Script: Scan for Open Ports . . . . . . . .Our Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Simple Scanner . . . . . . . . . . . . . . . . . . . . . . . .Improving the MySQL Scanner . . . . . . . . . . . . . . . .Common Built-in Bash Commands . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9Compressing and Archiving93What Is Compression? . . . . . . . . . . . . . . . . . . . . . . . . .Tarring Files Together . . . . . . . . . . . . . . . . . . . . . . . . .Compressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .Compressing with gzip . . . . . . . . . . . . . . . . . .Compressing with bzip2 . . . . . . . . . . . . . . . . .Compressing with compress . . . . . . . . . . . . . .Creating Bit-by-Bit or Physical Copies of Storage Devices .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10Filesystem and Storage Device ManagementThe Device Directory /dev . . . . . . . . . . . . . . . . . . . .How Linux Represents Storage Devices . . . . .Drive Partitions . . . . . . . . . . . . . . . . . . . . .Character and Block Devices . . . . . . . . . . . .List Block Devices and Information with lsblk .Mounting and Unmounting . . . . . . . . . . . . . . . . . . . .Mounting Storage Devices Yourself . . . . . . .Unmounting with umount . . . . . . . . . . . . . . .Monitoring Filesystems . . . . . . . . . . . . . . . . . . . . . . .Getting Information on Mounted Disks . . . . .Checking for Errors . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11The Logging 01102103103105105106106107107107108109109111The rsyslog Logging Daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112The rsyslog Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112The rsyslog Logging Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113xiv   Contents in Detail

Automatically Cleaning Up Logs with logrotate .Remaining Stealthy . . . . . . . . . . . . . . . . . . . .Removing Evidence . . . . . . . . . . . . .Disabling Logging . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . .12Using and Abusing Services121Starting, Stopping, and Restarting Services . . . . . . . . . . . . .Creating an HTTP Web Server with the Apache Web Server .Starting with Apache . . . . . . . . . . . . . . . . . . . . . .Editing the index.html File . . . . . . . . . . . . . . . . . . .Adding Some HTML . . . . . . . . . . . . . . . . . . . . . . .Seeing What Happens . . . . . . . . . . . . . . . . . . . . .OpenSSH and the Raspberry Spy Pi . . . . . . . . . . . . . . . . . .Setting Up the Raspberry Pi . . . . . . . . . . . . . . . . . .Building the Raspberry Spy Pi . . . . . . . . . . . . . . . .Configuring the Camera . . . . . . . . . . . . . . . . . . . .Starting to Spy . . . . . . . . . . . . . . . . . . . . . . . . . . .Extracting Information from MySQL . . . . . . . . . . . . . . . . . . .Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . .Interacting with MySQL . . . . . . . . . . . . . . . . . . . . .Setting a MySQL Password . . . . . . . . . . . . . . . . . .Accessing a Remote Database . . . . . . . . . . . . . . . .Connecting to a Database . . . . . . . . . . . . . . . . . . .Database Tables . . . . . . . . . . . . . . . . . . . . . . . . .Examining the Data . . . . . . . . . . . . . . . . . . . . . . .PostgreSQL with Metasploit . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13Becoming Secure and AnonymousHow the Internet Gives Us Away . . . . . . . .The Onion Router System . . . . . . . . . . . . .How Tor Works . . . . . . . . . . . . .Security Concerns . . . . . . . . . . .Proxy Servers . . . . . . . . . . . . . . . . . . . . .Setting Proxies in the Config File .Some More Interesting Options . .Security Concerns . . . . . . . . . . .Virtual Private Networks . . . . . . . . . . . . . .Encrypted Email . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . 42143144146148148150151151Contents in Detail   xv

14Understanding and InspectingWireless Networks153Wi-Fi Networks . . . . . . . . . . . . . . . . . . . . . . . . .Basic Wireless Commands . . . . . . . . . .Wi-Fi Recon with aircrack-ng . . . . . . . . .Detecting and Connecting to Bluetooth . . . . . . . .How Bluetooth Works . . . . . . . . . . . . . .Bluetooth Scanning and Reconnaissance .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Using Kali Linux, an advanced penetration testing distribution of Linux, you'll learn the basics of using the Linux operating system and acquire the tools and techniques you'll need to take control of a Linux environment. First, you'll learn how to install Kali on a virtual machine and get an introduction to basic Linux concepts. Next,