18-5 ������X�

·í§A¾Ç·|¤F¸ê®Æ®w»P ASP ªº¾ã¦X¡A¤@©w«Ü°ª¿³¡A¦Ó¥B«æµÛ§â©Ò¦³ªº¸ê®Æ³£©ñ¨ì¸ê®Æ®w¡A¥H«K¶i¦æ§ó¦nªº¸ê®ÆºÞ²z¡C¦b¤U­±³o­Ó½d¨Ò¤¤¡A§Ú­Ì±N¨Ï¥ÎªÌªº±K½X¦s©ñ¦b¸ê®Æ®w¤§¤¤¡A¥H¹ï¨Ï¥ÎªÌªº±b¸¹©M±K½X¶i¦æ¦³®ÄªººÞ²z¡A¦¹¸ê®Æ®wªº¤º®e¦p¤U¡]password.mdb¡^¡G
userid passwd
CS3431 CS3431 
ªL¬F·½ gavins 
³¯¦¿§ø jtchen 
¸­¨Î¼z beball 

®Ú¾Ú¦¹¸ê®Æ®w¡A§Ú­Ì´N¥i¥H¼g¤@­Ó ASP ºô­¶¨Ó¶i¦æ±b¸¹©M±K½Xªº»{ÃÒ¡G

Example¡]database/password01.asp¡^¡G

¨ä­ì©l½X¦C¥X¦p¤U¡A¥H¨ÑŪªÌ¤ñ¸û¡G

­ì©lÀÉ¡]database/password01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@ language="jscript" %>
<% title="¥H¸ê®Æ®w¤º¤§¸ê®Æ¶i¦æ±K½X»{ÃÒ¡G°ò¥»½g" %>
<!--#include file="../head.inc"-->
<hr>

<% //§Q¥ÎASP¤º«ØªºRequestª«¥ó¨ú±oªí³æÄæ¦ìªº¡u±b¸¹¡v¤Î¡u±K½X¡v¡A'¨Ã§PÂ_¬O§_¬°ªÅ¥Õ¡C
x=Request("user")+"";
y=Request("passwd")+"";
if ((x=="undefined") && (y=="undefined")){ %>
	<% //Åã¥Ü­ì¦³ªºªí³æÄæ¦ì %>
	<form method="post">
	½Ð¿é¤J±b¸¹¤Î±K½X¡G
	<ul>
	<li>±b¸¹¡G<Input name="user" value="CS3431"><br>
	<li>±K½X¡G<Input type="password" name="passwd"><font color=white>±K½X¬O¡GCS3431</font>
		<p><input type=submit><input type=reset>
	</ul>
	</form>
	¡]´£¥Ü¡G«ö ctrl-a ¥i¥H¬Ý¨ì±K½X³á¡I¡^
	<hr>
	<!--#include file="../foot.inc"-->
	<% Response.End();	 // µ²§ôºô­¶ %>
<%}%>

<% //Åã¥Ü¬d¸ß¸ê®Æ®wµ²ªG
//======«Ø¥ßADO Connection¡AµM«á¶}±ÒAccess¸ê®Æ®w
Conn = Server.CreateObject("ADODB.Connection");
database = "password.mdb";
Conn.ConnectionString = "DBQ=" + Server.MapPath(database) + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
Conn.Open();
//======±q¸ê®Æªí¤¤¤ñ¸ûuserid»Ppasswd¨â­ÓÄæ¦ì¡A¬Ý¬Ý¬O§_©Mªí³æÄæ¦ìuser¤Îpasswd¬Û¦P¡C
SQL = "select * from password where userid='" + Request("user") + "' and passwd='" + Request("passwd") + "'";
//======°õ¦æSQL«ü¥O¡A¨Ã±Nµ²ªGÀx¦s©óRecordset¤¤
RS=Conn.Execute(SQL);
//======³z¹LRecordSet¶°¦X¨ú±oÄæ¦ìªº¤º®e
if (RS.EOF) {%>
	<p align=center>±b¸¹©Î±K½X¿ù»~¡I<br>SQL«ü¥O = <u><font color=green><%=SQL%></font></u>
<%} else {%>
	<p align=center>±b¸¹¤Î±K½X¥¿½T¡I<br>SQL«ü¥O = <u><font color=green><%=SQL%></font></u>
<%}
//======Ãö³¬¸ê®Æ®w
RS.Close();
Conn.Close();
%>

