The Basics of Hacking and Pen Testing
The Basics of Hacking and Pen Testing
1
18/01/2016
https://www.amazon.co.uk/Basics-Hacking-Penetration-Testing-Ethical/dp/0124116442/
NOTES
- There may be additional methods/ techniques required for use against modern systems.
- These are my hacky notes. This document will not be perfect.
- Even if you read this, I would still recommend reading the book itself. It’s excellent!
- This knowledge is largely that of the author. At this point in time, I have had little practice.
This document is my way of having a quick reference. The ‘Red Team Field Manual’ is also
good as a quick reference.
Table of Contents
Steps........................................................................................................................................................ 4
Authorization .......................................................................................................................................... 4
Reconnaissance (Passive and Active) ...................................................................................................... 5
Definitions ........................................................................................................................................... 5
Aim/Purpose of Recon ........................................................................................................................ 5
Google ................................................................................................................................................. 5
Search Engine Directives Search (Johnny Long’s fame) ...................................................................... 6
Research Associates of Merged Companies ....................................................................................... 6
HTTRACK ............................................................................................................................................. 6
Other Online Sources .......................................................................................................................... 7
Social Media ........................................................................................................................................ 7
The Harvester ...................................................................................................................................... 7
Whois.NET and Netcraft ..................................................................................................................... 8
DNS...................................................................................................................................................... 8
NsLookup ............................................................................................................................................ 8
Dig - Attempting Zone Transfer .......................................................................................................... 9
Fierce................................................................................................................................................... 9
Email Server ........................................................................................................................................ 9
Metagoofil........................................................................................................................................... 9
Threat Agent ..................................................................................................................................... 10
Social Engineering ............................................................................................................................. 10
J J v1.1
18/01/2016
Roundup ............................................................................................................................................ 10
Scanning ................................................................................................................................................ 11
Aim of Scanning ................................................................................................................................ 11
Definitions ......................................................................................................................................... 11
Overview ........................................................................................................................................... 12
Ping (Single)....................................................................................................................................... 13
Ping (Multiples) ................................................................................................................................. 13
Port Scanning .................................................................................................................................... 13
TCP Connect Scans ............................................................................................................................ 14
SYN Scans .......................................................................................................................................... 14
UDP Scans ......................................................................................................................................... 15
XMAS Tree Scans and Null Scans ...................................................................................................... 15
Scripting NMAP ................................................................................................................................. 16
Telnet / SSH....................................................................................................................................... 16
Vulnerability Scanning ...................................................................................................................... 16
Exploitation ........................................................................................................................................... 18
Aim .................................................................................................................................................... 18
Brute Force Accessing Remote Services with Medusa / Hydra ........................................................ 18
Metasploit ......................................................................................................................................... 19
More password cracking – Local – Samdump2 and John (JtR) ......................................................... 21
Password Cracking- Remote ............................................................................................................. 22
Sniffing Network Traffic .................................................................................................................... 22
Armitage – The Hail Mary ................................................................................................................. 23
Social Engineering ................................................................................................................................. 24
Aim .................................................................................................................................................... 24
SET – Social Engineering Toolkit ....................................................................................................... 24
Web Exploitation................................................................................................................................... 25
NikitoWeb Server Vuln Scanning ...................................................................................................... 25
W3AF – Finding standard weaknesses.............................................................................................. 25
Webscarab(Full Spidering) ................................................................................................................ 25
ZAP Spidering, Proxies and Man-In-The-Middle ............................................................................... 26
Injection attacks ................................................................................................................................ 26
XSS – Cross Site Scripting .................................................................................................................. 26
Post Exploitation – Maintaining Access ................................................................................................ 27
Aim .................................................................................................................................................... 27
NetCat and CryptCat ......................................................................................................................... 27
J J v1.1
18/01/2016
Rootkits ............................................................................................................................................. 27
Defending against rootkits ................................................................................................................ 28
Report ................................................................................................................................................... 29
Executive Summary........................................................................................................................... 29
Walkthrough Report ......................................................................................................................... 29
Detailed Report ................................................................................................................................. 30
After Care .......................................................................................................................................... 30
J J v1.1
18/01/2016
Steps
1. Authorization
2. Reconnaissance (including research, scanning, vulnerability scanning)
3. Exploitation
4. Post Exploitation (keeping a back door open)
5. Back Hat – Hiding
6. White Hat – Reports (Management Overview and Technical Report)
Authorization
- For white hat hacking, we need permission for the Pen Test.
- Scope must be agreed (on/off limits), especially with regard to maintaining back doors and
rootkits.
- Can be expanded, but must be re-signed off before progressing
- Should have insurance in case you accidentally trash their systems
- Everything must remain confidential.
J J v1.1
18/01/2016
Definitions
Passive Reconnaissance – Gather data about the client, but without making direct contact. This is
largely untraceable.
Active Reconnaissance – Gathering data from the client e.g. directly from their website/servers, or
by contacting them.
Aim/Purpose of Recon
The main goals are to acquire IP addresses, email addresses and, if possible, logins.
Remember that email addresses are often closely linked with domain names, so could give us logins.
E.g. [email protected], could have a login of the email address or ben.owned2, ownedb,
ownedb2 etc. There are tools to help us work out domain accounts from emails (see later on)
Remember when researching people that little details can lead to big clues. Someone’s Facebook
post or tweet could give away the starter for a password.
- IP’s
- Email addresses
- Host names
- Urls
- User Names
- Additional relevant information
Google the company. Gather as much detail about them as possible. E.g.
- What they do
- Where they’re based
- Contact details
- Employee details
- Boss details (esp small companies, the boss probably still has access!)
- Other relevant info
J J v1.1
18/01/2016
Use google directives search to get more information. Some searches are now blocked, so need to
be inventive.
As title suggests, research merged companies. These may have access to the IT infrastructure.
Mergers always lead to holes in the infra.
HTTRACK
HTTRACK creates a copy of the clients’ website. You can then do further recon/scanning of their site
offline. THIS IS TRACEABLE
- EMAIL ADDRESSES!!!!
- IP Addresses!!!! (possibly in Excel, PPT, Word, or PDF’s)
- Employee names (esp. tech staff)
- Social media accounts (esp. of employees)
- Physical address
- Location
- Phone
- Business hours (so we can hack them out of hours, or contact them during work hours)
- Business relationships (who they do business with)
- Mergers and Acquisitions (more routes in for the future)
- Job Adverts with details of their tech stack/firewalls. Can also get this from job sites
- Robots.txt
J J v1.1
18/01/2016
Forums, news groups, bulletin boards even live chat could reveal info about their software,
hardware, email addresses and configs.
Social Media
People give away tons of details on social media. Investigate employees Facebook, Twitter,
Instagram etc. Might find revealing tweets “Battling with Cisco all day” (Thank you!) or info about
them that could lead to passwords.
The Harvester
Use ‘The Harvester’ or another email scraper to get company email addresses. It’s built into Kali.
Keep it updated.
Cmd: theharvester
Cmd: updatedb
Locate the harvester folder
Usr/bin/theharvester (if not working by default)
-d = domain
-l = number of results to get
-b = search engine. Could be google, bing, yahoo, PGP?, LinkedIn
J J v1.1
18/01/2016
http://news.netcraft.com will give you IP’s, DNS settings and Operating Systems.
If you get host names rather than IPs from whois/netcraft, you can use the host command to get IPs.
The IP can also be used to get the host, but generally, the query will look like:
DNS
We hope one of these will be the companies DNS Server. DNS Servers are important as they contain
a list of IP’s we can target. Companies DNS systems often weak. Admins know if they mess up the
DNS, bad things happen, so can be wary of touching it, leaving it unpatched and vulnerable.
DNS servers use zone transfer to share/sync their records with other DNS servers. We may be able
to perform a zone transfer, but it is now much harder to do. If we could do a zone transfer, it would
give us all IP mappings for the company. Even if the transfer fails, it’s worth investigating.
NsLookup
Cmd: nslookup
>server 8.8.8.8
It is likely this will be blocked, but we may just get a list of host/IP addresses. Do this for all IP
addresses. Not completely sure how we know which is the DNS server, so trying all for the moment.
Fierce
May need to install this via atp -get install fierce. Fierce will attempt a zone transfer. It will then
attempt to brute force host names by sending queries to the server. This uncovers additional targets
e.g. mail.example.com. Assume use “man fierce” for more options, but the basic is:
Cmd: cd usr/bin/fierce
Cmd: ./fierce –dns example.com
Email Server
We can get information about the company’s setup from emails. We could send in a sales request
and then check the headers on the returned mail. Alternatively we could send an email with an
empty.bat file or empty.exe. The email server will hopefully reject it. Good email servers will quietly
swallow this, but less secure ones will return a reject email including info such as headers (from
which we can get the IP), server branding, anti-virus brand.
Metagoofil
Metagoofil searches the internet for meta data about the target e.g.
- User names
- Computer/server names
- Network paths/file shares
- IPs?
Cmd: cd /usr/bin/metagoofil
Cmd: mkdirmytargetfiles
Cmd: ./metagoofil.py –d syngress.com –t pdf,doc,xls,pptx –n 20 –o
mytargetfiles –f results.html
./ = current directory
J J v1.1
18/01/2016
-d = domain switch
-t = file types
-n = number of each file type to report on
-o = save to the this folder
-f = output summary to here.
If all we got back from this was a directory path, it could still tell us a username and OS.
For windows, Search Diggity and FCOA are also useful for this.
Threat Agent
http://www.threatagent.com
Threat agent does a lot of harvesting for you. Sign up for a free account. Enter target organisation or
domain. It emails you info, IP’s, email addresses, contacts, even open ports. Highly recommended by
Patrick Engeretson. Creates an entire dossier for you!
Social Engineering
Social engineering is about exploiting human weaknesses. Will be more on this later. For example, a
conversation with someone from the company could reveal a lot, an email to the sales team could
reveal IP’s.
Could impersonate someone on holiday, or drop carefully labelled USB’s (Annual Reports, Company
Salaries, Employee Reviews!) around in hope someone will put it into their laptop. This would run a
script enabling us access to the system, open a back door and dial home to the attacker.
Need to be very careful. Often only get one chance and once the alarm is sounded, everyone gets
suspicious.
Roundup
At the end of the reconnaissance, you should compile your results into the list of items show in the
Aim/Purpose of Recon section.
J J v1.1
18/01/2016
Scanning
Aim of Scanning
Every port is a potential gateway to our target system. See p56 or google for commonly open
ports/services.
Make special notes of any IP address that includes some type of remote access service (e.g. SSH. FTP,
PCAnywhere, VNC, RDP as these will help ‘own’ a system.
Even if the open system is a perimeter device and not the ‘gold’ target, it may be worth pivoting
from to get there. It might encounter fewer restrictions, or be able to discover other
machines/devices.
Definitions
Perimeter devices – computers, servers, routers, firewalls or other equipment with sits at the outer
edge of a protected network.
TCP – Transmission control protocol. Connection oriented protocol, meaning it requires packets to
arrive in the order they were sent. Ensures packets are received.
UDP – user datatgram protocol. Connectionless protocol. Packets can arrive in any order, but sender
has no means of knowing/ensuring all packets were received at the other end.
Overview
NOTE: Some companies will turn off PING responses (IMCP packets), so don’t worry too much if
there’s nothing for step 1; Nmap/Nessus might provide something later. If we can ping, they
probably haven’t secured this server very well.
J J v1.1
18/01/2016
Ping (Single)
Try sending an IMCP echo request packet to test if a computer is on and responding.
NOTE: Windows pings will send 4 packets, Linux pings will continue until you stop them. (Ctrl+C).
If it’s offline or blocking IMCP packets, will see 100% loss or ‘destination host unreachable’.
Ping (Multiples)
FPing will save manually pinging each IP. Use something along the lines of the following to ping a
range. Note: lookup comma separated lists or reading from a list in case company does not have
range (Cmd: “man fping”).
Port Scanning
The purpose of scanning is to identify open ports and the services running on the target system. E. g
email, FPT, printing, web server. For example, if we find port 80 is open, it is likely it is a web server.
There are 65,536 ports on every computer, either using TCP or UDP.
Good as gracefully completes handshake (stable, less chance of crash). If you don’t specify a port,
Nmap will scan the 1000 most common.
Can also import from a text file to save running many scans! (Cmd: man fmap to find out?)
SYN Scans
The default type. Faster than TCP connect, with little chance of DOS’ing the server. Only completes
the first 2 parts of the handshake. Equivalent of a prank call to see if the user answers. Also most
stealthy than TCP connect as the connection is never completed there is a small chance it won’t be
logged.
NOTE: -sV would be a version scan and –T (scale 0-5 fastest) can set the speed of the scan. Faster =
less accurate.
J J v1.1
18/01/2016
UDP Scans
Scanning is slow (10 seconds per IP), so probably ok to search top 1000 ports rather than all of them!
It is also rare for a server to return a message. Most likely the firewall will swallow it, giving us a
response of ‘open|filtered’. For this reason, we also use the version flag in Nmap to try to illicit a
better response.
Both of these violate the TCP communication and can find stuff TCP scans do not, but DO NOT WORK
ON WINDOWS
Can send a TCP RFC (request for comment) to determine the current state of a port. ONLY works on
UNIX and Linux.
df
J J v1.1
18/01/2016
Scripting NMAP
NSE – Nmap scripting Engine, built into Nmap, provides a lot of additional functionality. Large
community with scripts available. Review docs for each script before running.
http://nmap.org/nsedoc .
Can run a single script or whole category. Huge functionality can even exploit. Check docs, but
examples:
Check out ‘Common Vulnerabilities and Exposures’ (CVE), Open Source Vulnerability Database
(OSVDB) or links provided.
Telnet / SSH
With your list of IP’s/ports, you may be able to log into any remote access services using the default
username and password. Unlikely, but always worth a try.
Vulnerability Scanning
Nessus is a good tool for this. May need to register for a non-commercial HomeFeed Key from
Nessus.org. Keep plugins updated.
Install (p73)
NOTE: in the Nessus config, make sure you check “Safe Checks”. Some vuln plugins will actually
exploit the target. We don’t want that yet.
If you have problems, make sure you restart the service (last call above).
Click Scans > New Scan and fill in the form. Make sure you name it well for future reference. If you
have a text file of IP addresses (which you should!), you can load it.
J J v1.1
18/01/2016
The results will show you known vulnerabilities found by the scan (e.g. p89), which you can use in
the exploitation step. E.g
OpenVas is like Nessus and can also be handy. We will just use Nessus.
J J v1.1
18/01/2016
Exploitation
Aim
The aim of exploitation is ideally to gain control of a machine with admin privileges. May get some or
all of the way there. For example, you may not get admin access, but may still be able to download
files.
Using some user names (and system guesses) a password cracker can be used to attempt to log in.
Many systems now include some form of password throttling to limit number of attempts. After x
fails, your IP may be blocked, or the user locked out.
For online password cracking, Medusa and Hydra are well known. For medusa, need IP address, user
name/ user name list, a password or dictionary file containing passwords and the name of the
remote access service (RAS) you are attempting to authenticate with.
Good password dictionaries are streamlined and fee of duplication. Kali has one at
/usr/share/wordlists (taken from a large data breach). Also a small but useful one at
/usr/share/john/password.lst.
If you don’t have a list of usernames, but did manage to get email addresses, medusa can create a
list of potential usernames from this. Checking in the manual, but couldn’t see how.
Once this is done, you can use the list to brute force your way into the remote access service.
-h = host
-u = username (U = path to username list)
-p = password (P = path to password list)
-M = service to attack
-n = non standard TCP Port number (found in “man medusa” – also some options for parallelisation
which may be default in hydra)
When it finds a working password, the system will stop and tell you. Your pen test may be complete
at this point (depending on the scope).
J J v1.1
18/01/2016
Metasploit
This is a beast, with whole books on how to use it. It can add users, open backdoors, install software
onto a target machine.
Cmd: msfconsole
Cmd: msfupdate
To use Metasploit properly, need to match a known vulnerability with an exploit. High or critical
often allow root access / remote code execution.
1. Take the vuln code from nessus/nmap and use the search function for a matching exploit
Cmd :searchMS08-067
4. Select a payload (review docs for info – in this case, it will install VNC remote control and
have it connect back to us). Remember VNC needs a GUI based OS. See next page for
meterpreter payloads.
5. Show options again and set the rest. Make sure they are all set!!! In this case we have 2
options that need filling
7. Exploit it!
Cmd: exploit
J J v1.1
18/01/2016
More realistic payloads for Metasploit p95. Bind payloads send an exploit and make a connection to
the target machine. Reverse payloads send the exploit, then have the remote machine connect back.
Meterpreter is a powerful shell. It runs in memory so stealthy. Will try to gain admin access, but will
be restricted if the service exploited does not have privileges.
Meterpreter commands
‘migrate’ - migrate the meterpreter to another process e.g. vschost.exe to escape it being shut down
by a reboot.
‘hashdump’ – get password hashes from the computer. Hashdump is important for getting better
privileges on that machine. This means if your first login had little privileges, all is not lost – you
might still get admin access yet. Once you have that admin password, it could well be the same for
other accounts!
Upload, edit, kill to stop a process (e.g. antivirus), + standard terms e.g. cd, mkdir. Can interact with
a ruby shell.
J J v1.1
18/01/2016
In windows, C:\Windows\System32\Config, there is a SAM file. This contains all the usernames and
password hashes. This is encrypted. It is also locked on boot of the system.
Need to boot Kali or a similar OS from CD or Pen Drive. Might need UNetBootin
(https://unetbootin.github.io/). You may also need to change the boot order first.
Once you’re up and running. Mount the drive. You may need to run command ‘fdisk -l’ to work out
which to mount. May need to create the mount point with command ‘mkdirmnt/sdal’.
Cmd: cd /mnt/sdal/Windows/system32/config
Then use Samdump2 to extract the hashes and make sure you have a local copy…
Then need to email the hashex.txt file or find another way of getting it to yourself as the USB will not
be persistent!
Can then set about cracking the passwords, either with a good dictionary, or a brute force cracker
(aaa, aab, aac…).
OS X/ Linux Notes: passwords held in a ‘shadow’ file located at /etc/shadow. Only privileged users
can access it, but /etc/passwd may have a list of old passwords which we can get to and then make
educated guesses about the new password.
Usually done once you have gained access to a machine. Get the hashes by using command
‘hashdump’ from a meterpreter shell. These can usually be copied and pasted from the terminal.
One of the key features to doing this is that we can get privilege escalation. If your lowly user is
lacking rights, the hashdump may give you details of other users with more rights.
Effective, but the user will definitely know you’ve been there. There’s no way to restore a SAM file.
Mount Kali as before, but instead of samdump, run
-i = run interactively so you can choose the user you’d like to reset.
Might need to run ‘fdisk –l’ to check correct disk drive.
Follow the chntpw instructions. With a blank password, you’ll gain easy access. Will need to issue
‘reboot’ command first.
Use Wireshark, Dsniff and macof p113. Ettercap may also be handy.
Macof can be used to flood switches with fake DNS entries to force fail open switches to broadcast
all information to all computers on the network. If switches are fail closed, with will effectively DOS
them.
e.g.
Wireshark is sweet for viewing network traffic. Can restrict down to network cards and types p114.
J J v1.1
18/01/2016
Armitage will bombard the target with every known exploit it has, even when it’s gotten in! Get an
easy to use GUI.
Cmd: armitage
Screens with lightning bolts will appear. Click on them to get a shell. Can now upload programs to
the target and/or interact with it.
J J v1.1
18/01/2016
Social Engineering
Aim
Cmd: se-toolkit
Web Exploitation
-h = target IP
-p = ports (if missing, just scans 80).
-o = filepath to save results.
Easily identifies most web based vulns e.g. SQL Injection, XSS, file includes, CORS. Has a handy GUI
Cmd: w3af
When scanning ensure check boxes from error and info are not on. We just want vulnerabilities.
If it finds something good, you may be able to exploit this from the exploits tab
Webscarab(Full Spidering)
Cmd: webscarab
In the GUI go to Tools > Use full featured interface. Restart the tool.
Certificates are the best way to defend man in the middle attacks like this.
Now in the webscarab GUI, you will have options such as ‘spider tree’ (right click), which you can use
to look for info not readily available, hopefully confidential. May be able to change the requests
using the ‘intercept requests’ and ‘intercept responses’ check boxes.
J J v1.1
18/01/2016
***(p160) Better for intercepting attacks is ZAP (Zed Attack Proxy). Configure the proxy as above,
but on port 8080.
Cmd: zap
This will really nicely intercept web traffic and allow you to edit the content sent (most importantly)
and received. Can choose the direction of the traffic you want to stop and tamper with. Some good
tests:
Can right click on a request and do “spider site” for a larger attack surface (e.g. hidden, less well
validated, content)
Can also do vulnerability scans, where it will show you the weak pages for you to attack. This can be
passive (it just looks at the responses you get), or active, where it sends lots of malicious requests to
work out vulns for you to attack. Rapid requests are likely to raise alarm bells at the website’s end.
Results are in the ‘Alerts’ tab.
Injection attacks
Not much new here, though can be quite effective against PHP services. May be able to hack using
examples
‘searchterm’ or 1 =1 –
‘or’ 1 = 1 –
For searches and logins. If you can get this to work on a username, you might even end up with the
admin account!
Hard to do now. Basically try some javascript in input boxes. E.g. “<script>alert(‘ok’)</script>”.
Reflected XSS is returned to the user immediately. Stored XSS goes into the DB for execution at a
later point.
J J v1.1
18/01/2016
Aim
The aim is to be able to get back into an exploited machine even after reboots and shutdowns.
2. Once netcat has been uploaded, pick a port number, bind the cmd.exe and start netcat in
server (listener) mode.
3. Add netcat to the registry to run on startup need to look this up, but could be like.
nctarget_ip 5777
Rootkits
P174. Like HackerDefender. Works at a lower level than the OS, so can hide netcat and other
programs completely. Be careful if using these as they can be very dodgy! Amazingly powerful
though. Can use to keep back doors open, escalate privileges etc.
Never use a rootkit without specific permission from the client and client and specific permission.
Most clients will not allow rootkits and you might well be sacked for using one.
J J v1.1
18/01/2016
Report
The most interesting bit as far as the client is concerned. Make it good for more work!
Remember to send it encrypted with something likeTrueCrypt. They may need help with this. If
they’re dinosaurs and want a paper copy, make sure it’s signed for. An info leak at this point could
be terrible for the company and your reputation.
Obviously, if a company asks for a sample report, never provide a redacted copy. Send them
something completely made up (but based on a true scenario)!
Executive Summary
2 pages max. Aimed at managers with no IT skills. General overview and highlight of any major
concerns.
Should reference sections of the detailed report for those needing a more in depth look.
Walkthrough Report
May want to provide a walkthrough report so the client can see the steps you took to compromise
the system. This could be part of the detailed report.
J J v1.1
18/01/2016
Detailed Report
It’s ok to point out you couldn’t access a system if you can demonstrate there is potential and the
system is important.
Mitigations, Solutions and suggestions for fixing usually googleable and are essential for the report.
It’s up to you/ the client whether you provide raw data. May want to sanitize it. Don’t use raw data
in the report. You can reference an appendix or separate report for raw data.
Make sure the raw data doesn’t have any silly ‘dev/hack test’ names/properties.
After Care
Remember the client will have questions, so this needs to be in the budget to help them. They might
not be tech savvy. Need to find the line, but after care will effect whether they recommend you or
not. Maybe have a set amount of time agreed in the contract to start?