将数据形成数据库:防止SQL注入
最近有人提到,我们通过表单提交将数据插入 SQL 数据库的方法会受到 SQL 注入攻击,因此需要一些建议来加强我们的安全性。
下面是将表单数据插入数据库的代码:
<cfquery name="InsRegistrant" datasource="#application.Datasource#" dbtype="odbc">
INSERT INTO Schedule_Registrations(
schedule_id,
first_name,
last_name,
phone_number,
email,
guest,
list_type,
datetime_registered
)
VALUES(
#url.schedule_id#,
'#FORM.first_name#',
'#FORM.last_name#',
'#CleanPhoneNumber#',
'#FORM.email#',
#attendee.guest#,
<!--- Values for list types
0 = NEVER USE Will cause many many problems
1 = Main List
2 = Waiting List --->
#attendee.list_type#,
#createodbcdatetime(now())#
)
</cfquery>
CleanPhoneNumber 是这样设置的:
<cfset CleanPhoneNumber = REReplace(form.phone_number, "[^0-9]", "", "ALL") />
例如,我被告知要使用,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />
但我不确定要替换什么以及在哪里替换。当我用这样的值替换这些值时,我收到错误。
任何方向都会有帮助..
It has recently been mentioned to be that our method of inserting data into our SQL database via form submission is subject to SQL injection attacks, and want some advice to harden our security.
Here's the code that inserts form data into the DB:
<cfquery name="InsRegistrant" datasource="#application.Datasource#" dbtype="odbc">
INSERT INTO Schedule_Registrations(
schedule_id,
first_name,
last_name,
phone_number,
email,
guest,
list_type,
datetime_registered
)
VALUES(
#url.schedule_id#,
'#FORM.first_name#',
'#FORM.last_name#',
'#CleanPhoneNumber#',
'#FORM.email#',
#attendee.guest#,
<!--- Values for list types
0 = NEVER USE Will cause many many problems
1 = Main List
2 = Waiting List --->
#attendee.list_type#,
#createodbcdatetime(now())#
)
</cfquery>
CleanPhoneNumber is set this way:
<cfset CleanPhoneNumber = REReplace(form.phone_number, "[^0-9]", "", "ALL") />
I've been told to use, for instance,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />
but I'm not sure what to replace and where. When I replace the values with such I get an error.
Any direction would be helpful..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将所有表单和 url 变量包装在
cfqueryparam
中。您的查询将如下所示:
我不确定所有数据类型是否正确,请参阅 所有数据类型的
cfqueryparam
的完整文档。You should wrap all form and url variables in
cfqueryparam
Your query would look like this:
I'm not sure I got all the data types correct, see the full documentation of
cfqueryparam
for all the data types.您可以采取一些好的做法。
对于插入代码,您可以执行的操作之一是在插入数据之前显式检查表单字段的输入。检查空格和“'”等内容。您还希望确保用户不会看到因输入错误数据而导致的错误消息。这对于想要了解表结构的人很有用。
否则,将插入放入存储过程中,并在调用存储过程进行插入或更新之前验证输入参数。
以下列出了您可以采取的措施来防止 SQL 注入攻击。它与 asp.net 相关,但无论您使用哪种语言,这些概念仍然适用。
如何:防止 ASP.NET 中的注入攻击
There are several good practices you can do.
For the insert code you have provided one of the things you can do is explicitly check the input of the Form Fields before inserting the data.Check for things like spaces and "'". You also want to ensure that the user does not see your error messages from bad data entered. This is useful to somebody wanting to know your table structure.
Otherwise place the insert in a stored procedure and validate the input parameters before calling the stored procedure for the insert or update.
Here is a good list of things you can do to prevent SQL injection attacks. It is related to asp.net but the concepts still apply no matter what language you are using.
How To: Protect From Injection Attacks in ASP.NET