Posts Tagged ‘programming’

fail2ban(ning) fun!

Tuesday, May 5th, 2009

The hard drive on my development server was failing last week so I had the fun job of getting the data off of it while I was still able to.  The process of getting all my data and installing Ubuntu 8.04.2 LTS on my new hard drive took the better part of two days.  After getting the server back to a solid state from which to build my LAMP stack on, I decided it would be a good idea to follow standard security procedures and install fail2ban to block any unintelligent hackers.  I think any hackers worth their respective salt can get around a basic implementation of fail2ban, but this is just my dev server and I don’t have any sensitive data on it, so I’m not too worried.

Today I was toiling away on my company’s website, updating, fixing, etc…  I was attempting to upload ~7 MB, 1312 files onto the server and my ftp client, filezilla, crashed.  I figured it was a one-time crash, so I reloaded filezilla and tried again, crash again.  I tried one more time just to make sure I was doing everything correctly.  Crash again.  I believe the issue revolved around the number of files I was transferring at once, so I tarballed the files and pulled filezilla back up and….couldn’t log into the server with filezilla.  After a moment of troubleshooting I realized I couldn’t log into the server via filezilla or putty (I sftp through port 22).  I was able to login, with my same username/password through putty AND filezilla through the public ip address though.

After digging through the log files and ifconfig to make sure my server wasn’t dying again I pulled up my iptables.  Yep, I was banned.  Apparently when filezilla was crashing there was some sort of login attempt made to the dev server and each login attempt failed.  So after my three attempts to upload my files, I was banned.  Lesson learned, always have two access methods to any server.  It makes troubleshooting issues much easier.

BTW, for reference the commands for iptables is:

list all banned ip’s

iptables -L -n -v
Chain INPUT (policy ACCEPT 78238 packets, 18M bytes)
 pkts bytes target     prot opt in     out     source               destination
 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 
Chain OUTPUT (policy ACCEPT 83695 packets, 66M bytes)
 pkts bytes target     prot opt in     out     source               destination
 
Chain fail2ban-ssh (0 references)
 pkts bytes target     prot opt in     out     source               destination
   36  3456 DROP       all  --  *      *       10.0.55.4            0.0.0.0/0
   22  1732 DROP       all  --  *      *       81.208.51.90         0.0.0.0/0
41764 7846K RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

list all banned ip’s for a specific program (fail2ban-ssh) with line numbers

iptables -L fail2ban-ssh -n -v --line-numbers
Chain fail2ban-ssh (0 references)
num   pkts bytes target     prot opt in     out     source               destination
1       36  3456 DROP       all  --  *      *       10.0.55.4            0.0.0.0/0
2       22  1732 DROP       all  --  *      *       81.208.51.90         0.0.0.0/0
3    41764 7846K RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

remove banned ip from iptables

iptables -D fail2ban-ssh 1[line-number]

Till Next Time

returning a file in symfony

Friday, March 6th, 2009

At work I am working on a complete redesign of my company’s website and we are using symfony as the php framework.  Since I am still fairly new to symfony I had to google how to return a file download instead of the MVC returning the template.

I found my solution here.

My code is very similar but for the fact that I’m not creating the file before sending it back, I am just querying the database and returning the file at the location specified in the database.  My code looks something like this:

public function executeViewEvent(sfWebRequest $request)
 
{
     $c = new Criteria();
     $c->Add(ActivitySeismoInfoPeer::ID, $request->getParameter('id'));
     $this->event_file = ActivitySeismoInfoPeer::doSelectOne($c);
     $this->forward404Unless($this->event_file);
     $file = $this->event_file->getAsiShotReport();
     $new_filename = substr($file, 8);
     if($file)
     {
          $this->getResponse()->setHttpHeader("Content-type", "application/pdf");
          $this->getResponse()->setHttpHeader("Content-Disposition", "attachment;filename=\"$new_filename\"");
          $this->getResponse()->setContent(file_get_contents("/var/www/web$file"));
          return sfView::NONE;
     }
 
}

I’ve taken care to forward to a 404 if there are no results and to return an error message in the success template if there is a result from the database, but there is no file specified in the asi_shot_report field.

So this has been my first installment of documenting any weird code that I might need later.

Till Next Time