简介:
sql注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过sql语句,实现无帐号登录,甚至篡改数据库。
实例:
方式:
在登录界面,要求输入用户名以及密码,可以按照下面方式实现免账号登录:
在用户名中输入(键入或者粘贴):“'or 1 = 1 -”
密码:“”
分析:
String sql = "select * from user_table where username= ' "+userName+" ' and password=' "+password+" '";
当输入了上面的用户名和密码,上面的sql语句变成:
SELECT * FROM user_table WHERE username= '’or 1 = 1 -- and password='’
分析sql语句:
条件后面username=”or 1=1 用户名等于 ” 或1=1 那么这个条件一定会成功;
然后后面加两个-,这意味着注释,它将后面的语句注释,让他们不起作用,这样语句永远都能正确执行,用户轻易骗过系统,获取合法身份。
这还是比较温柔的,如果是执行
SELECT * FROM user_table WHERE username='' ;DROP DATABASE (DB Name) --' and password=''
那不就是呵呵了呀。
解决方案:
限制特殊字符输入:
Private Sub txtusername_Change() Dim s As String Dim ss As Long Dim l As Long Dim i As Long s = ",。、;’【】·!@#¥%……&*()——+|~《》?:“{}',.;\/:*?""<>|{}[]!@#$%$^&()~`_-+=" '需要禁止的字符都放这里 ss = txtusername.SelStart 'selstart是选中文本的开始位置 For i = 1 To Len(s) 'len(s)求s的长度 l = Len(txtusername.Text) txtusername.Text = Replace(txtusername.Text,Mid(s,i,1),"") 'replace是sql语句里面的替换,把txtusername.text里面的mid(s,i,l)替换为“” 'Mid就是从一个字符串中取子字符串,比如a="aabbcc",我们想取出"bb"就可以用Mid("aabbcc",3,2) Next txtusername.SelStart = ss End Sub
禁止右键:
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long,ByVal lpString As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long,ByVal hwnd As Long,ByVal Msg As Long,ByVal wParam As Long,ByVal lParam As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long,ByVal dwNewLong As Long) As Long Private Const GWL_WNDPROC = (-4) Private Const WM_CUT = &H300 '-------------剪切消息 Private Const WM_COPY As Long = &H301 '-------------复制消息 Private Const WM_PASTE As Long = &H302 '-------------粘贴消息 Private Const WM_CLEAR = &H303 '-------------删除消息[右键菜单的删除] Private Const EM_UNDO = &HC7 '-------------撤销消息 Private Const WM_CONTEXTMENU = &H7B '-------------右键菜单 Private prevWndProc As Long在有需要的窗体中添加以下事件:
Private Sub Form_Load() DisableAbility txtUserName DisableAbility txtPassword End Sub为什么禁止右键呢,因为有的人或许不采用第一种方案,而是利用ASCII码对输入的内容进行了限制(额,这是在说我),虽然没有办法在用键盘输入特殊字符,但是我有Ctrl+V还有右键呀!
上面的我是不明觉厉的!