tags:

views:

83

answers:

3

hey guys , today i visited my website and i saw someone insert more than 1000 query in my story table

my script is in php/mysql and i have captcha enabled and i wonder how he can do such a thing

a simple form and in another function , it checks $vars and validate them and then insert in database

im sure he is using a bot to do such a thing

im really confused

this is my function to validate and insert into table

    function submitStory($name, $address, $subject, $story, $storyext, $topic, $alanguage,$tags) {

        $subject = check_words(check_html(addslashes($subject), "nohtml"));
        $story = check_words(addslashes($story), "nohtml");
        $storyext = check_words(addslashes($storyext), "nohtml");

        $result = $db->sql_query("INSERT INTO ".$prefix."_stories 
            (sid,catid,aid,title,time,hometext,bodytext,newsref,newsreflink,comments,counter,topic,informant,notes,ihome,alanguage,acomm,hotnews,haspoll,pollID,associated,tags,approved,section)
        VALUES
(NULL, '$catid', '', '$subject', now(), '$story', '$storyext', '','', '0', '0', '$topic', '$name', '', '', '$alanguage', '', '', '0', '0', '','$tag_ids','2','news')");

 mysql_error();


        include ('header.php');
        echo "<font class=\"content\"><b>"._THANKSSUB."</b><br><br>"
        .""._SUBTEXT.""
        ."<br>"._WEHAVESUB." $waiting "._WAITING."";

        include ('footer.php');
    }
+1  A: 

Most likely an SQL injection attack. You should take your site offline immediately and not re-enable it until you have both fixed the vulnerability and checked your entire database carefully for malware such as the Zeus dropper.

If your site is allowing user-generated content, you should also be filtering it carefully for evil HTML tags, javascript etc.

crazyscot
+2  A: 

@Mac,

Although you are adding addslashes to few of the variables, the rest are exposed to sql injection. Please apply mysql_real_escape_string function to all the variables and include the following in the list:

$catid, $topic, $name, $alanguage, $tag_ids

I suggest you strongly to apply mysql_real_escape_string but if you are in a real hurry and want a quick fix to try it out right away without having to wonder what mysql_real_escape_string really is and what it does, then atleast apply addslashes to the variables I mentioned above within your function submitStory.

You can read more about mysql_real_escape_string here

Hope this helps. Let us know.

Devner
wait a moment , i know what that means , i just cut some codes of my function , problem is how they insert those amount of query in my database , so u r saying the problem is only in filtering the $vars ?! im sure bug is somewhere else ! maybe in where condition
Mac Taylor
'where condition' ??? My friend, your query does not even use a WHERE condition. If you are talking about a WHERE condition that you are using in a different function than you listed above, then maybe yes. How they could have done that? That's because you haven't escaped the vars that I have mentioned, which allows them to cleverly craft their own queries and use it instead of the regular input values that you would expect. Someone who knows a little about the schema of your DB or who is good at guesswork, can just guess and try out injecting their codes. Does that make sense?
Devner
my codes are opensource and everyone can see it , hmm i should learn more in security , still im sure there is no problem in my vars becuase i escaped all them and didnt list them in my question . there should be something else i dont know .they did the same with my registration section and inject lots of datas to my database , that section was written by a professional coder .even these parts have captcha system .uh
Mac Taylor
Mac, I understand your pain and I would like to extend my sympathies to you. But at least now you know that it is not totally 100% safe. You can take the necessary precautions now. You mentioned that you are SURE that there is no problem in your vars as you escaped them all but didn't list them. If you can post at least the relevant section which shows how you escaped them, we will be able to help you out with it. Since you are the only one who has looked at the code, others and myself here on SO, have no way to tell if there's something wrong with that code.
Devner
Also to be able to comment on the captcha, even the captcha code needs to be looked at. So I feel it would help you to resolve the issue quicker if you can post the code for the captcha as well.
Devner
A: 
  1. Once someone has gotten by the captcha, can they post any number of stories? (i.e., are the now considered safe for that session). This only proves they are human the first attempt...

  2. Is there a posting limit once someone has registered?

  3. Do you have a generated id for each registered user's session and the form so their credentials can't be used in a XSS?

If 1 is true or either 2 and 3 are false, they can absolutely run a script to spam the db.

Cryophallion
yes they can post any number , days ago i asked how to stop spammers in php and delay in posting but didn't receive a good answer ,
Mac Taylor
Add a timestamp to the $_Session for each user. When posting a story, check the current timestamp vs the session stamp. IF it is less that what you have set (say 45 seconds or so for new stories, 10 seconds for edits), show an error.
Cryophallion