Friday, 14 November 2014

linux: Autologin VOO_HOMESPOT free wifi


The goal is to connect to the VOO_HOMESPOT wifi network without login into the web browser.VOO_HOMESPOT the is the wifree wireless network of VOO in Belgium.

This should work with any linux using network-manager.

Just copy file below into /etc/NetworkManager/dispatcher.d/voo-homespot
make it executable and be sure root is the owner


chown root.root  /etc/NetworkManager/dispatcher.d/voo-homespot
chmod 700 /etc/NetworkManager/dispatcher.d/voo-homespot


Here is the script
#!/bin/sh

USERNAME=<YOUR VOO USERNAME>
PASSWORD=<YOUR VOO PASSWORD>

ESSID="VOO_HOMESPOT"
TARGET=http://www.google.com/
AUTH_URL=https://wifree.voo.be/authenticate.html

if iwconfig | grep -q $ESSID ; then
    wget -qO- --post-data="username=${USERNAME}&password=${PASSWORD}&rememberme=rememberme&accept=accept&target=${TARGET_URL}" $AUTH_URL | grep -q connected_success.png
   if [ $? -eq 0 ] ; then
       logger "connected to $ESSID "
   else
       logger "login to $ESSID failed"
   fi
else
   logger "not ${ESSID}, don't try to login"
fi

Tuesday, 4 November 2014

Monospaced and fixed width fonts for programming and terminal

The best of all font ever for console and programming is the fixed size used in X windows :

  • 6x13 -Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO10646-1
