Hi everyone, <br />A few months ago we did a project to get certified at the institute, but although I knew about this vulnerability, I couldn't fully understand it, until recently when I was doing the hackthissite wargames and researched it thoroughly to pass a challenge.<br />So I'll make the post with the project I did for the institute.<br />Well, first you must find a screen that loads information via the GET method (URL) <br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />To know if it's vulnerable to SQLI we put a ' at the end and if it throws an error it means it's vulnerable<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Also, instead of the ' you can put AND 1 = 0 (if it doesn't load records it's because it's vulnerable)<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Now we need to know how many columns the query that uses that table has.<br />For that we're going to use an ORDER BY by ranges, that is, first we'll put ORDER BY 5, if it shows us data then we increase the range to 10 and so on by 5s.<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Well, something I haven't mentioned is that the query sent to the database goes something like this<br />SELECT * FROM historial_caddie WHERE id_usurario = 199 ORDER BY 5 ORDER BY fecha ASC;<br />As you can see there are two ORDER BY (the red one is the one being inserted in the url), to solve this problem we'll put –- which means comment in SQL, and it will comment out everything that follows after our ORDER BY inserted in the URL<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />So it shows us the screen again, now we continue with the ranges<br />ORDER BY 5-> Still showing data<br />ORDER BY 10-> Still showing data<br />ORDER BY 15 -> Still showing data<br />ORDER BY 20 -> Shows an error <br />SQLSTATE[42S22]: Column not found: 1054 Unknown column '20' in 'order clause'<br />The error may vary depending on the type of connection (in my case we used php PDO, but if you use mysqli a different one comes up, but it will always say something about the columns)<br />Now we know that the number of columns the query brings is between 15 and 20, so we start decreasing the number until it shows us the page again without errors<br />ORDER BY 19 -> Still showing the error<br />ORDER BY 18 -> Still showing the error<br />ORDER BY 17 -> Shows the page again<br />The next thing we must do is use the UNION statement, which will allow us to make another query within the first query, that is UNION SELECT......<br />What we'll do is put UNION SELECT and the consecutive numbers of the columns the database has <br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />But now I don't like that it brings the information loaded by the original query, to remove it we'll negate the query, simply by putting a - before the id_usuario <br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Now, the numbers shown in the table are the ones we must modify in the url so it brings data.<br />We can get the version, replacing a number shown with version(), to get the name of the database we replace a number with database()<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />So far so good, what follows is to know the names and the number of tables in the database, for that we replace a number (for example the same 3) and put TABLE_NAME, at the end of the query (before the --) we type FROM information_schema.tables WHERE table_schema = database()<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />Well, for those who don't know, mysql has a database called information_schema where all the tables of all existing databases are stored, we can remove the where and it will bring all existing tables, but what interests us is only those of the database this program uses.<br />I notice there's a table called rol and one called usuarios, so I replace where it says TABLE_NAME with COLUMN_NAME and in the information_schema.tables I also change to information_schema.columns and also in the WHERE an AND TABLE_NAME = 'usuarios' ( or the table you want to query) must be added<br />/sicagolf_php/?controller=master&method=verHistorial&id_usuario=-223 UNION SELECT 1,2,COLUMN_NAME,4,5,6,7,8,9,10,11,12,13,14,15,16,17 FROM information_schema.columns WHERE table_schema = database() AND TABLE_NAME = 'usuarios'—<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />After identifying the access columns such as correo, password, id_rol we proceed to do the following:<br />1. Replace the numbers 3,4,5 (or the ones we want, depends on each page and each query) with the column names<br />2. Remove the entire WHERE and just leave FROM usuarios (because we already know the table columns and we don't need the information_schema anymore)<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />The correo and password data are now listed (encrypted of course), what we need to know is which is the admin, because it wouldn't be worth wasting time decrypting a password and it turns out to be a user without the desired privileges.<br />Before we saw there's a table called rol, if we query it in the information_schema, we notice it has two columns id_rol and rol where rol is the name of the role.<br />This indicates the tables are related, so we proceed to do an INNER JOIN to know the role of the listed users.<br />We put FROM usuarios u INNER JOIN rol r ON u.id_rol = r.id_rol at the end of the url (before the --) and we change the number 5 to rol<br />I'm not going to explain about INNER JOIN because it's off the topic of the post.<br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br />And well, the role with the most privileges is Master by logic, so that's the key we must decrypt, that part would be up to each person, how and with what tool to decrypt the password, honestly I tried with John The Ripper, but I don't know how to use it well hahahaha.<br />One more thing, in mozilla it allows entering spaces in the url, but in google chrome it puts a %20, what you should do is replace the spaces with a + so it doesn't look so ugly.<br />This is where the Post ends, I hope you liked it.<br />
🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
It's very well done!! and explained.<br />Good job!<br /><br />Thanks for sharing it.<br /><br />Best wishes.<br />
Very good tutorial @elshotta, didactic and well explained.<br /><br />Surely it will be very useful to everyone who is starting out.<br /><br />Regards.
@elshotta<br /><br />It's awesome!<br /><br />~ DtxdF
Good post! It's recommended to get used to SQL manually.
Very enjoyable. They should recommend this as an introduction to sqli. I'll do my part.
good post thank you very much
but just by putting a couple of security codes in the .htaccess file located in the main directory, bye bye "hackers"
#
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#
Options -Indexes
#
<LimitExcept GET POST>
Deny from all
</LimitExcept>
#
<FilesMatch "\.(htaccess|htpasswd|ini|env|log|sh)$">
Order allow,deny
Deny from all
</FilesMatch>
#
<IfModule mod_headers.c>
# Enable the X-XSS-Protection header
Header set X-XSS-Protection "1; mode=block"
</IfModule>
#
RewriteEngine On
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
#
<IfModule mod_headers.c>
Header set X-Content-Type-Options: nosniff
</IfModule>
#
<IfModule mod_headers.c>
Header set Edge-Control "no-cache, no-store"
</IfModule>
<IfModule mod.headers.c>
Header always set Strict-Transport-Security "max-age=5555555; includeSubDomains; preload"
</IfModule>
#
<IfModule mod_headers.c>
Header set Age "0"
</IfModule>
#
<IfModule mod_headers.c>
Header set Cache-Control "max-age=11, must-revalidate"
</IfModule>
#
<IfModule mod_headers.c>
Header set X-DNS-Prefetch-Control "on"
</IfModule>
#
Header unset X-Powered-By
#
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#
Options -Indexes
#
<LimitExcept GET POST>
Deny from all
</LimitExcept>
#
<FilesMatch "\.(htaccess|htpasswd|ini|env|log|sh)$">
Order allow,deny
Deny from all
</FilesMatch>
#
<IfModule mod_headers.c>
# Enable the X-XSS-Protection header
Header set X-XSS-Protection "1; mode=block"
</IfModule>
#
RewriteEngine On
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
#
<IfModule mod_headers.c>
Header set X-Content-Type-Options: nosniff
</IfModule>
#
<IfModule mod_headers.c>
Header set Edge-Control "no-cache, no-store"
</IfModule>
<IfModule mod.headers.c>
Header always set Strict-Transport-Security "max-age=5555555; includeSubDomains; preload"
</IfModule>
#
<IfModule mod_headers.c>
Header set Age "0"
</IfModule>
#
<IfModule mod_headers.c>
Header set Cache-Control "max-age=11, must-revalidate"
</IfModule>
#
<IfModule mod_headers.c>
Header set X-DNS-Prefetch-Control "on"
</IfModule>
#
Header unset X-Powered-By
besides... that type of sql injection (which was trendy in 2011) if in the case of practicing it on a website with very little security and all the security holes open (the only way that type of sql injection would work) in the case of getting lucky finding a page for that hacking technique to work... you couldn't see the password because all passwords are encrypted, in any case you'd find an encrypted code which you could then test if that code could be decrypted, and if the code containing the password contains a long, alphanumeric password with some symbol, it would be almost impossible to decrypt it
Cita de: Hern@n en Agosto 21, 2025, 05:18:25 PMbesides... that type of sql injection (which was trendy in 2011) if in the case of practicing it on a website with very little security and all the security holes open (the only way that type of sql injection would work) in the case of getting lucky finding a page for that hacking technique to work... you couldn't see the password because all passwords are encrypted, in any case you'd find an encrypted code which you could then test if that code could be decrypted, and if the code containing the password contains a long, alphanumeric password with some symbol, it would be almost impossible to decrypt it
Exactly, but it would surprise you how many sites don't even bother to this day xD, Besides it's a post from 4 years ago lol
I remember quite a few years ago this was done for you by a program called Havij, I don't know if it still exists or if there's something like it now but it was TOP