Sophie

Sophie

distrib > PLD > ac > amd64 > media > dist > by-pkgid > dd8ef74e7a184506d40e4328053fb785 > files > 3939

php-manual-ro-20051028-1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Database Security</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Manual PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Security"
HREF="security.index.html"><LINK
REL="PREVIOUS"
TITLE="Filesystem Security"
HREF="security.filesystem.html"><LINK
REL="NEXT"
TITLE="Error Reporting"
HREF="security.errors.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-2"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Manual PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="security.filesystem.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Cap. 16. Security</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="security.errors.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="security.database"
>Database Security</A
></H1
><P
>&#13;    Nowadays, databases are cardinal components of any web based application by
    enabling websites to provide varying dynamic content. Since very sensitive
    or secret information can be stored in a database, you should strongly
    consider protecting your databases.
   </P
><P
>&#13;    To retrieve or to store any information you need to connect to the database,
    send a legitimate query, fetch the result, and close the connection.
    Nowadays, the commonly used query language in this interaction is the
    Structured Query Language (SQL). See how an attacker can <A
HREF="security.database.html#security.database.sql-injection"
>tamper with an SQL query</A
>.
   </P
><P
>&#13;    As you can surmise, PHP cannot protect your database by itself. The
    following sections aim to be an introduction into the very basics of how to
    access and manipulate databases within PHP scripts.
   </P
><P
>&#13;    Keep in mind this simple rule: defense in depth. The more places you
    take action to increase the protection of your database, the less
    probability of an attacker succeeding in exposing or abusing any stored
    information. Good design of the database schema and the application
    deals with your greatest fears.
   </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="security.database.design"
>Designing Databases</A
></H2
><P
>&#13;      The first step is always to create the database, unless you want to use
      one from a third party. When a database is created, it is
      assigned to an owner, who executed the creation statement. Usually, only
      the owner (or a superuser) can do anything with the objects in that
      database, and in order to allow other users to use it, privileges must be
      granted.
     </P
><P
>&#13;      Applications should never connect to the database as its owner or a
      superuser, because these users can execute any query at will, for
      example, modifying the schema (e.g. dropping tables) or deleting its
      entire content.
     </P
><P
>&#13;      You may create different database users for every aspect of your
      application with very limited rights to database objects. The most
      required privileges should be granted only, and avoid that the same user
      can interact with the database in different use cases. This means that if
      intruders gain access to your database using your applications credentials,
      they can only effect as many changes as your application can.
     </P
><P
>&#13;      You are encouraged not to implement all the business logic in the web
      application (i.e. your script), instead do it in the database schema
      using views, triggers or rules. If the system evolves, new ports will be
      intended to open to the database, and you have to re-implement the logic
      in each separate database client. Over and above, triggers can be used
      to transparently and automatically handle fields, which often provides
      insight when debugging problems with your application or tracing back
      transactions.
     </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="security.database.connection"
>Connecting to Database</A
></H2
><P
>&#13;     You may want to estabilish the connections over SSL to encrypt
     client/server communications for increased security, or you can use ssh
     to encrypt the network connection between clients and the database server.
     If either of these is used, then monitoring your traffic and gaining
     information about your database will be difficult for a would-be attacker.
    </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="security.database.storage"
>Encrypted Storage Model</A
></H2
><P
>&#13;     SSL/SSH protects data travelling from the client to the server, SSL/SSH
     does not protect the persistent data stored in a database. SSL is an
     on-the-wire protocol.
    </P
><P
>&#13;     Once an attacker gains access to your database directly (bypassing the
     webserver), the stored sensitive data may be exposed or misused, unless
     the information is protected by the database itself. Encrypting the data
     is a good way to mitigate this threat, but very few databases offer this
     type of data encryption.
    </P
><P
>&#13;     The easiest way to work around this problem is to first create your own
     encryption package, and then use it from within your PHP scripts. PHP
     can assist you in this with several extensions, such as <A