Here is how I get it in my Ubuntu 14.04, following the greate article from pts.org  ( http://ptspts.blogspot.co.uk/2010/06/how-to-enable-bitmap-fonts-on-ubuntu.html )


$ wget -q -O - http://pts-mini-gpl.googlecode.com/svn/trunk/fonts/fixedsc.tgz |
       (cd / && sudo tar xzv)
$ sudo rm -f /etc/fonts/conf.d/70-{yes,no,force}-bitmaps.conf
$ if test -f /etc/fonts/conf.avail/70-force-bitmaps.conf
  then sudo ln -s {../conf.avail,/etc/fonts/conf.d}/70-force-bitmaps.conf
  else sudo ln -s {../conf.avail,/etc/fonts/conf.d}/70-yes-bitmaps.conf
  fi
$ sudo rm -f /var/cache/fontconfig/*
$ rm -rf "$HOME/.fontconfig"
$ sudo fc-cache
$ fc-cache

Then I select FixedSc size 10 for all my x-terminal. This works great with low res screen, but become more difficult to read with high definition one. Then I choose INCONSOLATA font for these displays. This font works well in all size, hinting and sub-pixel order. Other font looks better but then you have to tune the font rendering and maybe reduce

$ sudo apt-get install fonts-inconsolata

See some other fonts here :



Saturday, 2 March 2013

create a vfat Grub2 boot floppy


Create a small Grub2 boot .iso using :


 grub-mkrescue --diet -o fd.iso

create some directories, names are important

mkdir -p mnt fdboot/boot /grub

mount the .iso and copy all files in /boot/grub/i386-pc into fdboot/boot /grub
Then create a fdboot/boot /grub/grub.cfg that match your need, for example :


set timeout=5
set default=0 # Set the default menu entry

menuentry "This is the floppy that will boot on first disk" {
   set root=(hd0)
   chainloader +1
   boot
}


If you want to create a real 1.44Mo floppy, you have to remove some file in fdboot/boot /grub.
I choose to create a 2.88Mo floppy, because VMware don't care about the size of the floppy :-)

Then cut&paste the script bellow adapt the 2880 and the -a to mach the size of your floppy and the version of your Grub2. 5760 means 2.88Mo, use 2880 for 1.44Mo floppy.

The run the scrip below. the scrip come from Robert at Re: [SOLVED] Re: Grub2 editable floppy?


#!/bin/sh
# Creates a vfat editable Grub2 boot floppy  (not a read-only ISO9660 one like grub-mkrescue)
#
# template folder ./fdboot/boot/grub  shall contain a grub2 tree like /boot/grub
#    including a grub.cfg,  stripped down to <= 1.4MB;
#    or a tree copy from a "grub-mkrescue [--diet] -o fd.iso" iso floppy
#       (and do "cd fdboot/boot/grub; mv i386-pc/* ." ! because the boot.img+bootdisk looks there)
#       ("--diet" unfortunately disappeared in v1.99)
#
# Note: used with Grub2 v1.98 and v1.99; v1.99 requires the "-a" flag in "grub-setup -a ..."

# output image filename
fdimg=fdb.fat

# script

umount mnt
losetup -d /dev/loop0

dd if=/dev/zero of=$fdimg count=5760
losetup /dev/loop0 $fdimg || exit 1
fdformat /dev/loop0
mkfs.vfat /dev/loop0 || exit 1
mount /dev/loop0 mnt || exit 1

##rm fdboot/boot/grub/i386-pc/efiemu.mod grcy_* ... strip off to 1.4MB  / create by "grub-mkrescue --diet ..."
cp -a fdboot/* mnt/  || exit 1
##cp /boot/grub/grub.cfg mnt/boot/grub/

cat > mnt/boot/grub/device.map <<EOF
EOF
##(fd0) /dev/fd0
##(hd0) /dev/sda
##(hd1) /dev/sdb

cp /boot/grub/boot.img mnt/boot/grub/ || exit 1

grub-mkimage -o mnt/boot/grub/core.img -O i386-pc biosdisk fat part_msdos || exit 1

# --force is needed to force blocklists (for core.img) on floppy:
##grub-setup --force -d mnt/boot/grub -m mnt/boot/grub/device.map /dev/loop0 || exit 1

# variant with -a required for Grub2 1.99+ :
grub-setup -a --force -d mnt/boot/grub -m mnt/boot/grub/device.map /dev/loop0 || exit 1

umount mnt
losetup -d /dev/loop0

# copy the image to virtual machine image folder
cp -a $fdimg /hptemp/boot/

echo Grub2 VFAT editable boot floppy image $fdimg created. Done.
echo TO MOUNT: mount -o loop $fdimg mnt
echo TO WRITE TO REAL FLOPPY: cp $fdimg /dev/fd0
##cp $fdimg /dev/fd0




Good luck, thanks Robert for this script.


Friday, 1 March 2013

Run python-2.7 program inside the WinPE environment

This is notes I took few hours after the performance. They are incomplete.

Since version 2.6, python binaries are compiled with a compiler that depend of an annoying DLL. Because they don't have the right to redistribute this DLL, you have to download this free DLL and install it yourself.
99% of the time this DLL is installed by another package and you don't event know that you need it.

This DLL is missing in WinPE and is particular because it is handled by SxS.

Here is what I did.

First I have installed the python 2.7 32bits from Activestate on my XP.
Then I have booted the target host using a WinPE CD.
I have mounted the C: drive of my XP on the WinPE on G:

net use G: \\name_or_ip\C$  /user:Administrator password

Then cd to G:\Python27, an tried to start python interpreter :


G:\Python27:\>python.exe
The application has failed to start because its side-by-side configuration is in
correct. Please see the application event log or use the command-line 
sxstrace.exe tool for more detail.

I have used sxstrace as follow :


X:\>SxsTrace Trace -logfile:SxsTrace.etl

Tracing started. Trace will be saved to file SxsTrace.etl.
Press Enter to stop tracing...

Then I ran python.exe once again to get the trace, stopped SxsTrace and parsed the .etl file

X:\>SxsTrace Parse -logfile:SxsTrace.etl -outfile:SxsTrace.txt
Parsing log file SxsTrace.etl...
Parsing finished! Output saved to file SxsTrace.txt.

In the file, I found something like this (this one is about python-2.6) but is identical :

=================
Begin Activation Context Generation.
Input Parameter:
Flags = 0
ProcessorArchitecture = x86
CultureFallBacks = en-US;en
ManifestPath = G:\Python26\python.exe
AssemblyDirectory = G:\Python26\
Application Config File = 
-----------------
INFO: Parsing Manifest File G:\Python26\python.exe.
INFO: Manifest Definition Identity is (null).
INFO: Reference: Microsoft.VC90.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8"
INFO: Resolving reference Microsoft.VC90.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8".
INFO: Resolving reference for ProcessorArchitecture x86.
INFO: Resolving reference for culture Neutral.
INFO: Applying Binding Policy.
INFO: No publisher policy found.
INFO: No binding policy redirect found.
INFO: Begin assembly probing.
INFO: Did not find the assembly in WinSxS.
INFO: Attempt to probe manifest at X:\windows\assembly\GAC_32\Microsoft.VC90.CRT\9.0.21022.8__1fc8b3b9a1e18e3b\Microsoft.VC90.CRT.DLL.
INFO: Attempt to probe manifest at G:\Python26\Microsoft.VC90.CRT.DLL.
INFO: Attempt to probe manifest at G:\Python26\Microsoft.VC90.CRT.MANIFEST.
INFO: Attempt to probe manifest at G:\Python26\Microsoft.VC90.CRT\Microsoft.VC90.CRT.DLL.
INFO: Attempt to probe manifest at G:\Python26\Microsoft.VC90.CRT\Microsoft.VC90.CRT.MANIFEST.
INFO: Did not find manifest for culture Neutral.
INFO: End assembly probing.
ERROR: Cannot resolve reference Microsoft.VC90.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8".
ERROR: Activation Context generation failed.
End Activation Context Generation.


The two most important lines are :


X:\windows\assembly\GAC_32\Microsoft.VC90.CRT\9.0.21022.8__1fc8b3b9a1e18e3b\Microsoft.VC90.CRT.DLL.
INFO: Attempt to probe manifest at G:\Python26\Microsoft.VC90.CRT.MANIFEST.


On my XP I just copied few files :

copy C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\*.dll C:\Python27
copy c:\WINDOWS\WinSxS\Manifests\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375.manifest G:\Python26\Microsoft.VC90.CRT.MANIFEST
copy C:\Windows\system32\python27.dll c:\Python27

I started again python.exe and it was working.

I'have not yet tried, but I expect that copying the whole C:\Python27 to the WinPE CD should be enough.

I have copied the same .dll to my Python26 and to a official Python27 install and both worked too.

Hope this help.





Friday, 4 January 2013

pyzmail easy mail library for python


pyzmail 1.0 is out !

pyzmail is a high level mail library for Python. It provides functions and classes that help to readcompose and send emails. pyzmail exists because their is no reasons that handling mails with Python would be more difficult than with popular mail clients like Outlook or Thunderbird. pyzmail hides the difficulties of the MIME structure and MIME encoding/decoding. It also hides the problem of the internationalized header encoding/decoding.

Previous version 0.9.9 was out for more than 1 year, version 1.0 contains a minor bug fix.

Friday, 21 December 2012

pfSense bridge gateway vmware ovh ip failover ripe

This post is under construction.

This post is about the setup of a pfSense 2.0 firewall in transparent mode to connect and protect virtual machines hosted by OVH to the internet.

OVH provides failover IPs (fr) and RIPE IP Failover block (fr). These IPs and blocks can be re-routed between servers you are renting inside an OVH datacenter in 1min.
This make migration, failover and HA easy and at a low cost, no need to change something in the DNS or in the VM setup.

The setup and the use of these IPs is not easy. Adding a firewall in between don't help.To not increase the complexity of this post, the firewall is configured in transparent mode. This means that interfaces of the firewall are bridged.

What is a bridge ?

A network bridge fits between two networks and repeats Ethernet frames on one side to the other side and vice versa. A bridge works at layer 2 (Ethernet) of the OSI model (IP is layer 3) and ignores the IP settings. All type of Ethernet frames are forwarded by the bridge, even theses that encapsulate packets that are not related to the IP protocol. Bridges are smart and learn MAC addresses on both side to avoid to repeat frames that don't need to cross from one network to the other.

Promiscuous mode

If you are using an hypervisor like VMware ESXi or Xen, you have to enable the promiscuous mode on each interfaces of the bridge to allow it to see all Ethernet frames and guess host on each side.

Here is the procedure for VMware, ask Google if you use another hypervisor.


pfSense and bridging

The bridge connects multiple interfaces. Each of these interfaces can have an IP address, but this is not required. The Bridge itself can be attached to a network interface and have an IP address, but this is not required either. Here the goal is not to combine two networks in one but just to connect them without wasting any IP addresses or increasing complexity. Frames that contains IP packets can be filtered by the firewall rules. Here I'll bridge the WAN and the OPT1 (DMZ) interface. The LAN will be kept out of the bridge.
pfSence can filter at the bridge level, but we want to filter at the member level. Check in the advanced settings of your FW that your setup match this one:



When do you need to allocate an OVH virtual MAC for your VM

I think the main purpose of the virtual MAC at OVH is to avoid MAC address conflict. This could be very annoying if your production server would stop working because someone is installing a new VM using a random MAC address that match your ! In the mean time they can use this to reinforce the security of their network.
Most of the time when you attache a failover IP to a virtual machine then you need to allocate a virtual MAC for this VM. But his is not always true. For example if you are attaching a second IP to an existing VM using the IP Alias approach, you don't need one because this second IP will use the MAC address of the primary address. Also If your VM is behind a router doing some Proxy ARP, the router use its own MAC address to masquerade your IP and the other side of the router it will be happy with any MAC address used by your VM.
This could be counter productive to allocate a MAC address when not needed, the OVH infrastructure could block the traffic when the MAC don't match the IP address. And if their are not doing that today they could do it later.

In the configuration bellow, you need to allocate a virtual MAC address for VMs going in the DMZ and for the firewall itself if you give it an IP address. No need of virtual MAC for VMs going in the LAN.

Giving a routable IP address to the firewall or not ?

Even if the bridge mode allows the firewall to do its job without giving it any IP address, the firewall need one to be managed using a web browser.

For the early setup and for emergency access, I have the use to create a VM using a XUbuntu LiveCD distibution on the LAN side and use the VMware console to manage the firewall. The Firewall LAN side is configured with default address 192.168.1.1 and a working DHCP server, this make the network setup of the VM very easy. Just connect your browser to http://192.168.1.1, use admin / pfsense for login / password and you are in.

Giving a routable IP to the WAN side of your firewall would allow you to remotely manage your firewall and do hide NAT (masquerading) for the VMs connected to the LAN side (if any). This also allows you to upgrade and install new packages.

The challenge

Here is the "virtual" challenge I'll try to solve in this post. I'm using addresses from the private ranges to make my live easier for the demo but address in 10.x.x.x and 172.x.x.x must be viewed as routable address. 192.168.x.x are used for private network, like the LAN.

I have an OVH dedicated server with address 10.99.99.99/254 and its gateway at 10.99.99.254. OVH gives me a RIPE block of 8 addresses: 10.10.10.8/29 with the gateway at 10.10.10.14. I also have 2 single failover IPs 172.16.16.16/32 and 172.17.17.17/32.

The RIPE block can be used in 2 different ways.
  • The normal way is to use it as a subnet, with first address of the block reserved for the network and the last one for the broadcast address. Including the gateway address that is already allocated to an OVH router, this make 3 addresses unusable.  If you have a /30 block (4 addresses) this make only one usable address for your host(s).  The advantage of this setup is the simplicity of the network configuration of the hosts themselves.
  • The other possibility is to use each address individually with a netmask of  /32. You don't need to split your RIPE block in the OVH manager to works that way (this is what OVH say), just use each address like a single failover IP. When doing this, even if VMs are on the same switch, they are not on the same subnet anymore and traffic between them has to travel up to the default gateway and back. If you want a more direct path, you have to create a route between your VMs (see below). Also I thing this is better to use the RIPE gateway 10.10.10.10.14 than using the gateway of the physical host (10.99.99.254)  because when moving the VMs and the RIPE block to another host, you don't need to change the VMs gateway. The inconvenient of this choice is that you have to configure your host network using the OVH guide (french).

Simple solution for the RIPE block

In this simple setup, I don't give any routable address to the firewall. The early setup must be done with the help of a VM installed on the LAN side.


First we need to enable and setup the WAN and DMZ (OPT1) interfaces to none. We will not attach any IP addresses to these interfaces (this time). We will bridge them. Bridge works one level below the Internet protocol and don't need any IP settings.




Then we are creating the bridge :


And setup the two members of the bridge using ctrl-click. This is the most difficult part :-)


The setup is done. Yes don't be afraid this is enough. Now we have to setup the filter rules. I give you some sample rules, but use your owns.

First I like to be able to ping in any direction, at least for the setup and troubleshooting.


Here are the details :


I want to give unrestricted internet access to VMs in the DMZ. Anyway I'm adding a restriction for the LAN. The DMZ can be accessed by the internet (demilitarized zone) and can be compromised. This is why the DMZ cannot have more access rights to access the LAN than the Internet.


Here are all the details :



Now I suppose I want to host a mail server on 10.10.10.9 and need to let SMTP packets go in..



Here are the details of the SMTP setup.



Now the most important part is the network setup of 10.10.10.9. Nothing difficult  anyway :
IP_ADDR=10.10.10.9
NETMASK=255.255.255.248 (/29)
GATEWAY=10.10.10.14
Network setup for other hosts of the RIPE block are similar.

Add a single failover IP to the DMZ

Now I want to add a host with a single failover IP 172.16.16.16/32 to the DMZ. I just need to setup the filter rules like the SMTP rule above for this new host. Nothing more is required because we are working in bridge mode.
On the other hand, the setup of the host itself is more tricky, not because of the firewall, but because of how failover IP works. Follow the OVH guide (french) to setup your new host using 172.16.16.16 as the failover IP and 10.99.99.254 for the gateway (the one ending in .254).

In this configuration, traffics between 10.10.10.9 and 172.16.16.16 has to go up to the OVH router and then back to the destination. In both case the traffic has to pass through the firewall that must have a rules to let the traffic goes out and in. This is because hosts ignore they are side by side. If you expect to have a lot of traffics and want to speed it up, you have to add routes between the hosts themselves. Don't think that the fact that your traffics between host inside the DMZ goes trough the firewall increase the security. If a pirate is able to hack one of your VM inside the DMZ, he is able to bypass the firewall to attack your other hosts too.

Here are sample command lines for linux and windows. You have to hard-code these routes in the hosts configurations.

On 172.16.16.16, add a route to network 10.10.10.8/29
linux: route add -net 10.10.10.8 netmask 255.255.255.248 dev eth0
windows: route -p ADD 10.10.10.8 MASK 255.255.255.248 172.16.16.16

On 10.10.10.9 add a route to host 172.16.16.16/32
linux: route add -host 172.16.16.16 netmask 255.255.255.255 dev eth0
windows: route -p ADD 172.16.16.16 MASK 255.255.255.255 10.10.10.9

Attach an IP to the WAN side

This makes the firewall remotely manageable and allows to give an internet access to the LAN side.
I choose 10.10.10.13/29 from the RIPE block, in the next section I explain how to use a single failover IP instead.


I go to the WAN interface configuration to setup the IP and its gateway at once.


Now I masquerade the LAN addresses using hide NAT to give Internet access to the LAN side. Don't forget to switch to manual, I don't want to NAT hosts in the DMZ.



The details:


 I also need to check that the LAN has a rule that give access to the internet :


Done !

If you want, you can forward some ports to hosts inside the LAN.
You can reach host in the DMZ from LAN.

Attach a single failover IP to the WAN side

If you don't have a RIPE block or prefer to use a single failover IP instead, then read this section.
This setup cannot be configured easily using the pfSense GUI interface, some tricks are required.
Here is the plan, first get an Internet access using some hand written command lines. Then download and install the Shellcmd package. Finally hard code the command line to make the setup persistent.

First setup the WAN IP without any gateway :



Then setup the default gateway manually. I choose to use the Command Prompt interface of the GUI. Use the two commands :
route add -net 10.99.99.254/32 -iface em0
route add default 10.99.99.254


If you don't know where 10.99.99.254 is coming from, re-read the beginning of this article.
Don't forget to setup a working DNS to have a fully working internet connection. I choose the Google public DNS.


Now the firewall should have internet access and we can install package Shellcmd.


Then hard code the two commands above inside Shellcmd to get the make config persistent.


You can try to reboot, just to see if the config works and setup hide NAT for the LAN side as explained above.

Funny things 

It looks like the OVH gateway works like an universal ARP proxy or Captive portal. I means my OVH gateway replies to any ARP request. This means that for any given a.b.c.d/32 failover IP, I can setup a host or virtual host using a mask /24 (instead of /32) and a.b.c.X where X can be anything not in ( 0, 255 or d ) for my gateway.And it works on my Kimsufi server.
I think this is how OVH setup some (maybe all) of their routers to be able to support migration of  failover IP or block without too much headache.
What is fun is to traceroute some IPs in a.b.c.*  and see that they are not directly attached to the WAN but are behind some routers.
OVH don't say anything about this setup and then this feature is funny but cannot be used on a production server.
Be careful if you use a wrong setup and generate a lot of unexpected ARP requests, OVH monitor ARP request and can warn you to quickly fix the problem or even disable your network link.

Conclusion

For me the use of the bridge is a brilliant idea. The setup is simple. You can add hosts , IPs and RIPE blocks easily to the DMZ.