<hr>
<!--#include file="../foot.inc"-->

¬Ý°_¨Ó¤@¤Á¨S°ÝÃD¡A¦ý¬O¦pªG§A·Q¡uÀb¡v¡]Hack!¡^ ³o­Óºô¯¸¡A¨Æ¹ê¤W¥u­n¿é¤J¤U¦C¸ê®Æ´N¥i¥H¤F¡G

¡]½Ð»°§Ö¸Õ¸Õ¬Ý¡I¡^³o¬O¬°¤°»ò©O¡H¨Æ¹ê¤W³o´N¬O´c¦W¬L¹üªº¡u¸ê®ÆÁô½X¡v¡]SQL Injection¡^¯ä¦ä¡A²³æ¦a»¡¡A´N¬O±N¡u±b¸¹¡v©M¡u±K½X¡v¶ñ¤J¨ã¦³³æ¤Þ¸¹ªº¯S®í¦r¦ê¡A³y¦¨¦øªA¾¹ºÝ¦b±µ¦X³o¨ÇÄæ¦ì¸ê®Æ®É¡A·|·N¥~¦a²£¥Í¦X®æªº SQL «ü¥O¡A³y¦¨±K½X»{ÃÒªº¦¨¥\¡C­n¯S§Oª`·Nªº¬O¡ASQL Injection ªº°ÝÃD¤£­­¥uµo¥Í¦b­þºØ¯S©w¥­¥x©Î»y¨¥¡A¥u­n¬O¨Ï¥Î SQL «ü¥O¦s¨ú¸ê®Æ®w¤ºªº¸ê®Æ¡A³£¦³¥i¯à²£¥Í³o­Ó°ÝÃD¡C

§Ú­Ì¦A¨Ó¥J²Ó¬Ý¬Ý¤W­±³o­Ó½d¨Ò¡A¨ä¤¤²£¥Í SQL «ü¥Oªº±Ô­z¦p¤U¡G

SQL = "select * from password where userid='" + Request("user") + "' and passwd='" + Request("passwd") + "'"; ¬Ý°_¨ÓÅ޿觹¥þ¥¿½T¡A¨Ò¦p·í¿é¤J±b¸¹©M±K½X¤À§O¬O¡uªL¬F·½¡v©M¡ugavins¡v®É¡A©Ò±o¨ìªº SQL «ü¥O¬O¡G
SQL = "select * from password where userid='ªL¬F·½' and passwd='gavins'";
©Ò¥H¥i¥H±q¸ê®Æ®w¤¤¬d¨ì¤@µ§¸ê®Æ¡A¥Nªí±b¸¹©M±K½X¥¿½T¡C¦ý¬O·í§Ú­Ì±b¸¹©M±K½X¤À§O¬O¡uxyz¡v©M¡u' or 'a'='a¡v®É¡A©Ò±o¨ìªº SQL «ü¥O¬O
SQL = "select * from password where userid='xyz' and passwd='' or 'a'='a'";
«Ü¤£©¯ªº¡A©Ò²£¥Íªº SQL «ü¥O¤]·|°õ¦æ¦¨¥\¡]¦]¬° 'a'='a' ¬O¤@©w¦¨¥ßªº¡^¡A¦]¦Ó±q¸ê®Æ®w¤¤§ì¥X¦hµ§¸ê®Æ¡A³oºØ°Å±µ¤âªk§Ï©»¬O¦b SQL «ü¥O¤¤¡uÄéª`¡v¤@¨Ç´c·Nªº¦r¦ê¡A©Ò¥HºÙ¬°¡uSQL Injection¡v¡C

Hint
¦b SQL »yªkªº±ø¥ó¦¡¤¤¡A·|¥ý°õ¦æ and¡A¦A°õ¦æ or¡C

¦p¦óÁקK SQL Injection ©O¡H³Ì²³æªº§@ªk¡A´N¬O¦b¨ú¥Î«È¤áºÝ°e¶i¨Óªº¸ê®Æ«e¡A¥ý§R°£©Ò¦³¥i¯à³y¦¨°ÝÃDªº¯S®í¦r¤¸¡A³o¨Ç¦r¤¸¥]¬A³æ¤Þ¸¹¡]'¡^¡BÂù¤Þ¸¹¡]"¡^¡B°Ý¸¹¡]?¡^¡B¬P¸¹¡]*¡^¡B©³½u¡]_¡^¡B¦Ê¤À¤ñ¡]%¡^¡BAmpersand¡]&¡^µ¥¡A³o¨Ç¯S®í¦r¤¸³£¤£À³¸Ó¥X²{¦b¨Ï¥ÎªÌ¿é¤Jªº¸ê®Æ¤¤¡C¥t¥~¡A§R°£¯S®í¦r¤¸ªº°Ê§@°È¥²­n¦b¦øªA¾¹ºÝ¶i¦æ¡A¦]¬°¥Î¤áºÝªº JavaScript ªí³æÅçÃÒªºÀˬd¬O¥u¯à¨¾§g¤l¡A¤£¯à¨¾¤p¤H¡A§O¤H¥u­n°µ¤@­Ó¦³¬Û¦PÄæ¦ìªººô­¶¡A´N¤@¼Ë¥i¥H©I¥s§Aªº ASP µ{¦¡½X¨Ó¨ú¥Î¸ê®Æ®w¡A¶i¦ÓÁ׶}­ìºô­¶ªºªí³æÅçÃÒ¥\¯à¡C

