Posts Tagged “php”
Posted by Buzz in mySQL
The problem that most people face when setting up a UTF-8 database in mySQL is that without calling ‘SET NAMES’ in the mySQL client prior to issuing any queries (PHP, C++ etc …) that the client connection will actually in most cases default to latin-1.
However as of mySQL 5.x or higher you can issue a statement in the my.cnf file calling init_connect.
This will trigger a series of defined commands / queries every time a non super user connects (So if you are using root to connect to your mySQL database, stop reading now and slap yourself HARD).
i.e.
1 2 3 4 5 6 7
| [mysqld]
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake |
UPDATE 04/09/09
my mySQL version 5.0.45 x64 only picks up the last entry of init_connect
Use this example in this case:
1 2 3 4 5
| [mysqld]
init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;'
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci |
Restart mySQL and check the mysqld.log has not returned any errors (Or your event viewer if you are using windows).
Every client connection will now default to utf-8 encoding and not latin-1, removing the need to add a SET NAMES call on every connection.
This will work for PHP, C++, ruby etc… as the client encoding is now handeled server side, rather that waiting on the client to issue a SET NAMES command.
UPDATE 30/03/09: Added “skip-character-set-client-handshake” this ignores the clients request to set the connection charset, this info courtesy of “wardo” http://word.wardosworld.com/?p=164
UPDATE 10/09/09
Been having some issues with this working the workaround is to add this config as a single line:
1
| init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;' |
Tags: c++, init_connect, mySQL, php, utf-8, utf8
17 Comments »
Posted by Buzz in Linux, php
/usr/bin/ld: skipping incompatible /usr/lib/libcom_err.so when searching for -lcom_err
his one has been bugging me for a couple of hours now, when trying to compile PHP on a 64bit OS …
Simple put it’s a missing symlink, and the config script is trying to “failover” to the version is can find which is 32 bit …
ln -sf /lib64/libcom_err.so.2 /lib64/libcom_err.so
Et voila fixed!
Tags: compile, configure, libcom_err, libcom_err.so, Linux, php, skipping imcompatible, x64
1 Comment »
Posted by Buzz in hacking, php
This is another _old_ proof of concept I had several years ago, you can infact use PHP to scan ports, bare in mind the legality of this is still somewhat _hazy_ therefore if you must portscan I recomend you only do so on Systems you operate.
Disclaimer: This tutorial is provided for informational purposes only.
UPDATE: Project file now available from http://svn.saiweb.co.uk/branches/port_scanning/trunk/port_scanning.php
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ----- PORT SCAN 11 TCP PORTS -----
HOST: 127.0.0.1
DATE: Thu, 19 Jun 2008 08:43:13 +0100
PORT 80 OPEN
PORT 81 CLOSED
PORT 82 CLOSED
PORT 83 CLOSED
PORT 84 CLOSED
PORT 85 CLOSED
PORT 86 CLOSED
PORT 87 CLOSED
PORT 88 CLOSED
PORT 89 CLOSED
PORT 90 CLOSED
PORT 87 CLOSED
PORT 88 CLOSED
PORT 89 CLOSED
PORT 90 CLOSED |
NOTE: The current timeout is 0.5s per socket meaning you have a potential runtime of (($endport – $start_port) * 0.5) seconds. Make sure this does not excced your max execution time, or in the construct add:
This will increased the max execution time with a 5 second buffer.
Please also note in most cases of “shared” hosting you will not be able to crate socketed connections, they will either be blocked by the hosting providers firewall, or disabled at the php runtime, therfor not giving an accurate result.
Again please note this is a proof of concept, you may freely distribute the code under the MIT licence
Tags: php, portscan
No Comments »
Posted by Buzz in Acies, Linux, php
Well the XML rendering API has been giving me no end of head ache during the development … the end is in sight however.
Acies is moving along nicely, I am debating the use of globals over extended classes.
At this moment all objects are callable using the $this->CLASS->method(); this is fine in the current model of parent executing child, this does make accessing the parent objects from the child classes, much more difficult, however I want to avoid the use of many “Global” declarations …
*sigh* … Well as I strive to get this framework done no doubt there will be much more “hairpulling” …
Tags: Acies, php
No Comments »
“adimpleo” Latin for “to perform”, seemed a fitting name for dbStat.
dbStat is currently being re-worked to include the use of rrdtool among other features, “adimpleo” is to be geared towards providing an “under the hood” view of LAMP based web applications.
Expect screenshots soon.
Tags: adimpleo, lamp, mySQL, php, rrdtool
2 Comments »
So I thought maybe it’s time for an update.
The project is moving, albeit slowly, and I realy do not like the current PHP implementation, I want to move towards a C++ version, and I will do so as soon as I figure out how to do CLI “update/refresh” …
i.e. See how W GET works with the progress bar and kbps all in text, I have NO idea how that works.
I am also looking at adding RRDTOOL support.
Anyway here’s the current sample output:
1
| [buzz@server01 .sysadmin]$ ./dbstat.php summary |
1
| ----- mySQL dbStat v1.1 Summary Report----- |
1
| exampledb1: 13 tables (0 VIEWS 13 INNODB 0 MYISAM) 0.77MB DATA 0.64MB INDEX |
1
| exampledb2: 15 tables (0 VIEWS 14 INNODB 1 MYISAM) 0.22MB DATA 0.19MB INDEX |
1
| exampledb3: 62 tables (0 VIEWS 0 INNODB 62 MYISAM) 0.45MB DATA 0.5MB INDEX |
1
| exampledb4: 3 tables (0 VIEWS 0 INNODB 3 MYISAM) 0.02MB DATA 0.01MB INDEX |
1
| exampledb5: 4 tables (0 VIEWS 0 INNODB 4 MYISAM) 0.02MB DATA 0.01MB INDEX |
1
| exampledb6: 4 tables (0 VIEWS 0 INNODB 4 MYISAM) 39.81MB DATA 22.22MB INDEX |
1
| exampledb7: 3 tables (0 VIEWS 0 INNODB 3 MYISAM) 0.04MB DATA 0.01MB INDEX |
1
| exampledb9: 599 tables (8 VIEWS 1 INNODB 590 MYISAM) 8702.79MB DATA 4559.42MB INDEX |
1
| exampledb10: 22 tables (0 VIEWS 21 INNODB 1 MYISAM) 6.66MB DATA 2.26MB INDEX |
Detail:
1
| ----- START mySQL dbStat v1.1 Detail Report: exampledb9 ----- |
1
| exampledb9: 599 tables (8 VIEWS 1 INNODB 590 MYISAM) 8893.7MB DATA 4583.54MB INDEX |
1
| --- Table Index Ratio Report index:data (457 Tables) --- |
1
| exampledb9.table1: 315.0769:1 |
1
| exampledb9.table2: 315.0769:1 |
1
| exampledb9.table3: 157.5385:1 |
1
| exampledb9.table4: 146.2857:1 |
1
| exampledb9.table5: 128.0000:1 |
… (I’ve truncated this very long list)
1
| --- Table Fragmentation Report (2 Tables) --- |
1
| exampledb9.atable: 0.6067 |
1
| exampledb9.atable: 0.1285 |
1
| --- Table Low Size Report (47 Tables) --- |
1
| exampledb9.atable: 0 bytes |
1
| exampledb9.atable: 0 bytes |
1
| exampledb9.atable: 0 bytes |
1
| exampledb9.atable: 0 bytes |
1
| exampledb9.atable: 0 bytes |
… (Truncated again)
1
| --- Table Detail Report (591 Tables) --- |
1
| SCHEMA.TABLENAME: ENGINE: ROWS: TOTAL SIZE (MB): DATA SIZE (MB): DATA PERCENTAGE OF TOTAL (%): INDEX SIZE (MB): INDEX PERCENTAGE OF TOTAL (%): LAST UPDATE TIME |
1
| exampledb9.a_table: MyISAM: 28906414: 3272.43531003: 3031.82567548: 92.6474: 204.63809450: 7.3526: 2008-05-22 11:59:42 |
.. (truncated)
1
| ----- END mySQL dbStat v1.1 Detail Report: exampledb9 ----- |
Tags: dbStat, mySQL, php
No Comments »
Acies Latin: a sharp edge or point; mental acuity, sharpness of vision
A fitting name I thought for my new project, rather a name of a framework (yes I actually intend to finish this one!), initially the framework will comprise of code I have written over the years (brought in-line with PHP5).
More details will appear @ http://acies.saiweb.co.uk as I complete the modules.
There are several projects lined up awaiting completion of Acies, once in place I should be able to turn these around quite rapidly.
More Soon
Tags: Acies, framework, php, rapid development
No Comments »
To often I get passed code to review that quite frankly is so full of holes it wouldn’t make an adequate sieve…
So in this quick blog I outline a few simple and easily implemented steps to ensure as you start out in the world of PHP, your first site isn’t hacked within 5 minutes, leaving you a whimpering wrek …
PHP DON’T EXAMPLE 1:
Passing RAW globals to mysql!
i.e.
1 2
| $sql = "SELECT * FROM users WHERE email='.$_GET['email']."' and password='".$_GET['password']"';";
$result = mysql_query($sql); |
So what is wrong with the above? SQL INJECTION welcome to a world where people want to break your website, simply because they can …
I am not going to add more description, just click through to the wiki pedia entry linked above …
To avoid this PHP comes with two functions mysql_escape_string() and mysql_real_escape_string()
An example taken from the mysql_real_escape_string() page:
Example#2 An example SQL Injection Attack
1 2 3 4
| <span style="color: #000000;"><span style="color: #0000bb;"><?php
</span><span style="color: #ff8000;">// Query database to check if there are any matching users
</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'"</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">);</span></span> |
1 2 3 4 5
| <span style="color: #000000;"><span style="color: #007700;"> </span><span style="color: #ff8000;">// We didn't check $_POST['password'], it could be anything the user wanted! For example:
</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'username'</span><span style="color: #007700;">] = </span><span style="color: #dd0000;">'aidan'</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">] = </span><span style="color: #dd0000;">"' OR ''='"</span><span style="color: #007700;">;</span></span><span style="color: #007700;"><span style="color: #ff8000;">// This means the query sent to MySQL would be:
</span><span style="color: #007700;">echo </span><span style="color: #0000bb;">$query</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">?></span></span> |
The query sent to MySQL:
1
| <code>SELECT * FROM users WHERE user='aidan' AND password='' OR ''='' |
This would allow anyone to log in without a valid password.
So in summary READ the mysql_real_escape_string() page, and even if you don't implement the "best practice" example on that page PLEASE make sure you at least escape $_SESSION $_GET $_POST inputs with a mysql escape function!
Tags: injection, mySQL, php, sql
3 Comments »
Posted by Buzz in Apache, Linux
If like me you have a PHP framework, that runs multiple sites, you no doubt have thought at some point in time …
”Hey I realy could do with this re-write rule on that site, but I don’t want it applying to all sites running on the same framework”
Well fear not, after much head scratching, AccessFileName directive to the rescue! i.e.
Using the above method you can specify bespoke htaccess files on a per VirtualHost basis.
1 2 3 4 5 6 7
| <VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /path/to/framework
ServerName buzz.saiweb.co.uk
<strong>AccessFileName .buzz_htaccess</strong>
CustomLog logs/buzz_access_log combined
ErrorLog logs/buzz_error_log
</VirtualHost> |
Enjoy!
Tags: AccessFileName, Apache, framework, htaccess, multi site, php
No Comments »
So I find myself needing to tweak my Nagios installation a little bit, in this case I found the need for “out of hours” SMS alerts.
Nagios doesn’t cater for this natively, rather it does however allow you to create your own custom commands, this allows you to specify a script to be executed.
Now I am going to assume you are already quite familiar with Nagios , so here is the command definition from my installation.
# ‘alert-by-sms’ command definition
define command{
command_name alert-by-sms
command_line /etc/nagios/alert-by-sms.php “** $NOTIFICATIONTYPE$ alert – $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **”
}
As you can see all this command definition realy does is execute a php script, bear in mind that
“/path/to/php /path/to/script ”
as the command_line does not seem to work, so just add “#!/path/to/php -q” to the top of the php script (before the opening <?PHP tag). and CHMOD +X the file.
The php script used here takes $argv[1] and passes it into a function specific to the SMS api I use, the phone number and API definitions are hard coded ito the script.
You don’t really need me to upload my script, and if you do then you shouldn’t be attempting this …
Basically Nagios will execute the script, as defined at command_line, the script can do anything you choose.
Now to implement the command so it is actually used, I am pretty sure this entry in “timeperiods.cfg” is the default but just incase here it is.
# ‘nonworkhours’ timeperiod definition
define timeperiod{
timeperiod_name nonworkhours
alias Non-Work Hours
sunday 00:00-24:00
monday 00:00-09:00,17:00-24:00
tuesday 00:00-09:00,17:00-24:00
wednesday 00:00-09:00,17:00-24:00
thursday 00:00-09:00,17:00-24:00
friday 00:00-09:00,17:00-24:00
saturday 00:00-24:00
}
This is what I use for the “out of hours” definition, now to implement the SMS alerting, for this I have simply created a new contact definition in “contacts.cfg”, granted this means there are now two contact definitions for myself.
define contact{
contact_name out_of_hours
alias Out Of Hours Mobile
service_notification_period nonworkhours
host_notification_period nonworkhours
service_notification_options c,u,r,f
host_notification_options d,u,r
service_notification_commands alert-by-sms
host_notification_commands alert-by-sms
email HIDDEN EMAIL
}
This can be further customized depending on your setup, in this case the contact is me and I want to receive alerts for all servers & services, so I just add the contact “out_of_hours” into the admins contact group.
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagios-admin,out_of_hours
}
So there you have it, you now have the ground work to potentially make Nagios fire you alerts anyway you like, you could go as far as having it call you via attached modem, if you _realy_ want, but when you want your servers talking to you via phone call is the day you need to switch to decaff, and head out to the pub once in a while.
Now just “nagios -v /path/to/nagios.cfg” to do a quick sanity check and make sure there are no errors (if you have any go back and fix them and run nagios -v again!), if all is ok /etc/init.d/nagios restart (or equivalent for your distribution).
As always if you run into problems drop me a comment
Tags: alerting, customization, Linux, mobile, Nagios, Networking, phone, php, sms, text, txt
9 Comments »
|