当前位置: 代码网 > it编程>编程语言>安全编程 > JS和C#分别防注入代码

JS和C#分别防注入代码

2024年05月15日 安全编程 我要评论
如果你的查询语句是select * from admin where username=''"&user&"'' and password=''"&pwd&
如果你的查询语句是select * from admin where username=''''"&user&"'''' and password=''''"&pwd&"''''"
那么,如果我的用户名是:1'''' or ''''1''''=''''1
那么,你的查询语句将会变成:
select * from admin where username=''''1 or ''''1''''=''''1'''' and password=''''"&pwd&"''''"
这样你的查询语句就通过了,从而就可以进入你的管理界面。
所以防范的时候需要对用户的输入进行检查。特别是一些特殊字符,比如单引号,双引号,分号,逗号,冒号,连接号等进行转换或者过滤。
需要过滤的特殊字符及字符串有:
   net user
   xp_cmdshell
   /add
   exec master.dbo.xp_cmdshell
   net localgroup administrators
   select
   count
   asc
   char
   mid
   ''''
   :
   "
   insert
   delete from
   drop table
   update
   truncate
   from
   %
js版的防范sql注入式攻击的两段代码~:
[code start]  
<script language="javascript">
<!--
var url = location-.search;
var re=/^\?(.*)(select%20|insert%20|delete%20from%20|count\(|drop%20table|update%20truncate%20|asc\(|mid\(|char\(|xp_cmdshell|exec%20master|net%20localgroup%20administrators|\"|:|net%20user|\''''|%20or%20)(.*)$/gi;
var e = re.test(url);
if(e) {
alert("地址中含有非法字符~");
location-href="error.asp";
}
//-->
<script>
登陆和密码输入判断
//防止非法字符串注入
function checkuseravoid(str){
var inj_str="‘|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
var sarray=new array();
sarray=inj_str.split('|');
for (var i=0 ;i <inj_stra.length ;i++ ) {
if (str.indexof(inj_stra)>=0)
return true;
}
return false;
}
[code end]
asp版的防范sql注入式攻击代码~:
[code start]
<%
on error resume next
dim strtemp
if lcase(request.servervariables("https")) = "off" then
strtemp = "http://"
else
strtemp = "https://"
end if
strtemp = strtemp & request.servervariables("server_name")
if request.servervariables("server_port") <> 80 then strtemp = strtemp & ":" & request.servervariables("server_port")
strtemp = strtemp & request.servervariables("url")
if trim(request.querystring) <> "" then strtemp = strtemp & "?" & trim(request.querystring)
strtemp = lcase(strtemp)
if instr(strtemp,"select%20") or instr(strtemp,"insert%20") or instr(strtemp,"delete%20from") or instr(strtemp,"count(") or instr(strtemp,"drop%20table") or instr(strtemp,"update%20") or instr(strtemp,"truncate%20") or instr(strtemp,"asc(") or instr(strtemp,"mid(") or instr(strtemp,"char(") or instr(strtemp,"xp_cmdshell") or instr(strtemp,"exec%20master") or instr(strtemp,"net%20localgroup%20administrators") or instr(strtemp,":") or instr(strtemp,"net%20user") or instr(strtemp,"''''") or instr(strtemp,"%20or%20") then
response.write "<script language=''''javascript''''>"
response.write "alert(''''非法地址!!'''');"
response.write "location-href=''''error.asp'''';"
response.write "<script>"
end if
%>
[code end]
c# 检查字符串,防sql注入攻击
这个例子里暂定为=号和''''号
bool checkparams(params object[] args)
{
string[] lawlesses={"=","''''"};
if(lawlesses==null||lawlesses.length<=0)return true;
//构造正则表达式,例:lawlesses是=号和''''号,则正则表达式为 .*[=}''''].* (正则表达式相关内容请见msdn)
//另外,由于我是想做通用而且容易修改的函数,所以多了一步由字符数组到正则表达式,实际使用中,直接写正则表达式亦可;
string str_regex=".*[";
for(int i=0;i< lawlesses.length-1;i++)
str_regex+=lawlesses+"|";
str_regex+=lawlesses[lawlesses.length-1]+"].*";
//
foreach(object arg in args)
{
if(arg is string)//如果是字符串,直接检查
{
if(regex.matches(arg.tostring(),str_regex).count>0)
return false;
}
else if(arg is icollection)//如果是一个集合,则检查集合内元素是否字符串,是字符串,就进行检查
{
foreach(object obj in (icollection)arg)
{
if(obj is string)
{
if(regex.matches(obj.tostring(),str_regex).count>0)
return false;
}
}
}
}
return true;
(0)

相关文章:

  • Mac下使用mitmproxy抓包HTTPS数据方法详解

    在mac上常用的抓包软件是charles,网上关于charles的教程很多,这里介绍另一个抓包神器mitmproxy。mitmproxy是一款可交互式的命令行抓包工具,它除了可以抓…

    2024年05月15日 编程语言
  • 10个好用的Web日志安全分析工具推荐小结

    经常听到有朋友问,有没有比较好用的web日志安全分析工具?首先,我们应该清楚,日志文件不但可以帮助我们溯源,找到入侵者攻击路径,而且在平常的运维中,日志也可以反应出很多的安全攻击行…

    2024年05月15日 编程语言
  • WEB前端常见受攻击方式及解决办法总结

    WEB前端常见受攻击方式及解决办法总结

    一个网站建立以后,如果不注意安全方面的问题,很容易被人攻击,下面就讨论一下几种漏洞情况和防止攻击的办法。一、sql注入  所谓sql注入,就是通过把sql命令插... [阅读全文]
  • 详解常见web攻击手段

    web攻击在互联网中,攻击手段数不胜数,我们平时不能以自己只是普通的开发程序员而不是安全方向的开发者为理由,而不去掌握基本的 web 攻击手段!我们来熟悉一下有哪几种常见的 web…

    2024年05月15日 编程语言
  • 案例讲解WEB 漏洞-文件操作之文件下载读取

    原理产生:任意语言代码下载函数文件下载(一些网站由于业务需求,往往需要提供文件查看或文件下载功能,但若对用户查看或下载的文件不做限制,则恶意用户就能够查看或下载任意敏感文件,这就是…

    2024年05月15日 编程语言
  • CSRF跨站请求伪造漏洞分析与防御

    CSRF跨站请求伪造漏洞分析与防御

    csrf现在的网站都有利用csrf令牌来防止csrf,就是在请求包的字段加一个csrf的值,防止csrf,要想利用该漏洞,要和xss组合起来,利用xss获得该c... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com