­Y­n§R°£³o¨Ç¦MÀI¦r¤¸¡A¥i¥H¨Ï¥Î JavaScript ªº¦r¦êªº replace() ¤èªk¡A©Î¬O¨Ï¥Î VBScript ªº Replace ¨ç¼Æ¡A¨Ò¦p¡G

Example¡]database/sqlInjection01.asp¡^¡G

¨ä­ì©l½X¦C¥X¦p¤U¡G

­ì©lÀÉ¡]database/sqlInjection01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@ language="jscript" %>
<% title="¥H¸ê®Æ®w¤º¤§¸ê®Æ¶i¦æ±K½X»{ÃÒ¡G¦p¦óÁקK SQL Injection" %>
<!--#include file="../head.inc"-->
<hr>

<% //§Q¥ÎASP¤º«ØªºRequestª«¥ó¨ú±oªí³æÄæ¦ìªº¡u±b¸¹¡v¤Î¡u±K½X¡v¡A'¨Ã§PÂ_¬O§_¬°ªÅ¥Õ¡C
x=Request("user")+"";
y=Request("passwd")+"";
if ((x=="undefined") && (y=="undefined")){ %>
	<% //Åã¥Ü­ì¦³ªºªí³æÄæ¦ì %>
	<form method="post">
	½Ð¿é¤J±b¸¹¤Î±K½X¡G
	<ul>
	<li>±b¸¹¡G<Input name="user" value="ªL¬F·½"><br>
	<li>±K½X¡G<Input type="password" name="passwd" value="gavins">
		<p><input type=submit><input type=reset>
	</ul>
	</form>
	¡]´£¥Ü¡G«ö F7 ¥i¥H¿é¤J SQL Injection ©Ò¥Î¤§±b¸¹©M±K½X¡I¡^
	<script>
	function fillForm() {
		if (event.keyCode==118) {
			document.forms[0].user.value="³o¬O¥ô·N¦r¦ê"
			document.forms[0].passwd.value="' or 'a'='a"
		}
	}
	</script>
	<script>document.onkeydown=fillForm;</script>
	<hr>
	<!--#include file="../foot.inc"-->
	<% Response.End();	 // µ²§ôºô­¶ %>
<%}%>

