""
All times are GMT +1. The time now is 11:40 PM

Welcome to the
Adept Web Community - Adept Webmaster Discussion Forums
 forums!

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
 community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have not received your registration email message, please check your spam box/junk mail.

Get a FREE domain name!


That's right! You can get a free domain name (or renew existing) by just posting at our forums! For every 100th post you make, we will register a new domain for you! Join our community today!

]   Adept Web Community - Adept Webmaster Discussion Forums > Coding Forums > Tutorials & Code Snippets > PHP

Reply
 
Thread Tools Display Modes
  #1  
Old July 17th, 2007, 09:06 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Post Protecting Against CSRF Attacks

Protecting against CSRF attacks

CSRF stands for Cross-Site Request Forgery. These kind of attacks are very dangerous and therefore you should take care of them. CSRF attacks are far more unpopular than XSS attacks and that's the primary reason why so many web applications are vulnerable to CSRF attacks.

Where lies the Trust?


Unlike XSS, CSRF attacks exploit the trust that a site has for a particular user. The site is the target of the attack, and the user is both the victim and an unknowing accomplice.

Because the victim sends the request (not the attacker), it can be very difficult to determine that the request represents CSRF attack. To be more specific, if you haven't taken specific steps to soften the risk of CSRF attacks, your applications are most likely vulnerable.

When developing an application, challenging tasks include authentication, identification, and authorization. You may think or feel your script is totally safe from attackers, but you can still be vulnerable to serious CSRF attacks, because it allows an attacker to bypass traditional safeguards.

Example type of CSRF attack

Let's say John Doe has written a guestbook script. He has administrator control panel which no one else can access. In the control panel, he can delete guestbook messages by simply clicking a "Delete" link as the following shows:

HTML Code:
Message 32 (<a href="delete.php?message_id=32">Delete</a>)<br />
Although we have secure login system, we still have dangerous CSRF vulnerabilities. What do you think if John's guestbook uses cookies and someone put the following link into the guestbook:

HTML Code:
<a href="admin/delete.php?message_id=1">Click to see my website John!!</a>
Well. If John now presses the link, he will be autologged with cookies and he gets the guestbook message 1 deleted. Pretty nasty, isn't it? Think what could happen if Ebay had dangerous CSRF vulnerabilities? Then anyone could anytime make you to buy a $9999 product and if you have luck you may not even notice that!

Use $_POST and $_GET instead of $_REQUEST

One thing that you should always to do is to use $_POST or $_GET instead of $_REQUEST. Using $_REQUEST unnecessarily increases your risk. So never use $_REQUEST as seen sometimes in people's scripts. If you have form field "name" and you are using $_REQUEST, anyone can send the "name" as GET data instead of POST data like you thought, so use $_POST when you need it and $_GET when you need it. Forget the $_REQUEST.

GET requests are easily made by simple anchor link. But don't think that using just POST makes you safe. POST requests can also be forged, so do not consider a strict use of $_POST to be sufficient protection. Even JavaScript can launch POST forgeries.

Protecting against CSRF attacks

There are a few steps you can take to mitigate the risk of CSRF attacks. Minor steps include using POST rather than GET in HTML forms that perform actions, using $_POST instead of $_REQUEST, and requiring verification for critical actions. If you have a form which will delete, modify or other way do something important, it's good to have a "Are you sure" -style confirmation.

The most important thing you can do is to try to force the use of your own forms. If a user sends a request that looks like it is the result of a form submission, doesn't it make sense to be a little suspicious if the user has not recently requested the form?

Here's a sample how to make sure POST has been submitted from YOUR form. We will be using tokens.

csrf_form.php
PHP Code:
<?php
session_start
();
$token md5(uniqid(rand(),true));
$_SESSION["token"] = $token;
echo <<<TEXT
<form method="post" action="csrf_test.php">
<input type="hidden" name="token" value="$token" />
Field: <input type="text" name="field" /><br />
<input type="submit" name="submit" value="Send" />
TEXT;?>
I'll explain the above code to you. In the second line we will start a session. Then we will create totally random token with uniqid() function, and after that we will hash it with md5(). After successful generation of random string we will insert it to our session and also in our form. Now if someone submits data using our form, he will also submit random string with it. We need to compare session token and form token before we take any action after form submission like this:

csrf_test.php
PHP Code:
<?php
session_start
();
if (!isset(
$_POST["submit"]))
{
  if (isset(
$_POST["token"]) && isset($_SESSION["token"]) && $_SESSION["token"] == $_POST["token"])
   echo 
"We used our own form!";
  else
   echo 
"Don't try to cheat, we do not allow CSRF attacks!";
}
else
echo 
"Did you submit anything?";
?>
In the above code we ensure the data IS from our form. And if our form is only accessible through login screen, the data is safe. We have successfully defended against CSRF attacks.

CSRF attacks are very dangerous, and most applications that do not take specific steps to prevent CSRF attacks are vulnerable. Because the requests originate from the victim, it is possible for an attacker to target sites that only the victim can access, such as ones on a local network.

Final thoughs

One thing you can also do. Check the referrer header. If it is not present, or if it does not show the correct URL as the referrer, reject the submission. This has the advantage of being simple and sane, but the disadvantage that users who have told their browsers to omit the referrer header (out of concern for privacy) or lie about the referrer will have trouble. This strategy doesn't work if the form uses GET and the page can contain user-generated content with links.

If you use a token in all of your forms as I have suggested, you can eliminate CSRF attacks from your list of concerns.

If you liked this tutorial, please feel free to register at our forums or donate to keep quality tutorials coming. :B
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #2  
Old July 21st, 2007, 05:27 PM
agent777 agent777 is offline
Senior Member
 
Join Date: Jul 2007
Posts: 66
agent777 is on a distinguished road
Default

thanx a lot mr.kai sellgren now i can protect from CSRF Attacks..
how can i do this ?
Reply With Quote
  #3  
Old July 21st, 2007, 06:00 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Quote:
Originally Posted by agent777 View Post
thanx a lot mr.kai sellgren now i can protect from CSRF Attacks..
how can i do this ?
Please read the tutorial.
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #4  
Old June 23rd, 2008, 07:37 AM
aaaawow aaaawow is offline
Newbie
 
Join Date: Jun 2008
Posts: 0
aaaawow is on a distinguished road
Default

world of warcraft power leveling wow power leveling power leveling runescape gold rs2 gold wow gold 中国福利彩票 直流电源 安检门 福彩3d 程控交换机 松下程控交换机 google排名 google左侧排名 oil painting

吹膜机 点火开关 环保空调 runescape money rs2 money dofus kamas thermoforming Equipment 印刷机械 bag making machine 工业设计 锻件 液压机 涂布机 分切机 粉末冶金 packing machine plastic machine


power leveling wow power leveling 香炉 Thermoforming Machine 包装机械 液压机 铝型材 活塞 激光礼品 鞋业 环保空调 吹膜机 汽摩塑料配件 塑料酒瓶包装 塑料件喷漆 google排名


tungsten carbide tungsten plate tungsten electrode tungsten wire tungsten alloy tungsten rod tungsten product molybdenum sheet molybdenum product molybdenum wire molybdenum rod thermoforming machine thermoforming Equipment Plastic Machinery Plastic Thermoforming Machine Plastic Thermoforming Machinery Plastic Sheet Unit,Plastic Extruding Machine Plastic Machine prada shoes true religion jeans evisu jeans Ed hardy Gucci shoes Gucci Handbag adidas shoes Ugg Boots nike shoes LV handbags Jordan shoes new era


