msgbartop
Adam Palmer MBCS CITP, Linux, PHP Programmer, MySQL Developer, Embedded Hardware, Security Consultant
Did my blog help you? Please link to me!
  dns test
 
RSS Feed
msgbarbottom

19 Jan 10 Website Security Scan

Websites get hacked every day, customers details taken, and it’s usually REALLY EASY to do. As a security consultant,  I often get a call after a Google search turns up with my details as the guy to contact when this happens.

Shameless plug: Why not contact me BEFORE this happens for a FREE basic web scan.

Shameless plug over, why not consider some of the things that can be done to help prevent a website breach..
(more…)

Tags: , , , , , , , , , , , , , , , , , , ,

17 Dec 09 PHP Programmer – Logical Operators

PHP allows the use of boolean operators.

AND, OR, XOR and NOT. We can combine NOT with AND and OR to form the NAND and NOR operators respectively.

$a = ($b and $c); will return TRUE if both $b AND $c are TRUE, otherwise, it will return FALSE. This can also be specified as $a = ($b && $c)

$a = ($b or $c); will return TRUE if $b OR $c are TRUE, otherwise, it will return FALSE. This can also be specified as $a = ($b || $c);

$a = ($b xor $c); will return TRUE if $b OR $c are TRUE, but not if they are both TRUE, otherwise, it will return FALSE.

$a = (! $b); will return TRUE if $b is NOT TRUE.

$a = (!($b && $c)); will form NAND (NOT + AND)
$a = (!$b || $c)); will form NOR (NOT+AND);
(more…)

Tags: , , , , , , , , , ,

05 Nov 09 PHP Developer – Looping through database results

As a PHP Programmer, a very routine PHP/MySQL procedure is fetching a set of records from the result of a query.

$sql = "SELECT ...";
$result_set = mysql_query($sql);
for ($ctr = 0; $ctr < mysql_numrows($result_set); $ctr++)
{
    $my_object = mysql_fetch_object($result_set);
    //do something with $my_object
}

Now as tidy as the above code is, what’s the big problem? The number of rows returned by the query remains the same throughout. Why are we calling the mysql_numrows function on the same result set, to return the same answer over and over, possibly thousands of millions of times depending on the size of the result set? On a larger web application with a larger result set, things like this will dramatically increase unnecessary overhead. This is one of the most basic optimizations to make:

$sql = "SELECT ...";
$result_set = mysql_query($sql);
$result_num = mysql_numrows($result_set);
for ($ctr = 0; $ctr < $result_num; $ctr++)
{
    $my_object = mysql_fetch_object($result_set);
    //do something with $my_object
}

Now, there’s a couple of different methods you can use to achieve the same purpose, some of which may actually be more appropriate, such as a simple while loop, but the purpose of this article was to illustrate the issue above solely. More on optimization later..

Tags: , , , , ,

13 Oct 09 Copy/Export MySQL User Priviledges

I’m often asked how to copy or export MySQL Users from one machine to another. The following SQL query will show your users:

SELECT DISTINCT CONCAT (’show grants for `’, user, ‘`@`’, host, ‘`;’) AS query FROM mysql.user;

In my case on my test server, this shows:

SHOW GRANTS FOR ‘root’@'127.0.0.1′;
SHOW GRANTS FOR ‘debian-sys-maint’@'localhost’;
SHOW GRANTS FOR ‘root’@'localhost’;

Now, I’ll need to execute each one of these as separate statements. The output of SHOW GRANTS FOR ‘root’@'localhost’; is:

GRANT ALL PRIVILEGES ON *.* TO ‘root’@'localhost’ IDENTIFIED BY PASSWORD ‘*XXX…XXX’ WITH GRANT OPTION;

Copy and paste each ‘GRANT’ statement to your new SQL server, with the hashed password intact and you should be ready to go.

Tags: ,

26 Sep 09 PHP MySQL Developer – A Freelancer in London

Being a Freelance PHP MySQL Application Developer based in London has some major advantages as I found out today. The majority of both mine and my firm’s work is conducted online. Video conferencing over Skype, code delivery over SVN (Subversion), and bug tracking through Basecamp. Once in a while though an opportunity for a site visit in or around central London/West End pops up, and, schedule permitting, I’ll more often than not be happy to accept.

My core focus is on web application development, and being London based, I’ve had a chance to work with some great Companies. I’m currently at the time of writing, spending a few hours per week overseeing and managing a team of developers rewriting a wireless hotspot provider’s intranet which is proving to be very challenging, and great fun.

For more information on what it is that I actually do in the PHP/MySQL field, please view my PHP MySQL Developer series!

Tags: , , , ,

25 Sep 09 PHP MySQL Developer – Using MySQLi Prepared Statements to Avoid SQL Injection

I’m going to demonstrate a very short and simple method of avoiding SQL Injection at the SQL query level. You’ll need MySQLi support, on Debian you can apt-get install php5-mysql will contain everything that you need, and would be installed by default with your LAMP Installation.
(more…)

Tags: , , , , , , , , , , , , , ,

17 Sep 09 How to Find and Replace data in MySQL

It’s really easy!

UPDATE mytable SET myfield = REPLACE(myfield, ‘replace this’, ‘with this’);

Take a backup of your database first!

Tags: , ,

06 Sep 09 Security Consultant – PHP Developer – SQL Injection Attacks

One of the most common form of attacks against web applications is SQL Injection. In the most part, the language that the web application is written in is irrelevant, be that PHP, ASP, Python, Perl, C, etc. As long as the back end database uses something SQL based, be that MySQL, MSSQL, etc, again, we’re in business. This probably covers over 99% of web applications out there. Both the security consultant and the php developer or web application developer in general has to be aware of the implications of SQL Injection. Here’s how it works:
(more…)

Tags: , , , , , , , , , , ,

11 Mar 09 Simple MySQL Developer Intro

MySQL is one of the most powerful and widely used databases available. Here is a really quick guide to creating a database, creating a table, inserting, selecting and deleting the data, then table, then database. This will not go into too much depth as there are plenty of resources out there already that can provide more information on each step.

I’ll be using the MySQL command line tool on a Linux (Debian!) platform. Assuming I already have mysql running and a passworded root user account:

ns3:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 168753
Server version: 5.0.51a-24-log (Debian)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

(more…)

Tags: , , , , , ,

20 Jan 09 How to reset forgotton MySQL root password

As long as you have root access to your debian machine, you can do this as follows:

/etc/init.d/mysql stop #stop MySQL
/usr/bin/mysqld_safe –skip-grant-tables & #start MySQL with –skip-grant-tables
/usr/bin/mysql -u root mysql #connect to mysql as root, straight into the ‘mysql’ database. No password is required
UPDATE user SET password=PASSWORD(’newrootpassword’) WHERE user=’root’; #Do replace ‘newrootpassword’ with something that you’ll remember.
FLUSH PRIVILEGES;
\q #to quit
/etc/init.d/mysql stop #stop MySQL
/etc/init.d/mysql start #start MySQL

You can now test with mysql -u root -p you’ll be prompted for your password and your ‘newrootpassword’ should now work!

Tags: , , , , , , ,