insert into education (info) values (:thispost)
Check your input data before inserting it into an SQL Query.
The issue of SQL-Injection has been raised since like 5 years ago, and still there are people who keep ignoring it.
If you’re using PHP to query your database, make sure you’re not inserting crap first.
The easiest things you can do, and you can make it pretty complicated, are the following:
Check integers and ID’s given by the user
if ( !is_numeric( $userinteger ) ) {
return false;
}
Don’t just insert strings, escape them
$sql = "select * from users where username=':user' and password=SHA1(':pass')";
$sql = str_replace( ":user", mysql_real_escape_string($user), $sql );
$sql = str_replace( ":pass", mysql_real_escape_string($pass), $sql );
$res = mysql_query( $sql );
And stop using MD5.
Add comment February 23, 2009
Hacking a filter for Twitter
The following php script fetches a Twitter RSS feed and filters out all the irrelevant comments you made.
<?
// replace url with your twitter rss feed
$lines = file( "http://twitter.com/statuses/user_timeline/1234567890.rss" );
$skip = false;
$c = count( $lines );
for ( $i = 0; $i < $c; $i++ ) {
$line = $lines[$i];
$p = strpos( $line, "<item>" );
if ( $p !== false ) {
// Replace YourName with your twitter account name
$p2 = strpos( $lines[$i+1], "YourName: @" );
if ( $p2 !== false ) {
$skip = true;
}
}
if ( $skip ) {
if ( strpos( $line, "</item>" ) !== false ) {
$skip = false;
}
} else {
echo $line;
}
}
?>
2 comments February 22, 2009
Serving over multiple external IP’s
Normal people tend to just rely on 1 single internet connection to do both websurfing and server hosting, and some might even wonder if 2 internet connections is even going to work.
Let me explain why you can’t use 2 external ip’s in a normal 2x modem-router-network configuration.
To make things easy, I’ll give the actors some names in this example;
- Alice is the person who’s visiting your website
- Fred is the name of your internal web server
- Lucy is the name of the first router, and is the default router for outgoing traffic
- Peter is the name of the second router
When you configure Peter to forward/translate a specific port (eg tcp 80 for http) you supply the internal IP address Fred, and everything just works. Peter copy/pastes Fred’s mac-address into the IP-packet, changes the port number if needed, and sends it to your network. The external source IP address of Alice is still there in the packet, and Fred will use that IP address to send packets back Alice containing your website and other things.
When Fred tries to figure out on how to reach Alice, he figures he should just use the default router that is supplied in his network configuration, so he puts the MAC address of Lucy into a packet along with Alice her IP address and sends it. Lucy has NAT to go onto the big bad internet, so instead of Fred’s IP, she puts her own external IP address into the packet and adds some reference material into her memory so she knows that returning packets will have to go to Fred.
However, when Alice finds out that the external IP address is from Lucy when she wanted to see the website that Peter promised, she hits the panic button and tells Lucy to leave her alone, and tries to talk to Peter again – ignorant of the fact that Lucy was just relaying the same website.
Solution 1
Get a very expensive router that has 2 seperate WAN ports that remembers which way a certain stream of packets was going and sends things back on the same path. Unfortunately I don’t have one of those, so I can’t tell you how that exactly works, nor if it actually does work.
Solution 2
Get a third router and set it up to serve with a not so very logical NAT configuration.
From the example given you can see one occasion where a router does actually remember information about previous packets. Lucy masks her outgoing packets to pretend the packets are coming from her external IP address instead of an internal IP address. And you can abuse this exact mechanism to force webserver Fred to send his packets to the right router.
How that exactly works:
- outside has an external IP
- inside has an internal IP
- NAT masks packets going from the inside to the outside
outside outside
Peter Lucy
inside inside
| |
| |
| 192/168.0.0/24
| |
192.168.2.0/16 |
| |
| |
inside |
Markus |
outside |
| |
\ |
\ |
192.168.0.10
Fred
When Alice requests a website via Lucy’s outside IP address, Lucy directly sends it to Fred, and Fred has Lucy has his default router and thus sends it back through that route.
When Alice requests a website via Peter’s outside IP address, Peter wants to send it to Fred, but he can’t find Fred. Markus however tells Peter to just give it to him, and he’ll give it to Fred. Then Markus masks Alice her IP address with his own outside IP address, and sends the packet to Fred. When Fred wants to send something back, he thinks Markus sends the packet, and just sends it to him. But Markus checks his memory and remembers he should send it to Alice. All Markus needs to know now is that he needs to send things through Peter to get to Alice.
The downside
The unfortunate by-product of using a third router is that you can’t connect the 2nd internet connection directly to your local network, because then traffic wouldn’t be masked. So to use the 2nd internet connection as a Backup internet gateway, you’ll have to use a 4th router with a very careful routing scheme to not conflict with the internal nat router. I haven’t tried that out yet, but I have another Cisco 1841 lying here I might give it a go with as soon as I can get my hands on another switch…
Add comment February 22, 2009
Linux adventures
(times are fictional, but might as well be accurate)
15:00 – Backing up data from NTFS partitions to a Linux server with a 0.5m utp cable on 1Gbps
15:45 – Noticing the Xeon cpu is actually 64bit, downloading CentOS 5.2 NetInstall iso and burning it to a cd
16:00 – Rebooting and setting up Raid0 for 2 of 3 disks
16:10 – Figuring out the syntax of the HTTP-path I needed to download the images from
16:20 – Manually setting up the partitions and then forgetting to check the grub settings
17:00 – Grub errors, googling around
17:30 – Getting kernel to load and then some errors and panics
17:45 – Rebooting and reinstalling with automatic handling of partitions
18:15 – Dinner
18:30 – More errors and panics
19:00 – Reinstall with some more different things
19:30 – Hopeless
20:00 – Disabling Raid
20:10 – Reinstall with manual partitions
21:00 – Install is still busy with formatting 3 harddisks
21:30 – Succesful boot, copying some things and trying to install VMWare-server
23:00 – XEN Kernel is hopeless, reboot
23:10 – Installing normal kernel, setting up grub and rebooting
23:20 – Dowloading new VMWare software because I needed the 64bit version
00:30 – Installing VMWare – Huray, configuring
00:45 – Copying back 1 VM… the slowest 1Gbps connection I’ve ever seen…
(from here the actual times)
01:20 – Still copying… and googling how to install and start a VM without the web-interface
01:35 – Falling asleep whilst trying it with WGET instead of gftp… eta 2 minutes, but the connection keeps stalling…
01:38 – Still on 2 minutes…
01:45 – Still 2… And I still need to transfer some other files
02:15 – Fingers crossed – going to try and run the VM…
02:46 – Oh, there is a web-interface… nmap was just hiding the ports
02:47 – First VM running
Add comment February 9, 2009
Cisco purchases for dummies

