Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2011, 05:04 PM   PM User | #1
mrkfc
New Coder

 
Join Date: Aug 2010
Posts: 35
Thanks: 6
Thanked 0 Times in 0 Posts
mrkfc is an unknown quantity at this point
Angry Website Security

I run a dynamic PHP/MySQL membership website, and a competitor site has been constantly hacking us. I have a few backups, so thank fully I can restore the site to its normal state. But after I restore it they can still hack it very easily. I have checked through all my code and I cannot find any vulnerabilities. I suspected that they were using XSS, so I installed a script called html purifier. Still they were able to hack into the system. After they had hacked the system they were using the private message facility to send lots of abusive messages out using my username.

Here is some of the session coding:
PHP Code:
<?php
session_start
(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks "";
if (isset(
$_SESSION['id'])) {
    
// Put stored session variables into local php variable
    
$userid $_SESSION['id'];
    
$username $_SESSION['username'];
    
$toplinks '<a href="member_profile.php?id=' $userid '">' $username '</a>  <BR/>
    <a href="member_account.php">Account</a><BR/>
    <a href="logout.php">Log Out</a>'
;
    
$image $_SESSION['username'];
    
}
else {
    
echo
"login please!";
/* Make sure that code below does not get executed when we redirect. */
exit;
    
}
?>
<?php
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql mysql_query("SELECT * FROM members WHERE id='$userid'"); 
while(
$row mysql_fetch_array($sql)){
$country $row["country"];
$state $row["state"];
$city $row["city"];
$team $row["team"];
$avatarid $row["avatarid"];    
$accounttype $row["accounttype"];    
$bio $row["bio"];    
$level $row["level"];
$wages $row["wages"];
}
?>
I think they might be some how modifying the user sessions to impersonate our website staff. Some how they had managed to post over 300 messages onto the forums in under a minute, and I could not trace the ip address of the poster.

Any guidance/help would be really appreciated

Thank you.
mrkfc is offline   Reply With Quote
Old 09-11-2011, 06:40 PM   PM User | #2
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Are you using any security codes such as mysql_real_escape_string, addslashes, strip_slashes, magic quotes, is_numeric, etc?

If you're putting raw data into your database, it can be hacked very easily.
myfayt is offline   Reply With Quote
Old 09-11-2011, 06:46 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
There is no way to gain access to the session data unless they can physically hack into the server itself. Even then they would need to upload and run their own custom php code to scan through all the session files and integrate with your system.

I suspect your login system or one of your forms has some weakpoints. You've shown us the completely wrong thing.

Show the code for your login, registration and any contact forms you have.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-11-2011, 06:48 PM   PM User | #4
perpl3x3d
New to the CF scene

 
Join Date: Sep 2011
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
perpl3x3d is an unknown quantity at this point
I am no security expert, but I have a few pointers.

You are using unencrypted session variables. Yes, the session file is located on the server, not client side like a cookie, BUT those values can still be manipulated. I suggest using a token system and some type of encryption to prevent session hijacking (thats what is sounds like to me.)

I noticed this line specifically:
$sql = mysql_query("SELECT * FROM members WHERE id='$userid'");


while($row = mysql_fetch_array($sql)){
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$team = $row["team"];
$avatarid = $row["avatarid"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
$level = $row["level"];
$wages = $row["wages"];

Are you storing passwords on the table members? Because if so, I'd remove the * from your query, and specifically list which values you need to retrieve.

I hope you are able to lock down your site, good luck!
perpl3x3d is offline   Reply With Quote
Old 09-11-2011, 07:13 PM   PM User | #5
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Also another thing to mention, if your register/login isn't encrypted, like passwords, that is a huge security flaw.

Using MD5, SHA1, and Random SALT would make it quite secure.
myfayt is offline   Reply With Quote
Old 09-11-2011, 07:47 PM   PM User | #6
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Start off simple. Session hijacking is the least likely, as it's the hardest to do. It involves sniffing your traffic etc. etc. and is just unlikely.

The most common is SQL Injection, so I would check that you're validating user input that is being entered into queries, using mysql_real_escape_string().

Also, your file that you're including connect_to_mysql.php. It's possible they might know where that file is, and could easily include that into a script of their own from a different URL dependant on a couple of php configuration settings, so it might be worth moving this above the web root (the folder above public_html or www). That way, they physically can't get access to it, without having a script on your server.

Which leaves XSS. Ensure there's no unvalidated user uploads, or inputs, that point to a file location. Ensure you use something like $_SERVER['DOCUMENT_ROOT'] prefixed to file locations that are user provided. Also validate file uploads, by ensuring file types and disallowing certain types and sizes.

Your actual 'check if logged in' portion isn't great. It's easy to find out a user id, and if someone was able to set their id as a user id, they'd be logged in as that user. I'm not actually sure how easy, or hard, it would be to set a $_SESSION variable like that however. I would suggest rethinking that part, by validating the user's 'last logged in ip' in the table with the current IP, and validate on some sort of token set at login, also stored on login.

Also, like perplexed says, don't retrieve your password through the mysql. It could be sniffed out that way. Limit the fields in the query to the fields you require.

And of course, make sure you're using sha1() for your passwords, to hash them so that no-one can see them in plain text.
BluePanther is offline   Reply With Quote
Old 09-15-2011, 10:40 PM   PM User | #7
mrkfc
New Coder

 
Join Date: Aug 2010
Posts: 35
Thanks: 6
Thanked 0 Times in 0 Posts
mrkfc is an unknown quantity at this point
Sorry for the late reply. Thanks a lot for all your advice I am currently trying my best to code new security and make the site difficult to hack.
mrkfc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:36 AM.


Advertisement
Log in to turn off these ads.