views:

104

answers:

5

What ways are there for detecting exploits in PHP/MySQL web applications (checking for certain characters or pieces of codes in the GET, POST, COOKIE arrays / using a library with a database that has all the patterns for common exploits, if any exist?) and how should I proceed when one is detected?

For example, if someone tried to find a SQL injection in my PHP/MySQL web application using the GET request method, should I store the action performed by the user in the database, have the application send me an e-mail, IP ban the user and display him/her a message "Sorry, but we have detected a harmful action from your account that will be reviewed. Your account has been disabled and certain features may be disabled from your IP address. If this is a mistake, please e-mail us with all the details."

Thanks.

+1  A: 

just use strip_tags() on all $_REQUEST and $_COOKIE vars to take care of code showing up in these vars, as for SQL you would have to maybe write up a query-like regex or something, but this shouldnt be an issue as you should always mysql_real_escape_string($str) all variables in your queries. try something like this though.

function look_for_code_and_mail_admin($str) {
    $allowed_tags = "<a>,<br>,<p>";
    if($str != strip_tags($str, $allowed_tags)) {
        $send_email_to = "[email protected]";
        $subject = "some subject";
        $body = "email body";
        mail($send_email_to,$subject,$body);   
    }
    return($str);
}
seventeen
I know how to protect my application from exploits, I just want to catch any users who try to intentionally break it and cause harm. Considering how many different kinds of exploits there are I don't think a single regex will do the job.
TheMagician
Using escaping to defend against SQL injection is really the wrong way; parameterized queries (i.e. the mysqli functions/classes is much safer
Michael Borgwardt
you're right they're both tough to call, especially sql injection. Even the function I just offered would turn up positive if someone put in a < b> tag and didn't know it wasn't allowed.
seventeen
+1  A: 

Your question is twofold and I'll answer the second part.

Log everything but do not ban or display any message. It will be embarrassing in case of a false positive. As a general rule, try to build an application that can deal with any sort of user input without a problem.

cherouvim
A: 

Seems like too much work with the email bit and everything to me. Aside from that, I like to run around on sites I commonly use and try to find injectable points so I can warn the administrator about it. Would you IP ban me for that?

I suggest hunting down the exploitable parts yourself and sanitizing where necessary. Acunetix has a very very good program for that. If you don't feel like shelling out the giant price for Acunetix, there are some very good firefox addons called XSS Me and SQL Inject me you might want to look into.

Rob
How could I distinguish you from a user who is trying to hack into the application and steal every user's information? If I notice someone running exploits in my application I want to stop that user from proceeding if he eventually happens to succeed.
TheMagician
Well, thats the thing, you can't distinguish from me and a malicious user. You also wouldn't be able to distinguish from a malicious user and a normal user just accidentally typing the wrong thing. You'd need to set something so that if they did it more than once or twice, or even five times, THEN it activates the anti-exploit. So I guess if you're really worried about it, then you'd need to set the thing in place, but I still suggest just hunting down the exploits and sanitizing it yourself.
Rob
-1 If you are using secure programming methodologies, you don't need the false security that is Acuntex.
Longpoke
+2  A: 

Three things come to mind:

  1. defensive coding, sanitize all input, prepare sql statements and use Suhosin
  2. increase security of your site by breaking into it with a vulnerability scanner
  3. log hacking attemtps with an Intrusion Detection System

If you feel a full fledged IDS is too much, try PHP IDS, as it does pretty much what you are asking for out of the box. Note that detecting intrusions at the PHP level might already be too late though to prevent an attack.

In case of a successful intrusion, I guess your best bet is taking the server offline and see what damage was done. You might have to consider hiring someone to do a forensic analysis of the machine in case you need to collect legally usable evidence.

If you feel you need to react to unsuccessful intrusion attempts and got the malicious user's IP, find out the ISP and inform him with as much details of the intrusion attempt as possible. Most ISPs have an abuse contact for these cases.

Gordon
A: 

Um, I can't remember the last time I've seen a site that tries to log SQL injection attacks that I wasn't able to penetrate..

You don't need to worry about weather someone is attacking the site, as it is subjective at best as to weather something is an attack or not. What if the site base64 encodes some values and decodes them before it uses it? Your IDS is not going to catch that. What if a user wants to post a snippet of code, it gets detected as an exploit because it contains SQL? This is such a waste of time... If you really need to know if someone's attacking you, just install some IDS on a seperate machine with readonly access to the incoming traffic.. I say seperate machine, because many IDS are vulnerable themselves, and will only make the situation worse.

Use standard secure programming methodologies, use paramaterized SQL queries or an ORM.

Longpoke