Find r57 and c99 Shells Hidden Inside PHP and TXT Files

Posted 1529 days ago - Security

When malicious intruders compromise a web server, there's an excellent chance a famous Russian PHP script, r57shell, will follow. The r57shell PHP script gives the intruder a number of capabilities, including, but not limited to: downloading files, uploading files, creating backdoors, setting up a spam relay, forging email, bouncing a connection to decrease the risk of being caught, and even taking control of SQL databases. All these functions become readily available through an easy to use web interface, but now you can fight back.

A Turkish member on a forum I participate in released this nifty little bash command, but first, make sure you execute updatedb so find has an up to date image to search:

find /var/www/  -name "*".php  -type f -print0  | xargs -0 grep r57 | uniq -c  | sort -u  | cut -d":" -f1  | awk '{print "rm -rf " $2}' | uniq

You can also search regular text (.txt) files:

find /var/www/  -name "*".txt  -type f -print0  | xargs -0 grep r57 | uniq -c  | sort -u  | cut -d":" -f1  | awk '{print "rm -rf " $2}' | uniq

Or even cleverly disguised GIF image files:

find /var/www/  -name "*".gif  -type f -print0  | xargs -0 grep r57 | uniq -c  | sort -u  | cut -d":" -f1  | awk '{print "rm -rf " $2}' | uniq

The command might appear scary, or even malicious to an inexperienced Linux admin, but here's the break down.

find /var/www/

find is a must know command when dealing with Linux. Find is what's used to perform command line file searches. The path /var/www is the directory find will search, in addition to all directories contained within www, but nothing above. For example, /var/mail is not searched. If your publicly accessible files are not contained in /var/www, then you'll obviously need to replace /var/www with the correct path.

-name "*".php  -type f -print0

This portion of the command tells find to search file names (not directories) ending in .php. Anything else is ignored.

| xargs -0 grep r57

The pipe symbol ( | ) tells Linux to take the results of the first command (the PHP files we searched for), and pass them along to the second command, xargs. At this point, all located files are searched for any mention of r57, not just the file names, but the actual content within the files.

| uniq -c  | sort -u

uniq will prevent duplicate results from displaying. The command is smart enough to know when multiple instances are found in a single file, resulting in a single mention instead of potentially hundreds, flooding your console with repeated messages. The -c parameter tells uniq to count the number of consecutive lines that were combined. sort will take the unordered results, and display them in some type of orderly fashion.

| cut -d":" -f1

cut will prevent the line of code that contains r57 from showing up in the results. The output is just a simple mention of the filename or names, and how many occurrences. There's no need to display the actual code if your intentions are to remove the malicious files.

| awk '{print "rm -rf " $2}'

awk, a programming language in itself, is a very powerful command with many beneficial uses. In this command, awk is instructed to print rm -rf with the file path and file name appended. Here's an example output:

rm -rf /var/www/users/domain.com/images/uploads/r57shell.php

rm -rf is used to delete files without asking questions. The, "are you sure you want to delete ..." is skipped, so be careful when using the -rf switch, it's very destructive if used without care. Notice the print portion - this means the command is only printed, not carried out. Once you've confirmed all the found files are malicious, you can easily dumb the results into a file, make the file executable, and delete the plague in one shot instead of manually deleting individual files one by one.

Another popular tool is the c99shell, which I also recommend searching for. Just change three characters:

find /var/www/  -name "*".php  -type f -print0  | xargs -0 grep c99 | uniq -c  | sort -u  | cut -d":" -f1  | awk '{print "rm -rf " $2}' | uniq

If you're interested in seeing an example of the c99shell interface, here's a rooted site:

http://www.iett.gov.tr/en/kitap/

[tags]Linux, PHP, Security, Debian, Unix, exploit, shell, root, bash[/tags]

Word Count: 982

Tags: , , , ,

Click Here to Submit a Comment

Permalink / Last Modified:

Support Nullamatix.com:

