Category: Programming

Ideone – compiler pastebin

A friend of mine, just tossed me this link.. http://ideone.com/

What makes Ideone different from any other pastebin is its compiler feature; Supporting about 20 programing language, you will be able to live compile (of course with some limitations) your code.

Let’s find out the perl version running on that server

print $];

and result?

result: success
time: 0s    memory: 3248 kB    signal: 0
input: no
output:

5.008008

Ok… not the 5.010000 I excepted but not too obsolete either.

Have fun,

Denial of Service in PHP

It was about time, that the PHP team finally included a max_file_uploads directive to limit the number of file upload per request (default is of 20). (cf. http://www.php.net/ChangeLog-5.php#5.3.1)

Until PHP 5.3.1, it was possible to send an X number of file upload request thus creating an X amount of temporary file on the targeted system.. which would cause the web server to crash and the system to overload.

PHP-suhosin has already a max upload option “suhosin.upload.max_uploads” (default to 25), therefore systems with the suhosin patch are protected.

Recommendation is to apply the 5.3.1 release with the patch provided by PHP or to disable file_uploads in php.ini if not using file upload. Keep in mind, you do not need a file upload form on your site to not be vulnerable… all it takes is sending a multipart/form-data mime type to the php script as defined in RCF 1867

For those who are subject to dynamically assigned DSL IP, you would probably be familiar with a site such as http://myip.dk

Here is a small script I use to fetch my public IP for other script processes

updated “due to changes on the site myip.dk, I rewrote the script”

#!/bin/bash

link=`lynx -dump -listonly ‘http://myip.dk’ | awk -F: ‘/myip.dk/ && $0 != “” { getline; print $0}’ | awk -F ” ” {‘print $2′}`
curl -s $link | grep ‘”Box”‘ | egrep -o ‘[0-9.]+’

curl -s en.myip.dk | grep ‘”Box”‘ | egrep -o ‘[0-9.]+’

You may run it in a cron and email you your IP whenever it changes from the last fetch one and update your firewall rules accordingly…

Cheers,

Wrap your shell commands

One of the most annoying matter I have met when administrating a server was following and fixing messes other users (who happen to have root password) would do on the server.

Random users with root passwords often know two things… “sh” and “history -c”… and of course “I didn’t do it”.

Now, while it is important to keep logs of activities on the server, it is even better to be able to pull up logs of every single commands entered plus their arguments.

Here is a little C wrapper once can use to wrap /bin/sh so that every commands gets logged.

Now, we first need to backup our current sh executable…  so

mv /bin/sh /bin/shb


#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define LOG_FILE     "/var/log/sh.log"
#define LOG_FLAGS    O_CREAT|O_APPEND|O_WRONLY
#define LOG_MODE     S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
#define WRAPPER "/bin/shb"

int main (int argc, char **const argv)
{
FILE *        logfile         = fopen (LOG_FILE, "a");
const char *  program         = argv[0];
argv[0] = WRAPPER;
if (logfile)
{
struct passwd *pw         = getpwuid (geteuid ());
int            i          = 0;
fprintf (logfile, "(%s)", pw->pw_name);

for (i = 0; i < argc; ++i)
fprintf (logfile, " '%s'", argv[i]);

fprintf (logfile, "\n");
fclose (logfile);
chmod (LOG_FILE, LOG_MODE);
}

execv (argv[0], argv);

exit (EXIT_FAILURE);

}

Now, compile your code into an sh executable.

Move your newly compiled sh into /bin/

And voila :) … whenever someone executes /bin/sh… a log will be generated in /var/log/sh.log

There are numerous programs such as monit which are widely used as to monitor processes and take certain actions in case of different events.

Here is a little tip as to quickly monitor an service/process if you aren’t wanting to go through the hassle to configure monit

#!/bin/bash

r=$(ps cax |grep -c NAME_PROCESS)

if [ $r -eq 0 ]; then
service NAME_PROCESS restart
echo “NAME_PROCESS has crashed”  | mail -s “monitoring SERVERID” your_Email_Address
fi

Save this as an executable, set up a cron and voila :-)

Good luck,