How to Vyatta firewall

By Nisse Pettersson at March 16, 2010 11:13
Filed Under: Technical
Share on Facebook

When ever i'm in need of a software firewall / router I use Vyatta.  A advanced firewall and still rather easy to setup. I'm going to have my latest setup as an example. I'm setting up a development enviroment for deployment of Windows7 and WindowsXP. I'm using the latest version of VmWare vsphere 4.

Setup

Download the ISO from the download section. Then you need to create a new virtual machine and give it some diskspace and ram. I gave it 200mb of disk and 128mb of RAM. This is up to you and what type of load is expected. Remember to add two network interfaces and set the type to e1000. One for outside and one for inside.

Start the machine and mount the ISO you downloaded.

When the system boots you will be greeted with the text

vyatta login:

Logon as root and with vyatta as password.

We now need to install vyatta on the diskdrive instead of the ISO. So type the following.

install-system

 Follow the instructions on the screen. Now the fun parts starts. Now we need to go in configuration mode(ie. enable in Cisco CLI) Write the following.

vyatta@vyatta:~$ configure
[edit]
vyatta@vyatta# set interfaces ethernet eth0 address 192.168.1.81/24
[edit]
vyatta@vyatta# commit
[edit]
vyatta@vyatta# exit
exit
vyatta@vyatta:~$

This will set the first network interface(eth0) to have address 192.168.1.81 with netmask 255.255.255.0
Then commit will save this to the running configuration. Exit will leave the configuration mode.
We now need to set up the inside interface. Use the syntax above but change the IP and eth0 to eth1.
Vyatta firewall should now respond to pings from computers on the same network and in the same subnet.

NAT

set service nat rule 1 outbound-interface eth0
set service nat rule 1 source address 10.1.1.0/24
set service nat rule 1 type masquerade

This will is a generic NAT route that sends all traffic that's outbound(not in the same subnet) to the outbound-interface. And we tell it that outside interface is eth0. This will only apply if the source network is 10.1.1.0/24(our inside network)
I like PuTTy and SSH in general so I will add a second NAT rule for SSH.

set service nat rule 2
set service nat rule 2 type destination
set service nat rule 2 inbound-interface eth0
set service nat rule 2 protocol tcp
set service nat rule 2 destination address 0.0.0.0/0
set service nat rule 2 destination port ssh
set service nat rule 2 source address 0.0.0.0/0
set service nat rule 2 inside-address address 10.1.1.1

Don't forget to save you changes to the running configuration.

commit

 

Then you need to save this to the statup config, this is done with the following command.

save
Saing configuration to '/opt/vyatta/etc/config/config.boot' ...
Done

 

You now have a vyatta firewall running as a router. There is a lot of things you can do with this so I suggest you read the documentation as I do. Will return to this topic when I have something interesting to write about.

Technorati-taggar: ,,,


Incremental Linux backup to FTP-server

By Nisse Pettersson at January 30, 2010 04:16
Filed Under: Technical
Share on Facebook

I’m running a Macedonian e-date site (www.ergeni.com.mk) as a small project just for fun and it’s running on Ubuntu Machine. I need backup offcourse and found a nice script. I’m not the author of this script and i’m not interested in taking credit for it. It works fine and is fast. Just edit the variables and run the script.

#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/home /etc /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%y-%m-%d")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
### MySQL Setup ###
MUSER="mysql-user"
MPASS="mysql-password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/ergeni"
FTPU="ftp-username"
FTPP="ftp-password"
FTPS="ftp-hostname"
NCFTP="$(which ncftpput)"
### Other stuff ###
EMAILID="nisse@nissesblog.se"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
  FTPD="/home/vivek/full"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
  i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
else
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Backup failed" >>$T
 mail  -s "BACKUP FAILED" "$EMAILID" <$T
 rm -f $T
fi

 

Then you need to add this job to crontab. Run the following command.

crontab –e

 

Add a new line to run the file at midnight.

@midnight /path/to/script.sh >/dev/null 2>&1

 

And there you go, a fully working backup system. To restor all you need to do is to extract the files from the ftp server to the same location.
Download the document below and edit.

backup-job.txt (1,97 kb)



Howto implement google maps and a marker

By Nisse Pettersson at December 09, 2009 05:59
Filed Under: Code
Share on Facebook