HREF="ref.mcrypt.html"
>Mcrypt</A
> and <A
HREF="ref.mhash.html"
>Mhash</A
>, covering a wide variety of encryption
     algorithms. The script encrypts the data before inserting it into the database, and decrypts
     it when retrieving. See the references for further examples of how
     encryption works.
    </P
><P
>&#13;     In case of truly hidden data, if its raw representation is not needed
     (i.e. not be displayed), hashing may also be taken into consideration.
     The well-known example for the hashing is storing the MD5 hash of a
     password in a database, instead of the password itself. See also
     <A
HREF="function.crypt.html"
><B
CLASS="function"
>crypt()</B
></A
> and <A
HREF="function.md5.html"
><B
CLASS="function"
>md5()</B
></A
>.
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3839"
></A
><P
><B
>Exemplu 16-5. Using hashed password field</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#FF8000">// storing password hash<br /></font><font color="#0000BB">$query&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"INSERT INTO users(name,pwd) VALUES('%s','%s');"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">addslashes</font><font color="#007700">(</font><font color="#0000BB">$username</font><font color="#007700">), </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">$password</font><font color="#007700">));<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">pg_exec</font><font color="#007700">(</font><font color="#0000BB">$connection</font><font color="#007700">, </font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// querying if user submitted the right password<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"SELECT 1 FROM users WHERE name='%s' AND pwd='%s';"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">addslashes</font><font color="#007700">(</font><font color="#0000BB">$username</font><font color="#007700">), </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">$password</font><font color="#007700">));<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">pg_exec</font><font color="#007700">(</font><font color="#0000BB">$connection</font><font color="#007700">, </font><font color="#0000BB">$query</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">pg_numrows</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">) &gt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Welcome, $username!"</font><font color="#007700">;<br />}<br />else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Authentication failed for $username."</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="security.database.sql-injection"
>SQL Injection</A
></H2
><P
>&#13;     Many web developers are unaware of how SQL queries can be tampered with,
     and assume that an SQL query is a trusted command. It means that SQL
     queries are able to circumvent access controls, thereby bypassing standard
     authentication and authorization checks, and sometimes SQL queries even
     may allow access to host operating system level commands.
    </P
><P
>&#13;     Direct SQL Command Injection is a technique where an attacker creates or
     alters existing SQL commands to expose hidden data, or to override valuable
     ones, or even to execute dangerous system level commands on the database
     host. This is accomplished by the application taking user input and
     combining it with static parameters to build a SQL query. The following
     examples are based on true stories, unfortunately.
    </P
><P
>&#13;     Owing to the lack of input validation and connecting to the database on
     behalf of a superuser or the one who can create users, the attacker
     may create a superuser in your database.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3847"
></A
><P
><B
>Exemplu 16-6. 
       Splitting the result set into pages ... and making superusers
       (PostgreSQL and MySQL)
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$offset </font><font color="#007700">= </font><font color="#0000BB">argv</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]; </font><font color="#FF8000">// beware, no input validation!<br /></font><font color="#0000BB">$query&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"</font><font color="#007700">;<br /></font><font color="#FF8000">// with PostgreSQL <br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">pg_exec</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#0000BB">$query</font><font color="#007700">);<br /></font><font color="#FF8000">// with MySQL<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
      Normal users click on the 'next', 'prev' links where the <VAR
CLASS="varname"
>$offset</VAR
>
      is encoded into the URL. The script expects that the incoming
      <VAR
CLASS="varname"
>$offset</VAR
> is a decimal number. However, what if someone tries to
      break in by appending a <A
HREF="function.urlencode.html"
><B
CLASS="function"
>urlencode()</B
></A
>'d form of the
      following to the URL
      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3853"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="sql"
>// in case of PostgreSQL
0;
insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd)
    select 'crack', usesysid, 't','t','crack'
    from pg_shadow where usename='postgres';
--

// in case of MySQL
0;
UPDATE user SET Password=PASSWORD('crack') WHERE user='root';
FLUSH PRIVILEGES;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
      If it happened, then the script would present a superuser access to him.
      Note that <VAR
CLASS="literal"
>0;</VAR
> is to supply a valid offset to the
      original query and to terminate it.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Not&#227;: </B
