我的网站是否容易被SQL注入?(PHP新手)

Poo*_*ars 1 php mysql sql

我是php新手大约一个月,我决定创建自己的网站.情况:我的mysql服务器将用户存储为md5,密码为md5.以及登录页面上的验证码

我们来看看我的代码吧

<?php
session_destroy();
$usermod=md5($_POST["user"]);
$passmod= md5($_POST["pass"]);
if(file_get_contents("http://www.opencaptcha.com/validate.php?ans=".$_POST['code']."&img=".$_POST['img'])=='pass')
{
$con=mysqli_connect("hidden","hidden","hidden","hidden");
// Check connection
if (mysqli_connect_errno())
 {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result2 = mysqli_query($con,"SELECT * FROM Users
WHERE Username_login='$usermod'");
if($row2 = mysqli_fetch_array($result2))
{
$result = mysqli_query($con,"SELECT * FROM Users
WHERE Username_login='$usermod' AND Password='$passmod'");
if($row = mysqli_fetch_array($result))
{
echo "Thank you for logging in: ".$row['FirstName']." as ".$row['Username'];
session_start();
$_SESSION['user'] = $row['Username'];
$_SESSION['email'] = $row['Email_start']."@".$row['Email_domain'];
$_SESSION['name'] = $row['FirstName']." ".$row['LastName'];
header("Location: http://mspb.tk/login/welcome.php");
}
else
{
header("Location: http://mspb.tk/login/login.php?login=failed");
}}
else
{
header("Location: http://mspb.tk/login/login.php?username=failed");
}
}
else {
header("LOCATION:http://www.mspb.tk/login/login.php?opencaptcha=failed");
}
?>
Run Code Online (Sandbox Code Playgroud)

就是这样,如果有人发现sql注入并告诉我,我会非常高兴:)非常感谢Poom

Gli*_*ire 5

不,它不容易受到SQL注入的影响,但是......

还有许多其他与安全相关的问题需要注意:

  1. 您正在使用MD5存储密码.众所周知,MD5很脆弱且破解成本低廉,你应该使用PHP的password_hash函数.如果由于使用共享主机等而无法使用PHP 5.5,请查看cryptCRYPT_BLOWFISH生成随机盐.
  2. 你没有腌制密码.通过使用salt(为数据库中的密码存储的每个用户记录生成的随机字符串),您可以使破解者更难以打破数据库中的哈希值,因为即使两个相同的密码也会产生不同的哈希值.您还删除了彩虹表攻击向量.如果您使用password_hash,这是为您完成的.
  3. 您似乎使用MD5来存储用户名,没有理由这样做只是为了避免SQL注入,学会使用预准备语句,因为后面的函数(例如搜索)会要求您提交明文参数以便与明文进行比较.
  4. 看起来您刚刚在StackOverflow上转储了主机名和MySQL用户名/密码.我建议您立即更改现场网站上的内容,因为有人可能会在隐藏它们之前复制/粘贴它们.