Hack Like a Pro: Scripting for the Aspiring Hacker, Part 2 (Conditional Statements) « Null Byte :: WonderHowTo

Welcome back, my greenhorn hackers!I recently began aseries on scriptingand have received such positive feedback that I'm going to keep this series going. As I've said before, to graduate from the script kiddie to the pro hacker, you'll need to have some scripting skills. The better you are at scripting, the more advanced your hacking. Ultimately, we are leading up to developing the skills to build your own zero day exploits.I left off teaching you thebasics of shell scripting in BASHand built a simple script that utilizednmapwith our user-supplied inputs to scan for open ports on a range of IP addresses. I hope you saved that script, as we'll be building upon it here, adding additional functionality.Step 1: Open a Text EditorThe first step in building any script is to open a text editor. You can use any text editor, but I will be using kwrite in the KDE version ofBackTrack(if you are using the GNOME version, gedit works just as well).Now, in this lesson, we'll be studying conditional statements. Those are statements within our script that enable us to make decisions. In other words, "If this happens, do this. If the other happens, then do that."Step 2: If...then...elseThe most basic type of conditional statement that is available in nearly every programming language is theif...then...else. This enables us to check for a condition (if) and if it is true,thenexecute some statement or statements, orelsedo something different. Its basic form is:if<a conditional statement that evaluates to true or false>then<statements to execute when true>else<statements to execute if false>fiTheelseis optional, as the conditional statement will run without the else clause. In BASH shell scripting, every if..then must be closed with a "fi" or the reverse of the "if."Step 3: Let's Add a Conditional to Our Scanner ScriptNow that we have the basic concept of a conditional script, let's add one to our scanning script namedScannerscript. Let's ask the user whether they want to use nmap orhping3to scan the target. If they say they want nmap, then we can execute the original part of our script that does the nmap scan. If they want to use hping3, we will have to add a new section to gather info and then run an hping scan. So, our script structure should look like this:ifyou want to run nmapthenrun nmap prompts and commandselserun hping prompts and commandsStep 4: Create Our Conditional StatementNow, let's edit our Scannerscript to give the user a choice of either using nmap or hping to scan. First, we need ask the user which they want to use with anechostatement.echo "Would you like to scan using nmap or hping"This simply asks the user to enter which scanner they would like to use and enter it from the keyboard. Next, we need to capture their input into a variable named scanner.read scannerNow, comes the key part of our conditional statement, the if...then...else. We create a statement that checks to see what value the user entered when prompted, and then sends our script to either the nmap section, or our yet to be created hping section. We can do this with the following statement:if "$scanner" = "nmap" thenPlease note a few things here.First, theifis lowercase. Anything else will throw a "command not found" error. Second, I used double quotation marks around the value of the variablescanner. This is to indicate that I want a string to compare to the string "nmap". Third, when retrieving the value of a variable, I precede it with a$sign.Step 5: Build the Hping Scan SectionNow that we've developed our basic structure and logic of our conditional statement, let's build the hping section that will be executed if the user chooses they want to run an hping scan. Let's start by prompting the user for which IP address they want to scan and capturing the data into a variable calledIPaddress.echo "Which IP address would you like to scan? :read IPaddressNext, let's prompt the user which port they would like to scan and capture the data into a variable calledhpingport.echo "What port would you like to scan for ?:read hpingportFinally, let's ask the user how many packets they would like to send. Remember, hping continues to send packets until stopped just like the ping command in Linux (and unlike theping command in Windowsthat only sends 4 packets and then stops). We can determine how many packets to scan by using the-cswitch in the hping command. So, let's ask the user how many packets they would like to send and gather that info into a variable calledpackets.echo "How many packets would you like to send?"read packetsStep 6: Create Our hping3 Scan CommandNow we have all the information we need from the user and saved it into variables to create our hping3 command.hping3 -c $packets $IPaddress -p $hpingport > hpingscanThis statement says run the hping3 command and send$packetsnumber of packets to the$IPaddressIP address, scanning for$hpingportport open and send it all to a file namedhpingscan.Step 7: Let's Test ItTo run this script and see whether it works, let's first re-save it as Scannerscript. Now let's open a terminal and run it by typing;./ScannerscriptAs you can see, when I asked it to run the nmap scan, it did just that.Now, comes the critical part. Let's ask our Scannerscript to run an hping scan. To do so, it will have to check to see whether the user requested a nmap or hping scan, and if an hping scan, skip over the nmap section of our code and go directly to the hping section. This utilizes our conditional statement. Let's give it a try.Success! Our script enabled the user to select either a nmap or a hping scan by using the conditional if...then...else!Infuture tutorials, we will continue to build functionality to this script, so make sure you save it. We will eventually look at other scripting languages such as Perl, Ruby, and Python so we can build our own zero day exploit!Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseRelatedHack Like a Pro:Python Scripting for the Aspiring Hacker, Part 3 (Building an FTP Password Cracker)How To:The Essential Skills to Becoming a Master HackerHow to Train Your Python:Part 6, If, Else, and Conditional StatementsNews:How to Study for the White Hat Hacker Associate Certification (CWA)Hack Like a Pro:Perl Scripting for the Aspiring Hacker, Part 2 (Building a Port Scanner)Hack Like a Pro:Python Scripting for the Aspiring Hacker, Part 1Hack Like a Pro:Perl Scripting for the Aspiring Hacker, Part 1Hack Like a Pro:Metasploit for the Aspiring Hacker, Part 14 (Creating Resource Script Files)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 21 (GRUB Bootloader)Hack Like a Pro:Python Scripting for the Aspiring Hacker, Part 2How To:Perl for the Aspiring Hacker - Part 1 - VariablesHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 11 (Apache Web Servers)How To:How Hackers Cover Their Tracks on an Exploited Linux Server with Shell ScriptingHow to Train Your Python:Part 12, Logical and Membership OperatorsHack Like a Pro:Using the Nmap Scripting Engine (NSE) for ReconnaissanceHow To:This Extensive Python Training Is Under $40 TodayHow To:Get Started Writing Your Own NSE Scripts for NmapHack Like a Pro:How to Use PowerSploit, Part 1 (Evading Antivirus Software)How To:Use conditional statements when writing ActionScript in Flash Professional CS5How To:Perl for the Aspiring Hacker (Control Statements)How To:Bash (Shell) Scripting for BeginnersGoodnight Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 2 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 7 - Legal HackerGoodnight Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 4 - Legal Hacker TrainingHow To:Schedule curtain panels in Revit conditional statementCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingGoodnight Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 5 - Real Hacking SimulationsCommunity Byte:HackThisSite Walkthrough, Part 7 - Legal Hacker TrainingCommunity Byte:HackThisSite, Realistic 4 - Real Hacking SimulationsGoodnight Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingCommunity Byte:HackThisSite, Realistic 1 - Real Hacking SimulationsNews:Indie and Mainstream Online Games Shut Down by LulzSec