<% //Åã¥Ü¬d¸ß¸ê®Æ®wµ²ªG
//=======¨ú±oªí³æÄæ¦ì¤º®e
user = Request("user")+"";
passwd = Request("passwd")+"";
user = user.replace(/'/g, "");		//§R°£³æ¤Þ¸¹¥HÁקK SQL Injection
passwd = passwd.replace(/'/g, "");	//§R°£³æ¤Þ¸¹¥HÁקK SQL Injection
//=======«Ø¥ßADO Connection¡AµM«á¶}±ÒAccess¸ê®Æ®w
Conn = Server.CreateObject("ADODB.Connection");
database = "password.mdb";
Conn.ConnectionString = "DBQ=" + Server.MapPath(database) + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
Conn.Open();
//=======±q¸ê®Æªí¤¤¤ñ¸ûuserid»Ppasswd¨â­ÓÄæ¦ì¡A¬Ý¬Ý¬O§_©Mªí³æÄæ¦ìuser¤Îpasswd¬Û¦P¡C
SQL = "select * from password where userid='" + user + "' and passwd='" + passwd + "'";
RS=Conn.Execute(SQL);
if (RS.EOF) {%>
	<p align=center>±b¸¹©Î±K½X¿ù»~¡I<br>SQL«ü¥O = "<u><font color=green><%=SQL%></font></u>"
<%} else {%>
	<p align=center>±b¸¹¤Î±K½X¥¿½T¡I<br>SQL«ü¥O = "<u><font color=green><%=SQL%></font></u>"
<%}
//======Ãö³¬¸ê®Æ®w
RS.Close();
Conn.Close();
%>

<hr>
<!--#include file="../foot.inc"-->

¦b¤W­z­ì©l½X¤¤¡A¦]¬° Request("userid") ©M Request("passwd") ªº¸ê®Æ¬OµLªk­×§ïªº¡A©Ò¥H¦b¨ú¥N«e­n¥ý¦s¨ì¥t¤@­Ó­ÓÅܼơC¥Ñ¦¹½d¨Ò¥i¥Hª¾¹D¡A¥u­n§R°£¨Ï¥ÎªÌ¿é¤J¦r¦ê¤¤ªº©Ò¦³³æ¤Þ¸¹¡A´N¥i¥HÁקK SQL Injection ªº°ÝÃD¡C

¨Æ¹ê¤W¡A¥i¥H§Î¦¨ SQL Injection ªº´c·N¦r¦êÁÙ¤£¤Ö¡A¦ý¤j³¡¤À¬O°w¹ï·L³nªº SQL Server ¸ê®Æ®w¨Ó¶i¦æ¯}Ãa¡C­Y¦³¿³½ì¡AŪªÌ¥i¦Û¦æ°Ñ¦Ò¤U¦C°Ñ¦Ò¸ê®Æ¡G

¦pªG§A¨ì Google ¥´¤J¡uµn¤J¡v¡A¦A¹ï»Ý­nµn¤Jªººô¯¸¶i¦æ SQL Injection ªº´ú¸Õ¡A´NÀ³¸Ó¥i¥H§ä¨ì¤@¨Ç¤£³]¨¾ªººô¯¸¡C½Ð¤d¸U¤£­n§@´c¡A­Y§ä¨ì³o¨Ç¤£³]¨¾ªººô¯¸¡A±N¤U¦C¤å¦r±Hµ¹¦¹ºô¯¸ªººûÅ@ªÌ¡]¤]¥i±N°Æ¥»±Hµ¹§Ú¡^¡G

·q±ÒªÌ¡G

§Ú­Ì¬ã²ß±i´¼¬P¦Ñ®vªº¡uJavaScriptµ{¦¡³]­p»PÀ³¥Î¡v¡A¹ïºô¸ô¤Wªººô­¶¶i¦æ SQL Injection ªº´ú¸Õ¡Aµoı±zªºµn¤Jºô­¶¡]ºô§}¬O http://xxx.xxx.xxx¡^¨ÃµLªk¹ï§Ü SQL Injection ªº¤J«I¡A¥u­n±b¸¹¥ô·N³]©w¡B±K½X³]©w¬°¡u' or 'a'='a¡v¡A§Y¥iµn¤J¡C

³o¬O¤@«Êµ½·Nªº«H¡A§Ú­Ì¶È´ú¸Õ¬O§_¥i¥Hµn¤J¡A¨Ã¥¼¹ï¸ê®Æ¶i¦æ¥ô¦ó­×§ï¡A½Ð¬d·Ó¡AÁÂÁ¡C

¡]½Ð¼g¥X§Aªº¥þ¦W¡^

ÁÂÁ±zªº§V¤O¡A³o¨Çºô¯¸ªººÞ²zªÌ·|·PÁ§A­Ìªºµ½¤ß¡I
JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò