SQLインジェクション攻撃とは、設計の脆弱性、ターゲットサーバーでSQLコマンドの実行、およびその他の攻撃を活用することを指します。
SQLインジェクション攻撃がSQLコマンドを動的に生成すると成功する主な理由。
例えば:
クエリステートメントがselect * select * from admin where username = '"&user&"'およびpassword = '"&pwd&"' "
次に、ユーザー名が1 'または' 1 '='の場合
次に、クエリステートメントは次のようになります。
select * from admin where username = '1または' 1 '=' 1 'およびpassword =' "&pwd&" '"
このようにして、クエリステートメントが渡され、管理インターフェイスを入力できます。
したがって、防止するときは、ユーザーの入力を確認する必要があります。単一の引用符、二重引用符、セミコロン、コンマ、コロン、接続などの特別な特殊文字が変換またはフィルタリングされています。
フィルタリングする必要がある特殊文字と文字列は次のとおりです。
ネットユーザー
XP_CMDSHELL
/追加
exec Master.dbo.xp_cmdshell
ネットローカルグループ管理者
選択します
カウント
ASC
char
ミッド
'
:
「
入れる
削除します
テーブルをドロップします
アップデート
切り捨てます
から
%
以下は、学習リファレンスのための注入攻撃の解決について書いた2つの予防コードです!
SQLインジェクション攻撃コードのJSバージョン〜:
[コードスタート]
<スクリプト言語= "javascript">
<! -
var url = location.search;
var re =/^/?(。*)(select%20 |挿入%20 | delete%20 from%20 |(| drop%20table | update%20trate%20 | asc/(| mid/(| char/(| char/(|)) (| xp_cmdshell | exec%20master | net%20localgroup%20administrators |/"|:| net%20user |/'|%20)(。*)$/gi;
var e = re.test(url);
if(e){
アラート( "アドレスには違法な文字が含まれています〜");
location.href = "error.asp";
}
//->
<スクリプト>
[コードエンド]
SQLインジェクション攻撃を防ぐためのコードのASPバージョン〜:
[コードスタート]
<%
エラーの再開時に次に再開します
薄暗いstretemp
lcase(request.servervariables( "https"))= "off" thenの場合
strtemp = "http://"
それ以外
strtemp = "https://"
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")
TRIM(request.QueryString)<> "" then strtemp& "?"
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 ")またはstrtemp、" update%20 ")またはstrtemp、" struncate%20 ")またはstrtemp、" asc( ")またはstrtemp、" mid( ")またはstremp(strtemp 、「char( ")またはstrtemp、" xp_cmdshell ")またはInstr(strtemp、 "exec%20master")またはstrtemp、 "net%20localgroup%20administrators")またはstrtemp、 ":")またはstrtemp、 "net%20user")またはstrtemp、 "'' '' ' ")またはstrtemp、"%20or%20 ")
Response.Write "<スクリプト言語= 'JavaScript'>"
respons.write "alert( 'Illegal Address !!');"
Response.write "location.href = 'error.asp';"
Response.Write "<script>"
ifを終了します
%>
[コードエンド]
C#SQL注入攻撃を防ぐために文字列をチェックします
この例では、=および 'numbersとして暫定的に指定されています
bool checkparams(params object [] args)
{
string [] lawless = {"="、 "'"};
if(lawless == null || lawless.length <= 0)trueを返します。
//正規表現を構築します。例:lawlessは=記号であり、「記号」です。正規表現は。* [=}]。*(正規表現の関連コンテンツについては、MSDNを参照してください)
//さらに、機能を簡単に変更したいので、キャラクター配列から実際の式までの別のステップがあります。
string str_regex = "。*[";
for(int i = 0; i <lawless.length-1; i ++)
str_regex+= lawless [i]+"|";
str_regex+= lawless [lawless.length-1]+"]。*";
//
foreach(argsのオブジェクトarg)
{
if(arg is string)//文字列の場合は、直接確認してください
{
if(regex.matches(arg.tostring()、str_regex).count> 0)
falseを返します。
}
else(argはicollection)//コレクションの場合、コレクションの要素が文字列であるかどうかを確認してください
{
foreach((icollection)argのオブジェクトobj)
{
if(objは文字列)
{
if(regex.matches(obj.tostring()、str_regex).count> 0)
falseを返します。
}
}
}
}
trueを返します。