>FreeBSD + VirtualBox + RoR = nice, easy development environment :-D

>

So the new St. Noble security intern, Matt, has a hard-on for FreeBSD and has been trying to convince me to use it. Though I like to give him flak about it, I do actually enjoy the simplicity that it has to offer, and the delightful lack of a GUI – call me old-fashioned 🙂 [no, really, I do actually love a nice simple text interface, especially when I’m coding. Visual Studio, Eclipse, all those IDE’s are nice and all with their integrated file management, library browsers and auto-completion modules but when I code rails, I like using a simple text editor with nothing but the most basic syntax highlighting]  
I’ve found that, amongst other things, FreeBSD is great to use as a lightweight Ruby on Rails development environment, especially when combined with VirtualBox. If you own an IronKey, you can set up PortableVirtualBox and get it up and running quite easily.
Anyway, this is a simple guide to setting FreeBSD as your development environment. It’s not very in-depth, but I hope it will provide you enough tips and tricks to set yourself up without committing harakiri or throwing your machine out the window…
Installation:
When you’re asked to do the partitioning, start by using auto-partition; take note of the different sizes, then adjust your swap space so that you can do a bit more swapping – I would recommend 1 GB instead of the de-facto standard. Choose a development installation on FreeBSD, and install the ports collection. Apart from that, follow the wizard. Pretty simple, huh?
I would recommend setting up sudo, but that’s not 100% necessary since your box is effectively sectioned off from all other computers. If you want to do that, then run ‘pkg_add -r sudo’. Remember to edit your /etc/sudoers file and add your regular username, and optionally prevent root from logging in via SSH by editing your /etc/ssh/sshd_config file.
VirtualBox setup:

Use a basic FreeBSD VM template, with two virtual NIC’s: set your first NIC up to be on the host-only network, and your second NIC to be on the NAT network. This allows you (and only you) to connect to your dev environment via SSH or web whilst providing the VM with a means to access the ‘net (especially practical if you’re consuming web services).

SSH and bash

By default, freeBSD uses csh. I’ve tried using tcsh instead, doesn’t seem to work for me; bash, on the other hand, works fine.  To install that, execute ‘pkg_add -r bash’ and wait until it’s installed. You can run it manually thereafter by executing ‘bash’ whether you’re at the console or remotely logged in via SSH. Ideally, though, you’d probably want your shell to be bash as soon as you start, right? Here’s the command to do that: ‘chsh -s /usr/local/bin/bash username‘, where /usr/local/bin/bash is the path to bash (so before executing the command, be sure to run ‘which bash’ to double-check the path) and username is your actual username. You must then edit your /etc/passwd file and substitute your shell for /usr/local/bin/bash – it’s the last field on the line with your username on it. You must both execute the chsh command and edit the passwd file in order for the switch to work! Kudos to vivek for the nice, easy tutorial that I didn’t read completely the first time like an idiot. If you follow it to the letter, this will work for your console, SSH, and will allow you to use SCP (there’s another way to set up bash on SSH, and that’s to use the ForceCommand directive in sshd_config – but that messes with your ability to use SCP).

Ruby, Rubygems, and Rails

No way around it – you’ve gotta compile the sucker; everything you need should be accessible via http://rubyonrails.org/download. Shouldn’t be too hard though: uncompress the tarball, run the usual ‘./configure; make; make install’. Download the rubygems tarball and install it using ‘ruby setup.rb’. Finally, run ‘gem install rails’.

Nano: syntax highlighting

A final note: if you are a fan of nano like me and you would like syntax highlighting, you can create a .nanorc file in your home folder and use the following example as a starting point: http://code.google.com/p/nanosyntax/source/browse/trunk/syntax-nanorc/ruby.nanorc

>An ubuntu install script

>

