Weakness in Perl CGI causes memory dump ??


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Weakness in Perl CGI causes memory dump ??
# 1  
Old 08-25-2003
Weakness in Perl CGI causes memory dump ??

I have discovered a curious phenomenon in GCI. I need some advice from someone far more adept. Is this a bug or potential security weakness?

Context: Redhat 8.0 on xx86 (pentium 3) Apache 2.0, Perl 5.80

Background:

I've been trying to harden a web application which accepts user uploaded files. The main part of this is to check the filenames and filetypes for any obvious mischeif. To this end I created a test folder full of nasty files with horrible names and tested these against my code improving it all the time.

Heres a list of some of the evil files:
Code:
total 2004
drwxr-xr-x    2 root     root         4096 Aug 25 05:26 .
drwxrwxrwx    8 root     root         4096 Aug 24 14:53 ..
-rw-r--r--    1 root     root        14612 Jul 15 14:22 46lijh*&()*%&(..jpg
-rw-r--r--    1 root     root        17289 Jul 15 12:50 @@@@ax2.jpg
-rwxrwxrwx    1 apache   apache      40721 Jul  6 11:30 `echo "hacked"`.jpg
-rw-r--r--    1 root     root        70168 Jul 15 12:42 %^^^^^fty^^^%.jpg
-rw-r--r--    1 root     root        27774 Jul 15 12:50 gm######ax3.jpg
-rw-r--r--    1 root     root        14813 Jul 15 12:40 gren%45%647%12%20%39%94ade2.jpg
-rw-r--r--    1 root     root        27397 Jul 15 14:23 hkacr1.gif
-rwxrwxrwx    1 apache   apache     130520 Jul  6 11:30 i%90rm -r -f../../*.jpeg
-rw-r--r--    1 root     root        26386 Jul 15 14:23 ioio{{[[}}]].jpg
-rwxrwxrwx    1 apache   apache      72068 Jul  6 11:30 ```````````````````````.jpg
-rw-r--r--    1 root     root        27728 Jul 15 12:48 ~~~~~.jpg
-rw-r--r--    1 root     root        25079 Jul 15 14:28 ======.jpg
-rw-r--r--    1 root     root        14372 Jul 15 14:18 >>><<<.jpg
-rw-r--r--    1 root     root        12887 Jul 15 14:29 |||||.jpg
-rw-r--r--    1 root     root        52689 Jul 15 12:40 ;cat /etc/passwd           .jpg
-rwxrwxrwx    1 apache   apache      28602 Jul  6 11:30 ,,,,,,.jpg
-rwxrwxrwx    1 apache   apache      36075 Jul  6 11:30 :::::.jpg
-rw-r--r--    1 root     root        12491 Jul 15 14:28 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.jpg
-rw-r--r--    1 root     root        23729 Jul 15 12:51 ?????.jpg
-rw-r--r--    1 root     root        29784 Jul 15 14:19 .jpg
-rw-r--r--    1 root     root        18780 Jul 15 14:28 ....jpg
-rw-r--r--    1 root     root        13547 Jul 15 14:27 '''''''''''''''.jpg
-rw-r--r--    1 root     root        33670 Jul 15 14:23 """""".jpg
-rw-r--r--    1 root     root        44942 Jul 15 12:41 (((((.jpg
-rw-r--r--    1 root     root        13274 Jul 15 12:43 *.jpg
-rw-r--r--    1 root     root        34681 Jul 15 14:19 \';|":\';|";\';|":\';|":.jpg
-rw-r--r--    1 root     root        13085 Jul 15 14:26 \\\\.jpg
-rw-r--r--    1 root     root        19159 Jul 15 12:37 &********.jpg
-rwxrwxrwx    1 apache   apache      77473 Jul  6 11:30 jpg.jpg
-rw-r--r--    1 root     root            0 Aug 25 05:26 list
-rw-r--r--    1 root     root        38004 Jul 15 14:22 }|:"|{LP{LP][;\;']\].jpg-rw-r--r--    1 root     root        34990 Jul 15 12:51 m18good.jpg
-rw-r--r--    1 root     root        21207 Jul 15 12:48 m43stk.jpg
-rw-r--r--    1 root     root        10120 Jul 15 12:50 muchtoolongyesitisfartoolongfortheservermuchmuchmcuhtoolong.jpg
-rw-r--r--    1 root     root        20877 Jul 15 12:37 +++++sdffs.jpg
-rw-r--r--    1 root     root        12924 Jul 15 14:28 >>>>foo.jpg
-rw-r--r--    1 root     root       691361 Jul 15 12:52 <<<<foo.jpg
-rw-r--r--    1 root     root        17910 Jul 15 12:47 s   p    a   c   e   y
 .jpg
-rwxrwxrwx    1 apache   apache      29531 Jul  6 11:30 testgood.jpg
-rw-r--r--    1 root     root        17919 Jul 15 14:25 &&&tfyj.jpg
-rwxrwxrwx    1 apache   apache      62764 Jul  6 11:30 UYGUGUGYUGIGIGYGUBJHBJGJGJKGJHJHGJHGJGJHGJHGJHJKHGJYTYTUIYTUT&YTUTUTUYTU*^&*%*%)**&^*^)*%&^_)*_)*)(*)(.jpg

I admit theres not much method in my madness, most are just random names with as much naughtyness as I can think of. I got the code to handle 99% of all such files but there is ONE that I just cant seem to deal with.

******** Super Evil File ************

-rw-r--r-- 1 root root 33670 Jul 15 14:23 """""".jpg
**************************************

As you can see its nothing special to look at, just 6 quotes, but this seems to choke CGI method using the following line:

$uploadpicname = $query->param('ulpic');

the resulting contents of $uploadpicname always devastate my code, I've even had a core dump as a result, I think the variable ends up full of binary from some memory area because the size of it is huge and consists of many weird and non printable characters.


For what its worth heres the Perl code for sanitising the filenames. I would welcome any advice or oppinions on how to tighten this up more, but I do beleive its CGI not my code thats the problem. Forgive the style im a C programmer
Smilie

Code:
sub upload
{
if ($uploadpicname eq '')				# check if empty
	{
	errorgivename();
	printhtmlfooter();
	exit 1;
	}
print "$uploadpicname";
$uploadpicname =~ s/.*[\/\\](.*)/$1/;			# remove any path info
$uploadpicname =~ s/[';|`{}:%^*&()\"]//g;		# kill naughty characters
if ($uploadpicname eq '')
	{
	errorgivename();				# check if all chars were killed
	printhtmlfooter();
	exit 1;
	}
$namelength = length($uploadpicname)-4;			# get length of the filename
$namebody = unpack("A$namelength", $uploadpicname);	# separate name
$extension = unpack("x$namelength A4", $uploadpicname);	# separate extension
if ($extension ne $validextension) 
	{
	erroruploadnotjpg();
	}		
($newname = $namebody) =~ s/[^a-zA-Z0-9]//g;		# remove funny characters in name body
$newname =~ s/[^\w]//g;
$newname = substr($newname, 0, $maxnamelength);		# truncate to sensible length
unless ($newname)					# if theres still nothing left 
	{						# then make up a random name
	$newname = randstring(9);
	}
$newname = $newname.$extension;				# stick the extension back on
$writeto = "$relativeroot$uploaddir/$newname";
if ( -e "$writeto")					# check name not already used
	{
	errorfileexists();
	}
	else
	{
	if ($uploadpasswd eq $groupadminpasswd)		# is user allowed to upload?
		{

.......  upload file etc

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Collecting Shared Memory in core dump

I have an application which crashed while accessing a shared memory. typedef struct { ... } LnxUserData; LnxUserData *ptrLnxUserData; fd = shm_open(shrSegName, O_CREAT|O_RDWR|O_EXCL, 0644); if(fd == -1 && errno == EEXIST) { fd = shm_open(shrSegName, O_CREAT|O_RDWR, 0644); }... (6 Replies)
Discussion started by: rupeshkp728
6 Replies

2. OS X (Apple)

Perl CGI

I am trying to get my MacBook Pro with 10.8 Mt Lion set up to run Perl CGI scripts. Having a problem. I can start Apache Web Server with no problems. Why do I put the static and dynamic scripts? I which directory? I have looked at this article:... (3 Replies)
Discussion started by: djehresmann
3 Replies

3. Shell Programming and Scripting

Cgi to dump xml data from form input field

Hi All, I am trying to write a shell script which takes parse the web form find the input field and dump the data of that field into one xml file. The form looks like, <input type="button" id="btnSave" value="Save" onclick="saveXmlData()"/> <form name="submitForm"... (1 Reply)
Discussion started by: jdp
1 Replies

4. Shell Programming and Scripting

CGI Perl : while loop in CGI perl

Hi Team, I am trying to connect to database(succeeded ) and print the records on the browser using while loop. But the elements of array are not displayed instead while loop is displayed directly. Instead of the below I can embed html statements in print but I am looking for the below style as I... (1 Reply)
Discussion started by: scriptscript
1 Replies

5. Shell Programming and Scripting

Perl CGI : unable to download the excel sheet from perl cgi page

Hi All, I have written an cgi perl script that displays an image(Excel image) and when clicked on that Image I need to download a excel sheet. I made sure that excel sheet exists in the folder with the given name but still I am not able to download the sheet. print "<center><table... (2 Replies)
Discussion started by: scriptscript
2 Replies

6. Shell Programming and Scripting

Perl cgi pages out of cgi-bin folder in WINDOWS

Hi team, I have a typical problem with cgi pages in apache webserver in WINDOWS I am able to execute(display) the pages that are saved in cgi-bin folder. But I am not able to execute the pages stored in htdocs or other folder other than cgi-bin folder. Could anyone please let me know how... (1 Reply)
Discussion started by: scriptscript
1 Replies

7. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

8. UNIX for Dummies Questions & Answers

random Physical memory dump

I am currently running unix open server 6.0 on a Dell Power Edge R720. The system will be running fine when the blue screen appears and states the system has performed a physical memory dump in # of ticks. I reboot the system and all looks well. Running diagnostics does not show anything wrong. The... (1 Reply)
Discussion started by: rformt
1 Replies

9. HP-UX

memory fault(core dump)

i am getting memory fault (core dump) in a C program i want to know which statement execution caused it. i tried following things $ gdb generalised_tapinread_mod HP gdb 5.4.0 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x. Copyright 1986 - 2001 Free Software Foundation, Inc.... (2 Replies)
Discussion started by: junaid.nehvi
2 Replies

10. Shell Programming and Scripting

Perl CGI.pm

my box is FreeBSD4.3 and I use Perl 5.0005_03. Here is the CGI script. test.cgi ...... if ($query->action eq 'detail') { ...... print $query->hidden('action', 'modify'); ...... } I found that the result of test.cgi?action=detail is not what I expected. the script does not... (4 Replies)
Discussion started by: tonyt
4 Replies
Login or Register to Ask a Question