This page intends to provide basic PHP security tips for developers and administrators. Keep in mind that tips mentioned in this page may not be sufficient. Learn ethical hacking, penetration testing, cyber security, best security and web penetration testing techniques from best ethical hackers in security field. CWE89 Improper Neutralization of Special Elements used in an SQL Command SQL Injection. BkqA49CcM/UzkRufOpNhI/AAAAAAAAAyo/SP-WXgkHqFM/s1600/tables.PNG' alt='Database Hacking Using Sql Injection Cheat' title='Database Hacking Using Sql Injection Cheat' />Like all web languages, there is also a large community of libraries etc. PHP. All three aspects language, framework, and libraries need to be taken into consideration when trying to secure a PHP site. PHP is a grown language rather than deliberately engineered, making writing insecure PHP applications far too easy and common. If you want to use PHP securely, then you should be aware of all its pitfalls. Language issues. Weak typing. PHP is weakly typed, which means that it will automatically convert data of an incorrect type into the expected type. This feature very often masks errors by the developer or injections of unexpected data, leading to vulnerabilities see Input handling below for an example. Try to use functions and operators that do not do implicit type conversions e. Not all operators have strict versions for example greater than and less than, and many built in functions like inarray use weakly typed comparison functions by default, making it difficult to write correct code. Exceptions and error handling. Almost all PHP builtins, and many PHP libraries, do not use exceptions, but instead report errors in other ways such as via notices that allow the faulty code to carry on running. This has the effect of masking many bugs. In many other languages, and most high level languages that compete with PHP, error conditions that are caused by developer errors, or runtime errors that the developer has failed to anticipate, will cause the program to stop running, which is the safest thing to do. Consider the following code which attempts to limit access to a certain function using a database query that checks to see if the username is on a black list. SELECT COUNTid FROM blacklistedusers WHERE username username. Code for feature here. There are various runtime errors that could occur in this for example, the database connection could fail, due to a wrong password or the server being down etc., or the connection could be closed by the server after it was opened client side. In these cases, by default the mysqli functions will issue warnings or notices, but will not throw exceptions or fatal errors. This means that the code simply carries on The variable row becomes NULL, and PHP will evaluate row0 also as NULL, and introw0 as 0, due to weak typing. Eventually the canaccessfeature function returns true, giving access to all users, whether they are on the blacklist or not. If these native database APIs are used, error checking should be added at every point. However, since this requires additional work, and is easily missed, this is insecure by default. It also requires a lot of boilerplate. This is why accessing a database should always be done by using PHP Data Objects PDO specified with the ERRMODEWARNING or ERRMODEEXCEPTION flags unless there is a clearly compelling reason to use native drivers and careful error checking. It is often best to turn up error reporting as high as possible using the. The behaviour of PHP code often depends strongly on the values of many configuration settings, including fundamental changes to things like how errors are handled. This can make it very difficult to write code that works correctly in all circumstances. Different libraries can have different expectations or requirements about these settings, making it difficult to correctly use 3rd party code. Some are mentioned below under Configuration. Unhelpful builtins. PHP comes with many built in functions, such as addslashes, mysqlescapestring and. Some of these built ins are being deprecated and removed, but due to backwards compatibility policies this takes a long time. PHP also provides an array data structure, which is used extensively in all PHP code and internally, that is a confusing mix between an array and a dictionary. This confusion can cause even experienced PHP developers to introduce critical security vulnerabilities such as Drupal SA CORE 2. Framework issues. URL routing. PHPs built in URL routing mechanism is to use files ending in. This opens up several vulnerabilities. Remote execution vulnerability for every file upload feature that does not sanitise the filename. In other words, the web server executes something instead of serving it. Ensure that when saving uploaded files, the content and filename are appropriately sanitised. Source code, including config files, are stored in publicly accessible directories along with files that are meant to be downloaded such as static assets. Misconfiguration or lack of configuration can mean that source code or config files that contain secret information can be downloaded by attackers. In other words, the web server serves a resource which should have been private or executable only. You can use. htaccess to limit access. This is not ideal, because it is insecure by default, but there is no other alternative. The URL routing mechanism is the same as the module system. This means it is often possible for attackers to use files as entry points which were not designed as such. This can open up vulnerabilities where authentication mechanisms are bypassed entirely a simple refactoring that pulls code out into a separate file can open a vulnerability. This is made particularly easy in PHP because it has globally accessible request data GET etc, so file level code can be imperative code that operates on the request, rather than needing request handling code to be within function definitions. The lack of a proper URL routing mechanism often leads to developers creating their own ad hoc methods. These are often insecure and fail to apply appropriate authorization restrictions on different request handling functionality. Input handling. Instead of treating HTTP input as simple strings, PHP will build arrays from HTTP input, at the control of the client. This can lead to confusion about data, and can easily lead to security bugs. For example, consider this simplified code from a one time nonce mechanism that might be used, for example in a password reset code. GETnonce. correctnonce getcorrectvaluesomehow. Go ahead and reset the password. Sorry, incorrect link. If an attacker uses a querystring like this. The function strcmp will then return NULL instead of throwing an exception, which would be much more useful, and then, due to weak typing and the use of the equality operator instead of the identity operator, the comparison succeeds since the expression NULL 0 is true according to PHP, and the attacker will be able to reset the password without providing a correct nonce. Exactly the same issue, combined with the confusion of PHPs array data structure, can be exploited in issues such as Drupal SA CORE 2. Template language. PHP is essentially a template language. However, it doesnt do HTML escaping by. XSS below. Other inadequacies. There are other important things that a web framework should supply, such as a. SQL injection Wikipedia. A classification of SQL injection attacking vector as of 2. SQL injection is a code injection technique, used to attack data driven applications, in which nefarious SQL statements are inserted into an entry field for execution e. SQL injection must exploit a security vulnerability in an applications software, for example, when user input is either incorrectly filtered for string literalescape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed. SQL injection is mostly known as an attack vector for websites but can be used to attack any type of SQL database. SQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server. In a 2. 01. 2 study, it was observed that the average web application received 4 attack campaigns per month, and retailers received twice as many attacks as other industries. HistoryeditThe first public discussions of SQL injection started appearing around 1. Phrack Magazine. 4SQL injection SQLI was considered one of the top 1. Open Web Application Security Project. In 2. SQLI was rated the number one attack on the OWASP top ten. There are four main sub classes of SQL injection The Storm Worm is one representation of Compounded SQLI. This classification represents the state of SQLI, respecting its evolution until 2. Technical implementationseditIncorrectly filtered escape characterseditThis form of SQL injection occurs when user input is not filtered for escape characters and is then passed into an SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application. The following line of code illustrates this vulnerability. SELECTFROMusers. WHEREname user. Name. This SQL code is designed to pull up the records of the specified username from its table of users. However, if the user. Name variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the user. Name variable as. SQL comments1. 3. All three lines have a space at the end. OR 11. OR 11. OR 11 renders one of the following SQL statements by the parent language SELECTROMusers. WHEREnameOR11 SELECTROMusers. WHEREnameOR11 If this code were to be used in an authentication procedure then this example could be used to force the selection of every data field from all users rather than from one specific user name as the coder intended, because the evaluation of 11 is always true. The following value of user. Name in the statement below would cause the deletion of the users table as well as the selection of all data from the userinfo table in essence revealing the information of every user, using an API that allows multiple statements. DROPTABLEusers SELECTROMuserinfo. WHEREtt. This input renders the final SQL statement as follows and specified SELECTROMusers. WHEREnamea DROPTABLEusers SELECTROMuserinfo. WHEREtt While most SQL server implementations allow multiple statements to be executed with one call in this way, some SQL APIs such as PHPs mysqlquery function do not allow this for security reasons. This prevents attackers from injecting entirely separate queries, but doesnt stop them from modifying queries. Incorrect type handlingeditThis form of SQL injection occurs when a user supplied field is not strongly typed or is not checked for type constraints. This could take place when a numeric field is to be used in a SQL statement, but the programmer makes no checks to validate that the user supplied input is numeric. For example. statement SELECTROMuserinfo. WHEREid avariable. It is clear from this statement that the author intended avariable to be a number correlating to the id field. Crack Money Wiz Mac there. However, if it is in fact a string then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting avariable to. DROP TABLE users. SQL becomes SELECTROMuserinfo. WHEREid1 DROPTABLEusers Blind SQL injectioneditBlind SQL Injection is used when a web application is vulnerable to an SQL injection but the results of the injection are not visible to the attacker. The page with the vulnerability may not be one that displays data but will display differently depending on the results of a logical statement injected into the legitimate SQL statement called for that page. This type of attack has traditionally been considered time intensive because a new statement needed to be crafted for each bit recovered, and depending on its structure, the attack may consist of many unsuccessful requests. Recent advancements have allowed each request to recover multiple bits, with no unsuccessful requests, allowing for more consistent and efficient extraction. There are several tools that can automate these attacks once the location of the vulnerability and the target information has been established. Conditional responseseditOne type of blind SQL injection forces the database to evaluate a logical statement on an ordinary application screen. As an example, a book review website uses a query string to determine which book review to display. So the URLhttp books. Review. php ID5 would cause the server to run the query. SELECTROMbookreviews. WHEREIDValueID from which it would populate the review page with data from the review with ID 5, stored in the table bookreviews. The query happens completely on the server the user does not know the names of the database, table, or fields, nor does the user know the query string. The user only sees that the above URL returns a book review. A hacker can load the URLs http books. Review. php ID5. OR11 and http books. Review. php ID5. AND12, which may result in queries. SELECTROMbookreviews. WHEREID5OR11 SELECTROMbookreviews. WHEREID5AND12 respectively. If the original review loads with the 11 URL and a blank or error page is returned from the 12 URL, and the returned page has not been created to alert the user the input is invalid, or in other words, has been caught by an input test script, the site is likely vulnerable to a SQL injection attack as the query will likely have passed through successfully in both cases. The hacker may proceed with this query string designed to reveal the version number of My. SQL running on the server http books. Review. php ID5. ANDsubstringversion,1,INSTRversion,. My. SQL 4 and a blank or error page otherwise. The hacker can continue to use code within query strings to glean more information from the server until another avenue of attack is discovered or his or her goals are achieved. Second order SQL injectioneditSecond order SQL injection occurs when submitted values contain malicious commands that are stored rather than executed immediately. In some cases, the application may correctly encode an SQL statement and store it as valid SQL. Then, another part of that application without controls to protect against SQL injection might execute that stored SQL statement. Database Hacking Using Sql Injection Cheat© 2017