Wrote a simple little script this morning to install all the software packages I might need for ruby development (plus a few security tools).  Hopefully it will serve someone other than me 🙂
I know, I know…  You can’t generalize and install some set of packages without knowing what they are.  That’s not the linux way.  On a production server, I’ll always perform a manual setup and, when I can, I compile from source rather than use packages.  This particular script is suited for a dev machine.
Note that, in the very beginning, I set up a few version variables.  You should be able to just set these and then fire up the script.
Caution: I’m providing this script as I use it, on a non-production, fresh install of a linux desktop environment. You can do whatever you want with it; but if you’re dumb enough to run this on a production server without checking it out in detail first, and it breaks your prod environment, don’t come complaining to me — I’ll hurt you, man! 😉
And now for the code:
#!/bin/bash

#This script assumes that you’re running ubuntu 10.4 32-bit. For the metasploit, ruby enterprise and flash packages, you’ll definitely need to change the packages downloaded!

if [ “$(whoami)” != ‘root’ ]; then
        echo “You have no permission to run $0 as non-root user.”
        exit 1;
fi

#Set a few variables here:
metasploit_version=3.4.1-linux-i686
ruby_version=1.9
ruby_enterprise_version=1.8.7-2010.02_i386_ubuntu10.04
gem_version=1.8
passenger_version=2.2.15
flash_version=10_linux

echo ************************** Installing basic packages: **************************
apt-get install -y build-essential subversion vpnc network-manager-vpnc libreadline5-dev

echo ************************** Installing forensics packages: **************************
apt-get install -y ewf-tools sleuthkit registry-tools hfsutils squashfs-tools
echo ************************** Installing security packages: **************************
apt-get install -y snort flow-tools aircrack-ng ettercap-gtk python-scapy wireshark tcpreplay ghex openvas-server openvas-client nmap zenmap

echo ************************** Setting up metasploit **************************
wget http://www.metasploit.com/releases/framework-`echo $metasploit_version`.run
chmod +x framework-`echo $metasploit_version`.run
./framework-`echo $metasploit_version`.run

echo ************************** Installing software development packages: **************************
apt-get install -y ruby`echo $ruby_version` ruby`echo $ruby_version`-dev libopenssl-ruby rubygems mysql-server meld

echo ************************** Installing web server packages: **************************
apt-get install -y apache2 apache2-prefork-dev libapr1-dev libaprutil1-dev

echo ************************** Removing mysql-server autostart **************************
update-rc.d -f mysql remove

echo ************************** Removing apache autostart **************************
update-rc.d -f apache2 remove

echo ************************** Setting up ruby enterprise **************************
wget http://rubyforge.org/frs/download.php/71100/ruby-enterprise_`echo $ruby_enterprise_version`.deb
dpkg -i ruby-enterprise_`echo $ruby_enterprise_version`.deb

echo ************************** Setting up passenger **************************
/usr/local/lib/ruby/gems/`echo $gem_version`/gems/passenger-`echo $passenger_version`/bin/passenger-install-apache2-module

echo LoadModule passenger_module /usr/local/lib/ruby/gems/`echo $gem_version`/gems/passenger-`echo $passenger_version`/ext/apache2/mod_passenger.so > /etc/apache2/mods-available/passenger.load
echo <IfModule mod_mime_magic.c> > /etc/apache2/mods-available/passenger.conf
echo PassengerRoot /usr/local/lib/ruby/gems/`echo $gem_version`/gems/passenger-`echo $passenger_version` >> /etc/apache2/mods-available/passenger.conf
echo PassengerRuby `which ruby` >> /etc/apache2/mods-available/passenger.conf
echo </IfModule> >> /etc/apache2/mods-available/passenger.conf

echo ************************** Getting Flash Player **************************
wget http://fpdownload.macromedia.com/get/flashplayer/current/install_flash_player_`echo $flash_version`.deb
dpkg -i install_flash_player_`echo $flash_version`.deb

echo ************************** cleanup **************************
rm examples.desktop install_flash_player_10_linux.deb framework-`echo $metasploit_version`.run ruby-`echo $ruby_enterprise_version`.deb

Here’s a sample apache config (taken straight from phusion’s installer…):
   <VirtualHost *:80>
      ServerName http://www.yourhost.com
      DocumentRoot /somewhere/public 
      <Directory /somewhere/public>
         AllowOverride all
         Options -MultiViews
      </Directory>
   </VirtualHost>

>A (very) simple ruby script to add files to your repository