See Also:

  • 04/11/2010 -- Howto: XCache in a Lighttpd Chroot on Debian
    Excerpt: "Whether you're pressed for resources on a virtual/dedicated server, or simply looking for ways to improve web application performance, XCache is guaranteed to produce the desired result. Within minutes of installing XCache: page load times were cut in half, ..."
  • 12/10/2009 -- 529 Attacks in 9 Days: id1.txt, RFI, & More
    Excerpt: "Long time Nullamatix readers know how much I love reviewing log files. Logs can provide detailed incite into not only the overall health of a system, but information one can use to mitigate the risks of automated attacks. In this post, I'll go over a couple ..."
  • 08/14/2009 -- Howto: Tail Lighttpd Logs with Style using Sed
    Excerpt: "Although extremely useful, http server access logs are a mess in their raw form. If you're interested in watching your http server logs in real time with formatting rules and pretty colors, this post is for you. Teh Scr1pt Create a new file to put the l33t ..."
  • 05/26/2009 -- 2HOST $5 512MB Ram Xen VPS (LowEndBox)
    Excerpt: "I woke up this morning and started reading through Google Reader to see what appears to be an amazing offer posted on LowEndBox.com. The post claims a company called 2HOST is offering Xen based virtual private servers at $5/mo. How could anyone resist looking ..."

21 (Comments|Trackbacks)

[ RSS feed | Trackback URI | Leave a Comment ]

collapse Charles # @ 2007-12-20 22:14:14

Awesome post, thanks for breaking down the command - this is highly useful - bookmarked and saved locally =)

 
collapse Guy Patterson # @ 2007-12-21 07:17:35

You're very welcome :) Linux FTW!

Thank you for taking the time to comment. Was your server/machine compromised?

 
collapse Ed # @ 2008-05-25 19:20:30

This looks like a real ‘leet’ linux command but it misses
the point. Why would a hacker keep the name of c99.php or r57.php? Of course he wouldn’t! If you believe your server is compromised by this type of attack then you probably first need to look for files owned by the webserver (ie. apache, httpd). Also note that if a hacker has the ability to run commands then shells/tools could
be uploaded to many other locations and he could have gained root by using further exploits.

Ed

collapse Guy Patterson # @ 2009-04-23 07:09:39

Ed,

Good point, but you'd be surprised. Most intrusions are by complete amateurs or "script kiddies" that don't have a true grasp of what it is they're doing.

If you're compromised by a competent intruder, chances are, you wouldn't want to reply on just this method..

-Guy

 
 
collapse Victim # @ 2008-06-30 12:03:29 Subscribed Via Email

Yeah. I would also have guessed that a hacker would have changed the name. But not in this case. I found a c99.php and a r57.php in my main directory. I have downloaded and deleted them.
My index.html file was also replaced by another file...

I'm not sure if this is the right place to beg for some insight informaiton. But you guys seem to have a good understanding about this problem.

I really don't have that many skills in linux nor hacking. But the thing I don't understand is how the person managed to get the files there... The person could not have had any permission to write to that directory...
The only way I can see it got there, is that he/she exploited some script on the server. Or is there another hole that he can have used ?

collapse Guy Patterson # @ 2009-04-23 07:36:52

Some will argue there is a 7 step process life-cycle for dealing with such events. Here they are, in order:

Preparation
Detection
Containment
Analysis
Eradication
Recovery
Follow-up (Lessons Learned)

Phase 1 involves securing your environment. Phase 2 is detecting the intrusion, Phase 3 is containing it. The fourth phase (Analysis) is where you should find how the attack got in which should reveal how to mitigate the risk for future attacks. Examine server logs around the estimated date of infection, you'll find some good stuff :)

 
 
collapse Dave # @ 2008-07-08 02:07:39

I have to agree with Ed's comment. We were compromised recently with bad files scattered over the server. They were stored with unusual file types and names to make them hard to find.

Because the injection is through a poor URL, the initial scripts run as user 'apache'. Therefore using the find command to search for anything that belongs to user apache, and which has been created, modified, and/or accessed in the days since you think the exploits began, is a useful command you can used in addition to the ones above in the article.

collapse Guy Patterson # @ 2009-04-23 07:38:18

Yes, find with a time/data parameter is a great method to find these nasties, especially if the attack took the time to conceal his or her weak tools..

 
 
collapse Andrew # @ 2008-11-06 23:07:54 Subscribed Via Email

This is a great command to know, but I would never have it rm -rf any files automatically. It found several legit files from a WHM billing script with encrypted php files. All the file needs is 'r57' anywhere, and it would be permanently deleted - not good.

Other than that, great command reference, I will be using it often. Maybe a tutorial on creating a script with several string searches and a cron entry?

collapse Guy Patterson # @ 2009-04-23 07:40:14

Yea, you can always exclude the -rf to get a prompt for each discovered file.

I like your script suggestion, perhaps that will come in a follow up post? :)

 
 
collapse Graham # @ 2008-11-07 04:35:16

This is a good script, it looks for content not file name so it does do the job - however it tends to delete whmcs files in the process - so be careful about that.

collapse Guy Patterson # @ 2009-04-23 07:40:33

Will do - thanks for the heads up!

 
 
collapse Mary c99.php fighter # @ 2008-11-09 08:05:42 Subscribed Via Email

Regardless, this post was very helpful. Probably best is to follow this post, with its excellent command break-down, and then go further. I'm just wondering *how* to go further. What I'm thinking is, wonder if it is possible to get hold of one of the c99.php files, dissect it and then search for commands in your server? The "apache" owner idea also quite good.

Thanks all for this great great help.

 
collapse segun # @ 2009-03-17 18:13:08

i want c99shell in my box

 
collapse darko # @ 2009-03-24 09:45:22

Yes, to use "locate" you will have to refresh database first. On the other hand, "find" will be slower, but it will search on-line. "find" with "-exec" switch can delete files without piping to 'awk' ... Here is example how to delete "bad" PHP file with the find command:

find /var/www/ -name "r59*".php -type f -exec rm {} \;

You can first run only:

find /var/www/ -name "r59*".php -type f -print

to see what files will be deleted, and then put -exec flag. Hope this tip will help. Bye!

 
collapse Joe # @ 2009-04-15 23:58:23 Subscribed Via Email

Hi,

what if r77 or c99 scripts are encrpted format . how to scan or search?

 
collapse AEYTR # @ 2009-06-23 04:56:51

find / -name "*.php" -type f -size +10000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

This code will find +100k php files.

 
collapse Asdrubal # @ 2009-08-09 12:19:26 Subscribed Via Email

Maybe using egrep linux command searching for common words that are writen into r57 and c99 txt files i.e. egrep -ir "expression" server_root
egrep is a powerful toll that allows you to search inside any file including jpg, gif, etc...
You can download r57 and c99 and other files to use in the search at localroot.net and remember: don´t do to the others what you wouldn't like to yourself.

 
collapse AstraSkyraider # @ 2009-09-22 10:57:30

This will of course through a wobbly and display false info on php encoded files such as those protected with ioncube, several chars with the encoded file will contain"r57"

e.g. 089tgFR£4eddSEt r57 9Wfkdlssr r57 dWQQWs

spaced above to show

 
collapse serial # @ 2009-11-26 11:53:49 Subscribed Via Email

"but first, make sure you execute updatedb so find has an up to date image to search"

find dont use updatedb ...

 
collapse iRick # @ 2010-11-29 20:23:28 Subscribed Via Email

Be careful searching for 'c99' as this is often used legitimately as part of HTML colors (e.g. #cc9999). Bottom line is to check the context before deleting.

 

Leave a Comment

Comments are moderated prior to showing up. If your comment does not show up immediately, please do not attempt to resubmit. If you're redirected to the original post after pressing "Add Comment", your comment was successfully entered into the moderation queue.

Trackback Responses to This Post:

  • No Trackbacks, yet. Help Nullamatix.com by Linking to This Post.