I'm developing a site for a customer who wish to have  a map of his stores and a marker as well. Googles new v3 API of google map is what i'm using since it's newer and hade a nice look and feel than v2.

In my application i'm reading the coordinates from a database but for this example we will use static. Paste the following code in your <HEAD> secion.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
 <script type="text/javascript">
  function initialize() {
    var myLatlng = new google.maps.LatLng(<? echo $clsShop->LatLong;?>);
    var myOptions = {
      zoom: 13,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });   
  }


</script>

In your <BODY> tag we will load the map with the onLoad command and unload it when we leave. Use the following tag.

<body onload="initialize()" onunload="GUnload()">

 

Then all you need to do is to create a <div> with id = map_canvas where you wish to place the map. You can use the following example.

<div id="map_canvas" style="width: 220px; height: 220px"></div>

 

Then you're all set and google maps will load. If not you might need sign up for the google API. For more examples you can check out googles own example gallery



Change CD-Key in Windows Server environment

By Nisse Pettersson at November 30, 2009 01:45
Filed Under: Technical
Share on Facebook

If you need to change your KEY for Windows 2008 server there is a nice way to do this.

   1. Open the command prompt and change working directory to System32 directory
   2. Type in slmgr.vbs -ckms (this clears and KMS entry you may have)
   3. Type slmgr.vbs -upk (this removes any product key installed)
   4. Type slmgr.vbs -ipk xxxxx-xxxxx-xxxxx-xxxxx-xxxxx (where the x's is the new product key you want to use )
   5. Type slmgr.vbs -ato (this activates the server)

 

To do the same thing in Windows 2003 server follow these instructions.

  • Click Start, and then click Run.
  • In the Open box, type Regedit, and then click OK.
  • In the left pane, locate and then click the following registry key:
    HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\Current Version\WPAEvents
  • In the right pane, right-click OOBETimer, and then click Modify.
  • Change at least one digit of this value to deactivate Windows.
  • Click Start, and then click Run.
  • In the Open box, type the following command, and then click OK.
    %systemroot%\system32\oobe\msoobe.exe /a
    1. Click Yes, I want to telephone a customer service representative to activate Windows, and then click Next.
    2. Click Change Product key.
    3. Type the new product key in the New key boxes, and then click Update. If you are returned to the previous window, click Remind me later, and then restart the computer.
    4. Repeat steps 6 and 7 to verify that Windows is activated. You receive the following message:Windows is already activated. Click OK to exit.
    5. Click OK.
    6. Install SP1 for Windows XP.

    If you cannot restart Windows after you install SP1, press F8 when you restart the computer, select Last Known Good Configuration, and then repeat this procedure.

     



  • PHP Classes guide - Part 1

    By Nisse Pettersson at November 19, 2009 08:07
    Filed Under: Code
    Share on Facebook

    I'm working as a technician and as a programmer and 90% of my programming is done in PHP which is a great language but some see it as an amateur language. This language is for me a great language to learn and it's also a very capable language. Facebook is a great example of an application running PHP. This guide however is not for beginners since there is already a lot of beginner guides out there. This is for programmers who is ready to take the next step and how shuffle more than 5 values to and from a database.

    The first big site i developed the SQL statements where a headache and after a while I found out that classes would be something that might come in handy. So read on and please comment if, makes the guide better and helps people to make PHP a better language.

    The issue with big SQL-strings is that you sometimes get lost in them, especially the INSERT strings. An example below.

    INSERT INTO tbl (var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13,var14) VALUES ($var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8,$var9,$var10,$var11,$var12,$var13,$var14)

    This is a messy code and we need a way of loading our variables and then just hit save.

    The solution

    Classes is a great way of manage you data and to have a decent structure in your code.
    In a nutshell we are going to create a class, give it some properties, function and then load and save our data to a database.

    Let's go!

    I like using the car as an example so we are going to use that. A car have some characteristics like make, model, colour, engine and so on.

    If we would build us a car inventory we need a class called car. So create a new file called classes.php and in that file we need to create a function that initialize a connection to our database. In this example I will use a ODBC connection. but you can use what ever connection you like, as it returns a connection handle.

    function db_conenct()
    {
    	$conn_id = odbc_connect("ODBC-DSN", "", "") or die ("Could not connect");	
    	return $conn_id;
    }

    Then we will create the class car and the properties we wish to use is, make, model, colour. We also need a way of keeping track of our items in the database so we need to add Id and then I always have a Deleted column since it's easier to set to false again then to restore database from backup. Insert the following code in our classes.php

    class Car
    {
    	var $Id;
    	var $Make;
    	var $Model;
    	var $Colour;
    	var $Deleted;
    	
    	var $connection_id;
    	
    	function Car($conn)
    	{
    		$this->connection_id = $conn;
    	}
    		
    }

    Notice the function with the same name as the class, this is called a constructor and is run every time you load a create a class in your code. But we need to insert and read some data from our class. So we need 4 functions and then we can do whatever we want with our car data. First of we need to get data for a specific car and we use the Id column to identify our cars. Insert the following function.

    	function GetCarById($id)
    	{
    		$sql = "SELECT * FROM tblCars WHERE Id = $id";
    		$result = odbc_exec($this->connection_id, $sql);
    		$data = odbc_fetch_array($result);
    		
    		$this->Id = $data['Id'];
    		$this->Model = $data['Model'];
    		$this->Colour = $data['Colour'];
    		$this->Deleted = $data['Deleted'];
    		$this->Make = $data['Make'];
    	}

    This function send a query to the database and get a result back, loads the current class's variables from the database, notice that we write $this->Id instead of $Id or $car->Id. The command $this-> tells php that THIS is for THIS instance of the class car. It is very important that we use the $this-> command. Next we need a function to get all the cars since we probably would like to make a list of cars.

    Here comes a nice way of doing business, we only get the Id from the database and return all the data in an Array. We then use the Id number to get the actual database row. Go ahead and insert the following function in our class.

    	function GetAllCars()
    	{
    		$sql = "SELECT Id FROM tblCars Where Deleted = false Order by Make DESC";
    		$result = odbc_exec($this->connection_id , $sql);
    		$i = 0;
    		while($data = odbc_fetch_array($result))
    		{
    			$DataArray[$i] = $data['Id'];
    			$i++;
    		}
    		return $DataArray;
    	}

    Notice the SQL statement we select just Id and we filter out the deleted rows and get an array in return. Now we can move on to our main page where we list our cars.
    Create a file and a table with the columns (make,model,colour).
    On top of our file we need to include our classes.php and this is done with require_once():. Go ahead and add our classes.php on row 1.

    Below you have an example showing how this works.

    <html>
    <head>
    <title>Nisses class example</title>
    </head>
    <body>
    <table border="0">
    <tr>
    	<td>Make</td>
    	<td>Model</td>
    	<td>Colour</td>
    </tr>
    <?
    $clsCar = new Car(db_connect());
    $DataArray = $clsCar->GetAllCars();
    
    for($i = 0; $i < count($DataArray);$i++)
    {
    	if($i % 2 == 0)
    	{
    		$bgcolor = "#FFFFFF";
    	}
    	else
    		{
    			$bgcolor = "#D8D8D8";
    		}
    	$clsCar->GetById($DataArray[$i]);
    	echo "<tr bgcolor=" . $bgcolor . ">";
    	echo "<td>" . $clsCar-Make . "</td>";
    	echo "<td>" . $clsCar-Model . "</td>";
    	echo "<td>" . $clsCar-Colour . "</td>";
    	echo "</tr>";
    }
    ?>
    </table>
    </body>
    </html>

    The row $clsCar = new Car(db_connect()); creates a connection to our database. Remember the function db_connect that returns a connection handle. It's then taken care of by our constructor and stored in the class variable $this->connection_id . We have now a class called $clsCar and what we need to do is to get a list of all non deleted cars. We need to call our function GetAllCars and store the returned value in an array. $DataArray = $clsCar->GetAllCars(); does that for us. We now have ourselves an array of Id's on which we can query the database for actual items. So we loop the array until it ends and print out the data. Notice that we don't use the $this-> anymore. We are only interested in the instance we created $clsCar and only for this user.

    I've been using this method for getting data from tables for a few years now and it proves to be very scalable since all we need to do if you like to add a property there is not that much work in doing that. Next step is to insert and update our data. The next step is in the works, stay tuned!

    Technorati-taggar: ,,,,