>You’re developing a ruby on rails site and, as a dutiful developer, you’ve set up versioning on your code. As you probably already know, when you generate a new object there are quite a few files that get created — so every time you want add a new model, you have to type that “svn add [filename]” command… A bit boring if you ask me.

In Windows, all you really need to do is use a graphic interface like TortoiseSVN; it’s a bit clunky at times but it definitely does the trick. In Mac OS X and Linux, the most powerful and flexible tools are already at your disposal in your command line — so why go through the fuss of a GUI?

I’m sure there are plenty of elegant, easy solutions to this — but here’s mine:

  • Open up your favorite text editor
  • Copy &Paste this code into it:

#!/usr/bin/ruby

delim = “? “

results = `svn stat | grep #{ARGV[0]}`.split “n”

results.each do |result|

`svn add #{result.gsub delim, “”}`

end

  • Save the file in, say, the same directory as the project that you have under SVN (don’t save it in the project directory — that wouldn’t be clean). I call mine svnaddfiles.rb
  • For convenience, make that file executable with a “chmod u+x svnaddfiles.rb”
  • From your project directory, you can use it like this: “../svnaddfiles.rb [file prefix]”

Voilà — easy adding of many files, using just ruby, svn and grep!

>Unicode in Ruby with SQL Server

>I’ve been using Ruby on Rails a lot recently, and in one instance I found myself in a bit of a pickle: using UTF-8 fields with SQL Server 2005. After a bit of googling and a lot of playing around, I finally found a solution that seems to work; figured that someone out there might find it useful…

Here’s the environment in which I tried this out:

Here are a few things I tried beforehand and which didn’t work:

  • Installed mssqlclient (had high hopes for this one but I kept getting errors about the adapter not existing. If anyone’s got some insight here, please let me know and I’ll add your URL or text to the article :-D)
  • Tried altering the column types (biggest problem here was with the constraint. Yes, you can drop the constraint but I found that it beat the purpose of using rails’ scripts to generate the data model because every time you’d want to drop the column you’d have to edit your migration and plug in the constraint’s unique name… Yuck.)

You’ll also note that, according to rubyonrails’ wiki on using unicode strings (http://wiki.rubyonrails.org/rails/pages/HowToUseUnicodeStrings), you don’t need to change much of your application’s code to switch to UTF8. As a matter of fact, I simply performed the necessary tests from script/console and realized that $KCODE and ActiveSupport:MultiByte were already set / installed.

My problem actually occured when my migrate scripts were generating the data model: instead of generating the utf8 fields nvarchar and ntext, it was creating varchar and text. If your environment is already set up to support utf8, the only thing you need to do is modify the sqlserver_adapter to generate the correct fields.

This is how it’s done:

  • Navigate to C:rubylibrubygems1.8gemsactiverecord-sqlserver-adapter-1.0.0.9216libactive_recordconnection_adapters (assuming that you’re using the default installation paths)
  • Save a copy of sqlserver_adapter.rb (duh :-D)
  • Edit the sqlserver_adapter.rb file and locate the native_database_types method.
  • Change ‘varchar’ to ‘nvarchar’ and ‘text’ to ‘ntext’ – Save the file
  • Regenerate your data model (yup, you’ll need to delete your existing database). If you’ve already got production data in your database, you’re most certainly going to want to backup your data, restore it to another database and migrate data using ETL’s (hand-rolled or SSIS…) Best of luck!

A few last things:

  • In your database.yml file, make sure you’ve got this line in the profile:

encoding: utf8

  • In the ApplicationController class, you’ll need to define a before_filter method to set the correct headers; open up the application.rb file and set up a before filter like this:

before_filter :set_charset

def set_charset

#Sets the response headers to UTF-8. Alone, this doesn’t do squat: 

response.headers[“Content-Type”] ) “text/html; charset=utf-8”

#Sets the code page; very important…

WIN32OLE.codepage = WIN32OLE::CP_UTF8

end

Many thanks to Andres Mondano Pellegrini for the code. You’ll find his article, which saved my ass, here: www.gaugeus.com (if you’re having trouble finding it, perform a search on “Unicode Support in Ruby on Rails with MySQL and Microsoft SQL Server”)