包装带设备 模切机 压痕机 切纸机 压纹机 上光机 开槽机 V槽机 折盒机 覆膜机 覆面机 气动马达 气动搅拌机 制袋机 手套机 收卷机 吹膜机 连线机 粉碎机 脱水机 搅拌机 造粒机 团粒机 卷绕机 拉丝机 织带机 包覆丝机 圆织机 裁料机 冲口机 下料机 压合机 纸杯机 纸碗机 纸碟机 热成型机 片材机 制杯机 牵引机 压底机 挤出机 冲压机 包装机 贴窗机 涂胶机 信封机 捆扎机 打包机 切袋机 喷码机 刻字机 打标机 标示机 缠绕机 灌装机 封箱机 丝印机 封口机 裹包机 整理机 滚齿机 封面机 包边机 折入机 整平机 冷压机 镂铣机 贴角机 贴膜机 纸巾机 湿巾机 折叠机 充填机 抛光机 装盒机 调头机 折边机 修边机 上光机 压光机 压纹机 压花机 分切机 分条机 涂布机 覆面机 裱纸机 除粉机 糊盒机 打孔机 磨刀机 切割机 钻孔机 胶水机 圆角机 压平机 划线机 纠编机 插边机 淋膜机 切片机 开槽机,V槽机 底封机 上糊机 制袋机
Reply With Quote
  #5  
Old July 1st, 2008, 01:01 AM
blaxus blaxus is offline
Newbie
 
Join Date: Mar 2008
Posts: 0
blaxus is on a distinguished road
Default

idiotic spamming.... anyways.. thanks for all the tutorials but i think this one is a bit too much over thought..

You see, i once downloaded a simple script. But it had its own secure "are you sure" option.. by one simple thing: Javascript!

It uses a simple thing: If you try to delete something no matter what, he's going to pop up javascript and say: Are you shure you want to delete "news item name". and then give option YES or NO.

When i get a javascript pop up i always read it and i find it a very easy script. Therefor i also find it unessecary to use this complicated stuff. I prefer the simple:

Quote:
<a href=\"javascript:administrate('Are you shure you wish to delete this message?','?page=news/delete&id=".$list->id."');\">Delete Message</a>
Simple but effective!

Last edited by blaxus : July 1st, 2008 at 01:04 AM.
Reply With Quote
Sponsored Links
  #6  
Old October 10th, 2008, 12:05 PM
Sir Rogers Sir Rogers is offline
Newbie
 
Join Date: Oct 2008
Posts: 0
Sir Rogers is on a distinguished road
Default

I'm sorry to shatter your dreams, but that "Protection measure" is more of a parlor trick than anything else.

It doesn't really help. Any good hacker will immediately spot this amateur method and your site will be hacked in no time.

To back up my words, here's the code you need to crack that security measure. Name the file crsf_hack.php and place it in the same folder with the other 2 files, you can see it just displays the message that "Our own form" has been used.

csrf_hack.php
Code:
<?php
	session_start();
	$_SESSION["token"] = "hacked";
	echo "<form method='post' action='csrf_test.php'>";
	echo "<input type='hidden' name='token' value='hacked' />";
	echo "Field: <input type='text' name='field' /><br />";
	echo "<input type='submit' name='submit' value='Send' />";
	echo "</form>";
?>
Reply With Quote
  #7  
Old October 15th, 2008, 03:24 PM
lronclawbigun lronclawbigun is offline
Webmaster
 
Join Date: Sep 2008
Posts: 378
lronclawbigun is on a distinguished road
Thousands of game workshops provide the war power leveling service

Recently the CEO of Mythic Entertainment writes his feelings on gold sellers on his personal blog. Because of his disgust at gold sellers, he and his team have been banning these jerks like crazy since WAR launched. God knows when it will be the Warhammer online power leveling.It is known that the majority of MMO operators oppose gold selling within their games, it has proved impossible to stamp out in most - and a silent majority of high-level players of games like World of Warcraft cd key selling services to ease the grind and in-game expense of raiding. The similar situation is to the warhammer power leveling. Some players love the game and but they have limited time and always be treated as green hands, there comes the needs for game power leveling. Thousands of game workshops provide the war power leveling service. And they are thought rob of the operators’ money like the gold sellers. The relations between the game operators and the game service providers are very tense. Maybe soon the power leveling will be the next banning object. cheap wow cd key.
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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



All times are GMT +1. The time now is 11:40 PM.
Style By: vBSkinworks