>
      It is common technique to force the SQL parser to ignore the rest of the
      query written by the developer with <VAR
CLASS="literal"
>--</VAR
> which is the
      comment sign in SQL.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     A feasible way to gain passwords is to circumvent your search result pages.
     The only thing the attacker needs to do is to see if there are any submitted variables
     used in SQL statements which are not handled properly. These filters can be set
     commonly in a preceding form to customize <VAR
CLASS="literal"
>WHERE, ORDER BY,
     LIMIT</VAR
> and <VAR
CLASS="literal"
>OFFSET</VAR
> clauses in <VAR
CLASS="literal"
>SELECT</VAR
>
     statements. If your database supports the <VAR
CLASS="literal"
>UNION</VAR
> construct,
     the attacker may try to append an entire query to the original one to list
     passwords from an arbitrary table. Using encrypted password fields is 
     strongly encouraged.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3864"
></A
><P
><B
>Exemplu 16-7. 
       Listing out articles ... and some passwords (any database server)
      </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$query&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"SELECT id, name, inserted, size FROM products<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE size = '$size'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY $order LIMIT $limit, $offset;"</font><font color="#007700">;<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">odbc_exec</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     The static part of the query can be combined with another
     <VAR
CLASS="literal"
>SELECT</VAR
> statement which reveals all passwords:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3868"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="sql"
>'
union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;
--</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     If this query (playing with the <VAR
CLASS="literal"
>'</VAR
> and
     <VAR
CLASS="literal"
>--</VAR
>) were assigned to one of the variables used in
     <VAR
CLASS="varname"
>$query</VAR
>, the query beast awakened.
    </P
><P
>&#13;     SQL UPDATE's are also susceptible to attack. These queries are
     also threatened by chopping and appending an entirely new query to it. But
     the attacker might fiddle with the <VAR
CLASS="literal"
>SET</VAR
> clause. In this
     case some schema information must be possessed to manipulate the query
     successfully. This can be acquired by examining the form variable names, or
     just simply brute forcing. There are not so many naming conventions for
     fields storing passwords or usernames.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3875"
></A
><P
><B
>Exemplu 16-8. 
      From resetting a password ... to gaining more privileges (any database server)
     </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$query </font><font color="#007700">= </font><font color="#DD0000">"UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     But a malicious user sumbits the value
     <VAR
CLASS="literal"
>' or uid like'%admin%'; --</VAR
> to <VAR
CLASS="varname"
>$uid</VAR
> to
     change the admin's password, or simply sets <VAR
CLASS="varname"
>$pwd</VAR
> to
     <VAR
CLASS="literal"
>"hehehe', admin='yes', trusted=100 "</VAR
> (with a trailing
     space) to gain more privileges. Then, the query will be twisted:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3882"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#FF8000">// $uid == ' or uid like'%admin%'; --<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#DD0000">"UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// $pwd == "hehehe', admin='yes', trusted=100 "<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#DD0000">"UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE<br />...;"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     A frightening example how operating system level commands can be accessed
     on some database hosts.
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3885"
></A
><P
><B
>Exemplu 16-9. Attacking the database hosts operating system (MSSQL Server)</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$query&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"SELECT * FROM products WHERE id LIKE '%$prod%'"</font><font color="#007700">;<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mssql_query</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     If attacker submits the value
     <VAR
CLASS="literal"
>a%' exec master..xp_cmdshell 'net user test testpass /ADD' --</VAR
>
     to <VAR
CLASS="varname"
>$prod</VAR
>, then the <VAR
CLASS="varname"
>$query</VAR
> will be:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3891"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$query&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">"SELECT * FROM products<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE id LIKE '%a%'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exec master..xp_cmdshell 'net user test testpass /ADD'--"</font><font color="#007700">;<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mssql_query</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
     MSSQL Server executes the SQL statements in the batch including a command
     to add a new user to the local accounts database. If this application
     were running as <VAR
CLASS="literal"
>sa</VAR
> and the MSSQLSERVER service is
     running with sufficient privileges, the attacker would now have an
     account with which to access this machine.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Not&#227;: </B
