Archive for June, 2010

PHP can do anything, what about some ssh?

Last time I already tried to prove PHP can do anything when it comes to network protocols by implementing a DNS server. This time I’m doing it again with a server-side implementation of the SSH2 protocol.

You probably know SSH at least by its name. It’s a of secure telnet replacement which also allows many other things such as port forwarding, remote file management (with sftp) and more.

With PHP I could write a fully working SSH server in only 3 days. Of course I didn’t implement every single extension there is to SSH, but I’ve implemented:

  • SSH2 protocol only (no SSH1, anyway who uses that anymore?)
  • Encryption protocols: aes128-cbc,blowfish-cbc,serpent256-cbc,cast128-cbc,3des-cbc (via mcrypt)
  • Message digests: hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96 (via hash)
  • No compression as I cannot easily keep a compression context active (the gzip extension in php is missing a way to create a compression context)
  • Password and public key (ssh-dss and ssh-rsa) identification
  • Ability to program an interactive shell in PHP (there are send and recv functions in a separate class, anyone can have some fun and write something out of that. Should be possible to make a wrapper to communicate with a shell launched with proc_open)
  • Support for multiple channels
  • SFTP subsystem
  • Can be easily extended to add support for custom channels or re-use the ssh protocol for something else

My goal when writing this was to provide a replacement for the FTP protocol for the customers of my hosting service. FTP has many drawbacks, including no encryption (you can with ftps or ftpes) and the way ftp transmits data (another connection has to be opened on a different port, leading most of the time to some problems for people behind a NAT and/or firewalled servers).

With this ssh server supporting sftp, I finally got the replacement I was looking for. Of course it uses more CPU than a C ssh server (about 3 times more) but the difference isn’t that big. Next steps will include fork()’ing to open channels (will allow the SFTP server to chroot) and maybe support for some SSH extensions.

To implement the SSH protocol the following PHP extensions were used:

  • OpenSSL: used to generate secure bits when negociating the key, and used to generate the host signature on connection. I was hoping to use openssl_verify() to verify the key used when logging in, but I couldn’t manage to convert the ssh-rsa key to something openssl would understand, so I re-implemented signature verification with gmp.
  • MCrypt: The ssh protocol is encrypted (usually with something like AES128). mcrypt has the required functions to handle encryption in block mode
  • Hash: each packet transmitted over SSH is optionally signed with a HMAC signature. In order to generate and verify those signatures I used hash_hmac()
  • And finally the most important: GMP. As I was missing some functions to properly handle the initial Diffie-Hellman key exchange (and later to implement publickey authentication) I had to re-implement those in PHP. Of course working with 1024 bits integers is not something we can use the native int type for. GMP (and bc) allows such calculations (and I used them). I was missing the ability in gmp to read from/convert to binary values, so I had to add the use of bin2hex() and pack(‘H*’, …) to be able to work with binary values easily. GMP computations are only used when negociating keys (the ssh rfc recommands doing this once an hour, or every gigabyte of data transmitted) or when using the publickey authentification.

What did I create a ssh server for? The same thing I created a DNS server for fun and for KalyHost. In order to provide services updated in realtime I wrote a database-backed dns server a while ago, and now a ssh server (which can be database-backed too by extending the “Base” class).

The sourcecode can be downloaded from the SVN: http://ookoo.org/svn/pinetd2/trunk/code/classes/Daemon/SSHd/ this depends on pinetd2, a framework I wrote which allows to easily create daemons in PHP, and which I already used to create various things (FTP, Mail server, etc).

People willing to help on pinetd2 (code and/or documentation) are welcome. If you do not mind being called crazy because you make something else than webpages in PHP, you can contact me by mail or on IRC (or by leaving a comment on this post too if you wish to).

Tags: , , , ,

MagicalTux vs. Microsoft – VICTORY !

A while ago I posted about moving in another country with a XBox 360. At this time it was cool since I still had a french credit card, but when I closed my bank account in France, things went somehow awry. I had to buy a 12 months xbox live code on eBay France to get my account working since you cannot add a japanese credit card on a french xbox live account…

However microsoft had tried to bill my french CC once more and it failed. Of course it’ll fail but for some reason Microsoft has a weird billing scheme. If it tries to bill but it fails, it will still give you what you tried to pay, then it will bug you every now and then to get your money. This is really stupid (they could just don’t give anything) or really brilliant (they can bill stuff you don’t want then try to get your money claiming you asked for that).

Anyway I had my account expire 12 months later so I didn’t care much. At some point I got annoyed by all those emails so I asked the microsoft support which replied they couldn’t help me with their tools, and told me I should contact phone support…
I just decided to ignore those emails until… until microsoft blocked my account. Yes, they just decided to suspend my account because they couldn’t get the money for 1 month, and aren’t intelligent enough to just remove one month from my current billing period… So I had an account expiring in some 10 months suspended because they couldn’t bill one month…

I then tried to add (again) my japanese credit card to see if I could re-activate my account (I really want to play a game right now to cool off a bit, and I hate it when some american based company tries to stop me) and it WORKED! Yeah! Somehow I could add a japanese credit card to a french xbox live account. I’m not sure if this is a glitch or just that microsoft suddently decided that you could want to move to another country without losing all your achievements, but yes, it worked!!!!

So here I am, finally resolved this problem with Microsoft. I posted about my success on the xbox live forums (this thread, page 14) so we’ll know if this was just a glitch, or if microsoft became intelligent.

Tags: , ,

BP, “those morons”

Hey, it’s been a few weeks that the Gulf of Mexico oil spill is spilling happily while weak humans are trying to stop it. You all know about this, I don’t have to make a drawing nor anything.

Now, you probably know James Cameron, director of movies “Avatar” and “Titanic”, the guy who is able to say that BP are “morons [who] don’t know what they’re doing“. Of course Mister Cameron is an expert in the field of oil spills, especially when they precisely happen in the Gulf of Mexico.

Hey no, he’s not ?

Well, who cares anyway. He’s not, and hopefully nobody will ever allow him to try anything. That might be exactly what he wants. Imagine: BP tries, BP fails again, oil spill grows bigger. Mister Cameron then says “Hah! I told you, if only you let me help you we wouldn’t have come to this”… Hell yeah, this way you’re the oppressed hero of the day, you just had to show off your submarines and show how intelligent you are.

Anyway the chances a movie director, whose job is to direct movies, would be allowed to work on the oil spill, which potentially means the death of many local industries, birds, etc… are really really slim. Somehow I hope they’d allow him to work on that on condition he kills himself would he fail. Don’t call people who are actually doing something morons when your only idea is to bring your 23 friends for a party where you won’t have much chances to find a better idea than BP who have experts whose job is to know oil, oil wells, and deep water conditions (and if you do, just give them your idea, you’ll be the great savior from darkness who saves the world without even having his name known… oh, that wasn’t your plan?)

Tags: , , , , ,