I have a modest Cisco Small Business Router C850 series router for over two years now, and I’m fairly happy about it. However when I tried to find IPv6 commands on it, I stumbled upon things like “what’s this ipv6 thing you’re talking about?”. Ignorant as I am about these things, I figured out on Cisco.com that the 850 series doesn’t support IPv6 in general. So when I finally decided to get a 1841, I guessed that it would support it out of the box.
The thing about Cisco is that they’re selling their operating system IOS under different versions and feature sets. And the standard 1841-BASE feature set, doesn’t know anything but simple IPv4 routing.
When you do some research via comparing these feature sets on the feature navigator you can conclude that only the Feature sets: Adv. Enterprise Services and Adv. IP Services actually contain IPv6 capabilities. Neither IP Base or Advanced Security has IPv6.
Being a simple consumer with no real way to upgrade my license or software, I had to buy a new 1841 with Adv. IP Services, which is about 1000 euro’s more than the standard model.
Add comment February 5, 2009
Delphi Intermezzo
Delphi example to communicate to a Sharp Aquos LCD-Television via RS232.
Link.
Add comment February 1, 2009
A fix
In my last post I told my fellow devs they should push the DBX-crap of the year to the side and just write your own wrapper.
But naturally, it was still itching as I even got a MS-SQL db connection to work. And it seems to be surprisingly easy: DBX wants MySQL4, and we’ll give it to him. With a DLL from the good ol’ mysql server 4.0 installation DBX will be fooled in a false sense of ease while you secretly have a connection to you MySQL 5 server.
Question is; how long is this going to last, at what point is it going to crack into a ‘ohno I got something unexpected from the server’-panic situation. Well, we’ll see.
Add comment December 23, 2008
Delphi 2007/2009 MySQL Madness
So I recently purchased Delphi 2009, and I have been working with Delphi 2007 occasionally elsewhere, and as always the third party libraries like Indy and DBX are as messy as can be.
Indy is never up to date (plus you keep writing thread-wrappers around the functions), and DBX contains a MySQL driver that doesn’t support MySQL 5.0 and up for some reason. With DBX you can insert, create, update and delete all you want – but a select query is too much for the library. When you select more than 1 field, using either * or the fieldnames, you get a nice access violation when you open the query. Don’t ask me why it does that, because it makes no sense whatsoever since No functions concerning queries, fields and values have changed since MySQL 4 (perhaps even earlier).
So we try to look up some other Delphi/Object Pascal libraries to work with MySQL. A simple rewrite of the C-API header files comes to mind (happily named mysql.pas) that dates from 2002. There are however a few hickups; Delphi 2009’s PChar is a PWideChar by default now. And honestly, who needs 2 byte chars when there’s a little thing called UTF-8. So that and some other stuff makes it quite annoying to adapt it.
And no, we’re not going to use the MySQL ODBC driver, because ODBC is the most useless system ever since you need to configure it on every client if you want to use it. I know that some people would like to believe Databases should only be run locally with fancy internetworking interfaces like SOAP or a custom thing, but sometimes that’s just not worth the trouble.
So what do we do when we are faced with overpriced libraries and useless alternatives? We write our own DLL in C/C++, define an easy to use interface, and dynamically link it against your application with a simple Delphi file.
Here’s a simple version to demonstrate it’s not rocket science.
ps. to create a valid lib to link against with MingW (instead of the default MSVC lib files that MySQL comes with), from the mysql\lib\opt directory type:
- prompt:> [yourmingwdir]\reimp -d libmysql.lib
- prompt:> [yourmingwdir]\dlltool -k -A -C -d libmysql.def -l libmysql.a
Another fix is to install the mysql version 4 dll libraries from the mysql.com website, but who knows what that kind of errors will produce in the longrun…
Add comment December 16, 2008
Cisco IOS DHCP Manual bindings for dummies
So I was screaming and ranting (not really, but dramatic effect always works… right?… what?.. it doesn’t? hmm…)
I use a Cisco 385 router (or is it 580, 850, 857, I can’t remember) to keep my network together and connect it to the outside world. It does a great job at most trivial things (except for IPv6 which is non-existant, and thus it will soon be replaced by a 1841 probably) and I have fun forwarding ports and whatnot.
The thing that however puzzled me was the fact that manual IP/DHCP bindings for computers in my local network were sometimes accepted, and sometimes not. I had “read” (as in read fast and inaccurate) the manual and came up with simple entries like:
ip dhcp pool afancyname host 192.168.0.10 255.255.255.0 hardware-address 0012.3456.789a client-name afancyname
And after fiddling around with settings in Windows for weeks, I finally started actually reading the manual and stumbled upon the difference between “hardware-address” and “client-identifier”.
The big difference (next to a simple 01 prefix to the mac-address) is that hardware-address is for BOOTP, and client-identifier is for DHCP. Client-Name is also BOOTP only, and should not be used.
So I looked up the Client-identifier via “sh ip dhcp bindings” and wrote the following config bit instead:
ip dhcp pool afancyname host 192.168.0.10 255.255.255.0 client-identifier 0100.1234.5678.9a
Et voila, release and renew your IP address client-side, and you have your preferred IP address.
However, ofcourse this is only because Windows supplies the client-identifier argument with its DHCPRequest packet, and Linux for some reason does not. So for Linux hardware we need to fallback on hardware-address (unless you manually edited your dhclient.conf file it seems):
ip dhcp pool myfancylinuxpc host 192.168.0.11 255.255.255.0 hardware-address 0012.3456.789b
Add comment December 6, 2008