Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Mar 15, 2012 4:58 pm 
Offline
Senior Newbie

Joined: Thu Feb 23, 2012 10:03 am
Posts: 14
Website: http://clayfreeman.com/
Location: Paragould, AR
We wrote this post to describe how to remove some nasty htaccess exploits from your server.

http://irishninjasdesign.com/content/ho ... our-server


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 5:43 pm 
Offline
Senior Member
User avatar

Joined: Sun Aug 10, 2008 11:26 am
Posts: 104
Location: ~$
The "findinfections.php" script you posted has some problems. You should escape the argument to `cat`, otherwise you are open to an injection attack. What if filenames have spaces? Or what if attackers create a file somewhere in your www root called '; rm -rf *; '? Also, why not fread() the file, or better yet, just a recursive grep instead?

Also, if you're distributing a PHP script of this nature, you can get the command line arguments with $argv so users don't have to edit the script.

Any indication of how the server was exploited in the first place?

_________________
Hartford makes bread and jeans, and paper art.


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 5:56 pm 
Offline
Senior Newbie

Joined: Thu Feb 23, 2012 10:03 am
Posts: 14
Website: http://clayfreeman.com/
Location: Paragould, AR
funkytastic wrote:
The "findinfections.php" script you posted has some problems. You should escape the argument to `cat`, otherwise you are open to an injection attack. What if filenames have spaces? Or what if attackers create a file somewhere in your www root called '; rm -rf *; '? Also, why not fread() the file, or better yet, just a recursive grep instead?

Also, if you're distributing a PHP script of this nature, you can get the command line arguments with $argv so users don't have to edit the script.

Any indication of how the server was exploited in the first place?


We have no idea how the server was exploited. We think it was most likely an injection attack though.


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 8:02 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
funkytastic wrote:
The "findinfections.php" script you posted has some problems.

I don't mean to sound rude, but if that script is representative of the quality of PHP scripts on OP's server, no wonder it got infected. Seriously, using shell_exec to cat a file? What happened to good old file_get_contents?

Here's a shell version:
Code:
grep -lir "htaccess" *
grep -lir "unlink" *
grep -lir "fwrite" *

Whatever file appears in the output of all three commands is the one you're looking for. If you have a lot of files, save the output of each command to a file or variable, and use the "comm" command or some PHP function to find out which file appears in all three.


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 8:27 pm 
Offline
Senior Newbie

Joined: Thu Feb 23, 2012 10:03 am
Posts: 14
Website: http://clayfreeman.com/
Location: Paragould, AR
hybinet wrote:
funkytastic wrote:
The "findinfections.php" script you posted has some problems.

I don't mean to sound rude, but if that script is representative of the quality of PHP scripts on OP's server, no wonder it got infected. Seriously, using shell_exec to cat a file? What happened to good old file_get_contents?

Here's a shell version:
Code:
grep -lir "htaccess" *
grep -lir "unlink" *
grep -lir "fwrite" *

Whatever file appears in the output of all three commands is the one you're looking for. If you have a lot of files, save the output of each command to a file or variable, and use the "comm" command to find out which file appears in all three.

I don't take any offense to what you are saying, but at the time, we were about to quit, and on top of that, it was late. Our other code isn't this bad. This was just an attempt to solve the problem one last time before we went home and gave up. I knew there were many more secure ways to do this, but in our situation, it wasn't required. This is not public, so for us, it doesn't affect security. I appreciate your concerns, and the file is being updated to respect the fact that it is exploitable.


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 8:35 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
clayfreeman wrote:
I don't take any offense to what you are saying, but at the time, we were about to quit, and on top of that, it was late. Our other code isn't this bad. This was just an attempt to solve the problem one last time before we went home and gave up. I knew there were many more secure ways to do this, but in our situation, it wasn't required. This is not public, so for us, it doesn't affect security. I appreciate your concerns, and the file is being updated to respect the fact that it is exploitable.

I understand. You've gotta do whatever works if you're getting attacked and you don't have time. But when you post a script on a public web site, please remember that other people will download it and run it in the most stupid manner possible. The world would be a better place without all those insecure PHP code snippets littering the blogosphere.

Just for fun, here's a one-liner (in bash) that improves upon the code I posted above:
Code:
grep -li "htaccess" $(grep -li "unlink" $(grep -lir "fwrite" *))

Just go to /var/www and run it in a bash shell!


Top
   
 Post subject:
PostPosted: Thu Mar 15, 2012 8:46 pm 
Offline
Senior Newbie

Joined: Thu Feb 23, 2012 10:03 am
Posts: 14
Website: http://clayfreeman.com/
Location: Paragould, AR
hybinet wrote:
clayfreeman wrote:
I don't take any offense to what you are saying, but at the time, we were about to quit, and on top of that, it was late. Our other code isn't this bad. This was just an attempt to solve the problem one last time before we went home and gave up. I knew there were many more secure ways to do this, but in our situation, it wasn't required. This is not public, so for us, it doesn't affect security. I appreciate your concerns, and the file is being updated to respect the fact that it is exploitable.

I understand. You've gotta do whatever works if you're getting attacked and you don't have time. But when you post a script on a public web site, please remember that other people will download it and run it in the most stupid manner possible. The world would be a better place without all those insecure PHP code snippets littering the blogosphere.

Just for fun, here's a one-liner (in bash) that improves upon the code I posted above:
Code:
grep -li "htaccess" $(grep -li "unlink" $(grep -lir "fwrite" *))

Just go to /var/www and run it in a bash shell!

Thanks for the shell command! I will post that to the site instead. I appreciate your help and concerns!


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
RSS

Powered by phpBB® Forum Software © phpBB Group