>
      Some of the examples above is tied to a specific database server. This
      does not mean that a similar attack is impossible against other products.
      Your database server may be similarly vulnerable in another manner.
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="security.database.avoiding"
>Avoiding techniques</A
></H3
><P
>&#13;      You may plead that the attacker must possess a piece of information
      about the database schema in most examples. You are right, but you
      never know when and how it can be taken out, and if it happens,
      your database may be exposed. If you are using an open source, or
      publicly available database handling package, which may belong to a
      content management system or forum, the intruders easily produce
      a copy of a piece of your code. It may be also a security risk if it
      is a poorly designed one.
     </P
><P
>&#13;      These attacks are mainly based on exploiting the code not being written
      with security in mind. Never trust any kind of input, especially that
      which comes from the client side, even though it comes from a select box,
      a hidden input field or a cookie. The first example shows that such a
      blameless query can cause disasters.
     </P
><P
></P
><UL
><LI
><P
>&#13;        Never connect to the database as a superuser or as the database owner.
        Use always customized users with very limited privileges.
       </P
></LI
><LI
><P
>&#13;        Check if the given input has the expected data type. PHP has
        a wide range of input validating functions, from the simplest ones
        found in <A
HREF="missing-stuff.html#ref.var"
>Variable Functions</A
> and
        in <A
HREF="ref.ctype.html"
>Character Type Functions</A
>
        (e.g. <A
HREF="function.is-numeric.html"
><B
CLASS="function"
>is_numeric()</B
></A
>, <A
HREF="function.ctype-digit.html"
><B
CLASS="function"
>ctype_digit()</B
></A
>
        respectively) and onwards to the
        <A
HREF="ref.pcre.html"
>Perl compatible Regular Expressions</A
>
        support.
       </P
></LI
><LI
><P
>&#13;        If the application waits for numerical input, consider verifying data
        with <A
HREF="function.is-numeric.html"
><B
CLASS="function"
>is_numeric()</B
></A
>, or silently change its type
        using <A
HREF="function.settype.html"
><B
CLASS="function"
>settype()</B
></A
>, or use its numeric representation
        by <A
HREF="function.sprintf.html"
><B
CLASS="function"
>sprintf()</B
></A
>.
        <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3915"
></A
><P
><B
>Exemplu 16-10. A more secure way to compose a query for paging</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />settype</font><font color="#007700">(</font><font color="#0000BB">$offset</font><font color="#007700">, </font><font color="#DD0000">'integer'</font><font color="#007700">);<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#DD0000">"SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// please note %d in the format string, using %s would be meaningless<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$offset</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
       </P
></LI
><LI
><P
>&#13;        Quote each non numeric user input which is passed to the database with
        <A
HREF="function.addslashes.html"
><B
CLASS="function"
>addslashes()</B
></A
> or <A
HREF="function.addcslashes.html"
><B
CLASS="function"
>addcslashes()</B
></A
>.
        See <A
HREF="security.database.html#security.database.storage"
>the first example</A
>.
        As the examples shows, quotes burnt into the static part of the query
        is not enough, and can be easily cracked.
       </P
></LI
><LI
><P
>&#13;        Do not print out any database specific information, especially
        about the schema, by fair means or foul. See also <A
HREF="security.errors.html"
>Error Reporting</A
> and <A
HREF="ref.errorfunc.html"
>Error Handling and Logging Functions</A
>.
       </P
></LI
><LI
><P
>&#13;        You may use stored procedures and previously defined cursors to abstract
        data access so that users do not directly access tables or views, but
        this solution has another impacts.
       </P
></LI
></UL
><P
>&#13;      Besides these, you benefit from logging queries either within your script
      or by the database itself, if it supports logging. Obviously, the logging is unable
      to prevent any harmful attempt, but it can be helpful to trace back which
      application has been circumvented. The log is not useful by itself, but
      through the information it contains. More detail is generally better than less.
     </P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="security.filesystem.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Acas&#227;</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="security.errors.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Filesystem Security</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="security.index.html"
ACCESSKEY="U"
>Sus</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Error Reporting</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>