Mintact Software Inc. builds secure software application

Tech Notes

    Upgrading a hard drive of my Fedora laptop

    October 16th, 2009 by Min Chen

    I bought a 500G SATA hard drive to replace the 160G one in my HP laptop. I then managed to clone all partitions from the old disk to the new one, and resized (increased) the file system. Here’s how I did it.

    What I needed:

    1. A computer with two SATA interfaces. I have a Windows desktop that has 4 SATA ports and 2 cables. I then pulled out the data cables and power cables from the existing 3.5″ desktop hard drives in the Windows box in order to hook up the 2.5″ laptop hard drives
    2. A Fedora 11 installation DVD
    3. Screw drivers

    First I unscrewed the 160G hard drive from my laptop, and then took both new and old hard drives to my Windows desktop. I then plugged the data cables and power cables to both 2.5″ drives, the new one was on SATA 1 and the 160G old one was on SATA 2.

    Powered on the box and booted from Fedora installation DVD. By entering “rescue mode”, I was able to ls /dev and saw /dev/sda and /dev/sdb, /dev/sdb1, /dev/sdb2, /dev/sdb3. Obviously the new 500G hard drive was /dev/sda and the old one was /dev/sdb.

    To check the partition table of the old drive, I run fdisk -l /dev/sdb.
    The output was


    Disk /dev/sdb: 160.0 GB, 160041885696 bytes
    255 heads, 63 sectors/track, 19457 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0×95aa95aa

    Device Boot Start End Blocks Id System
    /dev/sdb1 * 1 25 200781 83 Linux
    /dev/sdb2 18197 19457 10128982+ 7 HPFS/NTFS
    /dev/sdb3 26 18196 145958557+ 8e Linux LVM


    /dev/sdb1 was the grub
    /dev/sdb2 was the HP’s system recovery image
    /dev/sdb3 was the most important, it had all my Linux system and data

    Based on this partition table, I run fdisk /dev/sda to setup 2 primary partitions on the new drive. Use n to create partition, and t to change the Id for 2nd partition from the default 83 to 8e, w to save the partition table. I made the sizes of new partitions identical to the old ones. The partition table of new hard drive look like this


    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 25 200781 83 Linux
    /dev/sda2 26 18196 145958557+ 8e Linux LVM


    The size of /dev/sda1 was identical to the size of /dev/sdb1, and /dev/sda2 was identical to /dev/sdb3.

    Restarting the machine, booted from Fedora DVD, and entered into rescue mode, and I was ready to clone the partitions. Simply run the dd command
    dd if=/dev/sdb1 of=/dev/sda1 bs=1M to clone the /boot and
    dd if=/dev/sdb3 of=/dev/sda2 bs=1M to clone the Linux system

    It took about 10 seconds to clone the 200M grub partition, and about 2 hours to clone 140G Linux partition.

    I then shut down the machine, unplugged all cables from laptop hard drives, and screwed the new 500G hard drive into my HP laptop. I had to insert the Fedora DVD into laptop and entered into rescue mode to setup grub. I run these commands


    grub
    root (hd0,0)
    setup (hd0)


    After these, I removed the DVD and booted from hard drive. I was able to use the new hard drive now. Everything was saved, the laptop works exactly the same as if I’d never changed the hard drive.

    My next task is to increase the file system size. As shown in the fdisk output, my Linux is using LVM, so I need to increase the size of LVM partition, and then increase the size of virtual group and logical volume. Before I did any changes, I run pvdisplay, vgdisplay, and lvdisplay, and saved the output to a file. I have two logical volumes, /dev/VolGroup00/LogVol00 for my Linux file system, and /dev/VolGroup00/LogVol01 for swap.

    First step was to increase the partition size. It was easily done by fdisk, by using d to delete the partition, and then n to recreate the partition with bigger size, and w to save the changes. I had to reboot the laptop to apply the changes.

    This is the output of my partition table (fdisk -l /dev/sda) after increasing the size:


    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0×000ba110

    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 25 200781 83 Linux
    /dev/sda2 26 36392 292117927+ 8e Linux LVM


    I doubled the size of /dev/sda2 from the original 146G to now 292G.

    The rest would be dealing with LVM.

    1. run pvresize /dev/sda2 to update the physical volume size
    2. run lvresize -L +1G /dev/VolGroup00/LogVol01 to add 1G bytes to my swap
    3. run lvresize -l +100%FREE /dev/VolGroup00/LogVol00 to add all free space to my Linux system
    4. run resize2fs -p /dev/VolGroup00/LogVol00 to resize the ext3 file system
    5. run swapoff -a to turn off swap
    6. cat /etc/fstab to get the swap UUID, in my case it’s 52841e8a-2b3d-4a9d-92bc-1b5af50e4b8a
    7. run mkswap -U 52841e8a-2b3d-4a9d-92bc-1b5af50e4b8a /dev/VolGroup00/LogVol01 to rebuild swap
    8. run swapon -a to turn on swap
    9. run e2fsck to check the file system if necessary

    After these steps, dh -h shows that I have successfully increased the size of my file system.

    Using external monitors on my Linux laptops

    July 9th, 2009 by Min Chen

    I have one HP laptop (with Intel graphic chips) with Fedora 10 installed and I had never made the external monitor hotkey fn+f4 working. I have another HP laptop (also using Intel graphic chips) with Fedora 11 installed, fn+f4 hotkey works but not as flexible as what I wanted. so I had to find a solution with the xrandr extension.

    First step is to get the external monitor’s modeline. Running cvt h_resolution v_resolution will usually display the modeline that can be used directly. I have to make sure the refresh rates displayed by cvt is within the range of my monitor’s specification. My experience is that the modeline displayed by cvt needs to be fine tuned to get the best result. I have an LCD with 1680×1050 max resolution, the output of cvt 1680 1050 is


    # 1680x1050 59.95 Hz (CVT 1.76MA) hsync: 65.29 kHz; pclk: 146.25 MHz
    Modeline "1680x1050_60.00" 146.25 1680 1784 1960 2240 1050 1053 1059 1089 -hsync +vsync

    Using the same command, I get the modeline for my LCD TV


    # 1920x1080 59.96 Hz (CVT 2.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz
    Modeline "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync

    After running cvt, I modify the Xorg.conf for xrandr. Here’s how I set it up:

    1. Hook up my LCD to my laptop, turn on the LCD, restart X by press ctrl+alt+backspace to have a clean Xorg log
    2. run
      xrandr --query
    3. vi /var/log/Xorg.0.log
    4. search

      Using config file:

      I will need to edit this file later. I have

      (==) Using config file: "/etc/X11/xorg.conf"

      in Xorg log, so I’ll edit /etc/X11/xorg.conf later

    5. search ServerLayout in Xorg log to find out what screen is used.
      I see


      (==) ServerLayout "single head configuration"
      (**) |-->Screen "Screen0" (0)
      (**) | |-->Monitor "Monitor0"
      (**) | |-->Device "Videocard0"
      (**) |-->Input Device "Keyboard0"
      (**) |-->Input Device "Synaptics"

      So I know I am using Screen0

    6. Optionally search

      Output VGA using monitor section

      and I get

      (II) intel(0): Output VGA using monitor section Monitor0

      so I’ll edit Monitor0 in xorg.conf. This message might be Intel driver specific

    7. Optionally get the maximum sync rate from my monitor’s specification, which is 75.0 Hz (V)x 81.0 KHz (H), or by reading the detail message in Xorg.0.log
    8. Optionally edit xorg.conf mentioned in previous step. search Identifier "Monitor0", make sure the max of HorizSync and VertRefresh are larger than or equal to the numbers in monitor’s specification.This is my monitor section after changing HorizSync and VertRefresh


      Section "Monitor"
        Identifier "Monitor0"
        ModelName "LCD Panel 1280x800"
        HorizSync 31.5 - 75.0
        VertRefresh 30.0 - 81.0
        Option "dpms"
      EndSection

      These 3 steps are optional. Without this step, the driver may show hsync out of range or vrefresh out of range message.

    9. In above steps, I know I am using “Screen0″. Now in xorg.conf, search

      Identifier "Screen0"

      in Section "Screen", add Virtual in SubSection "Display" to specify the virtual screen size. The virtual screen size must be big enough to hold the laptop monitor and an external monitor. My laptop resolution is 1280×800, and my LCD TV resolution is 1920×1080, I want these monitors aligning from left to right, so the virtual screen size is (1280+1920) x Max(800, 1080), so I put 3200 1080 to xorg.conf.
      Here’s what the screen section looks


      Section "Screen"
        Identifier "Screen0"
        Device "Videocard0"
        Monitor "Monitor0"
        DefaultDepth 24
        SubSection "Display"
          Viewport 0 0
          Depth 24
          Modes "1280x800" "1280x720" "1024x768" "800x600" "640x480"
          Virtual 3200 1080
        EndSubSection
      EndSection

    Then I created a shell script to use xrandr to control my monitors.

    #!/bin/sh
    # This script is to switch monitors because the fn+f4 hotkey doesn’t work
    #
    # Author: Min Gang Chen
    # Make sure “Virtual” in SubSection “Display” in Section “Screen” is big
    # enough to cover all monitors

    LCD_MODELINE=”1680×1050 146.25 1680 1784 1960 2240 1050 1053 1059 1089 -hsync +vsync”

    TV_MODELINE=”1920X1080 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync”

    # this is the modeline for my HP laptop
    LAPTOP_MODELINE=”1280×800 69.30 1280 1328 1360 1415 800 803 809 816 -hsync -vsync”

    SVIDEO_MODELINE=”"
    # rung xrandr -q to get the output names
    # use VGA for Fedora 10 and VGA1 for Fedora 11
    EXTERNAL_OUTPUT_NAME=”VGA”
    # use LVDS for Fedora 10 and LVDS1 for Fedora 11
    LAPTOP_OUTPUT_NAME=”LVDS”
    # use TV for Fedora 10 and TV1 for Fedora 11
    SVIDEO_OUTPUT_NAME=”TV”

    XRANDR=/usr/bin/xrandr

    # Set the resolution for external display
    set_external_mode()
    {
      case $1 in
        lcd) EXTERNAL_MODELINE=”$LCD_MODELINE”;;
        tv) EXTERNAL_MODELINE=”$TV_MODELINE”;;
        *) EXTERNAL_MODELINE=”$LCD_MODELINE”;;
      esac
      $XRANDR –newmode $EXTERNAL_MODELINE >/dev/null 2>&1
      $XRANDR –addmode $EXTERNAL_OUTPUT_NAME `echo “$EXTERNAL_MODELINE” | cut -f1 -d ” “` > /dev/null 2>&1
    }

    # Use LAPTOP monitor
    laptop()
    {
      if [ "x$EXTERNAL_ON" = "xfalse" ]
      then
        # turning off external monitors in case they were on
        $XRANDR –output $EXTERNAL_OUTPUT_NAME –off
        $XRANDR –output $SVIDEO_OUTPUT_NAME –off
        # Positioning LAPTOP monitor at x:0 y:0
        $XRANDR –output $LAPTOP_OUTPUT_NAME –pos 0×0 –auto
      elif [ "x$EXTERNAL_ON" = "xtrue" ]
      then
        # external is alreay on, align the new monitor to the right
        RIGHT=`echo “$EXTERNAL_MODELINE” | cut -f3 -d ” “`
        $XRANDR –output $LAPTOP_OUTPUT_NAME –pos ${RIGHT}x0 –auto
      fi
    }

    # Use External monitor
    external()
    {
      set_external_mode $1
      MODE=`echo “$EXTERNAL_MODELINE” | cut -f1 -d ” “`
      if [ "x$LAPTOP_ON" = "xfalse" ]
      then
        # turning off LAPTOP and S_Video monitors in case they were on
        $XRANDR –output $LAPTOP_OUTPUT_NAME –off
        $XRANDR –output $SVIDEO_OUTPUT_NAME –off
        # positioning external monitor at x:0 y:0
        $XRANDR –output $EXTERNAL_OUTPUT_NAME –pos 0×0 –mode “$MODE”
      elif [ "x$LAPTOP_ON" = "xtrue" ]
      then
        # laptop monitor is on, align to the right
        RIGHT=`echo “$LAPTOP_MODELINE” | cut -f3 -d ” “`
        $XRANDR –output $EXTERNAL_OUTPUT_NAME –pos ${RIGHT}x0 –mode “$MODE”
      fi
    }

    # User both LAPTOP and External, external at the right of Laptop
    use_both()
    {
      set_external_mode
      $XRANDR –output $LAPTOP_OUTPUT_NAME –pos 0×0 –auto

      MODE=`echo “$EXTERNAL_MODELINE” | cut -f1 -d ” “`
      RIGHT=`echo “$LAPTOP_MODELINE” | cut -f3 -d ” “`
      $XRANDR –output $EXTERNAL_OUTPUT_NAME –pos ${RIGHT}x0 –mode “$MODE”
    }

    print_usage()
    {
      echo “Usage: $0 [options]”
      echo “options:”
      echo -e “\t-l use Laptop monitor”
      echo -e “\t-e[lcd|tv] use external monitor”
    #echo -e “\t-b use both monitor, the laptop on left and external monitor on right”
      echo “”
      echo “The order of options DOES matter. The monitors will be aligned from \
    left to right, based upon the order. For example, running $0 -l -e lcd will \
    align external monitor to the right border of the laptop monitor.”
      echo “If only one option is given, only that specific monitor will be turned \
    on. For example, running $0 -l will turn off external monitor if it was already on, and then turn on laptop monitor.”
      exit 1
    }

    if [ "$#" -eq "0" ]
    then
      print_usage
    fi

    LAPTOP_ON=false
    EXTERNAL_ON=false

    while getopts le: FLAG
    do
    case $FLAG in
      l) LAPTOP_ON=true
        laptop;;
      e) EXTERNAL_ON=true
        external $OPTARG;;
      ?) print_usage
        exit 2;;
    esac
    done

    By adding a new modeline to the script, I could support more monitor models and switch between laptop monitor and external monitor, or use both monitors easily.

    Editing PDF on Linux

    June 18th, 2009 by Min Chen

    OpenOffice and PDFedit are two good tools for editing text in the pdf file. However, if you need to change the layout, add a picture etc, you need to find a new approach.

    Here’s what I usually do:

    1. using ghostscript to generate TIFF files using this command
      gs -sDEVICE=tiff24nc -r150×150 -sPAPERSIZE=letter -sOutputFile=<output dir>/<base filename>%03d.tif -dNOPAUSE -dBATCH — <pdf filename>

      This command will create 24bit colour 150×150 resolution TIFF files to <output dir> using the filename pattern as <base filename>001.tif to maximum <base filename>999.tif using letter size. For example, a 5 page PDF file will generate 5 tif files

    2. editing tiff page by page using gimp, then explicitly saving each page as tiff, compressed with JPEG
    3. combining each tiff into one multi-page tiff using tiffcp
      tiffcp -c jpeg src001.tif src002.tif …. src999.tif dst.tif
    4. using tiff2pdf to generate PDF file from dst.tif
      tiff2pdf -p letter -j -q75 -t “new PDF title” -o output.pdf dst.tif
      Using this command I will get a letter size pdf file, the tiff is JPEG compressed with 75% quality and the pdf title will be “new PDF title”

    A quick note in installing JDK on Fedora 8

    June 11th, 2008 by Min Chen

    There are only gcj and icedtea java in Fedora repository, but I need JDK from Sun, IBM, BEA installed. What I’ve done was to download rpm packages and install them. Then I added these JDKs to the alternatives by running

    sudo /usr/sbin/alternatives –install /usr/bin/java java /usr/java/jdk1.5.0_15/bin/java 3

    Then by running

    sudo /usr/bin/alternatives –config java

    I configured the defaut JDK in my environment.

    LDAP injection

    June 10th, 2008 by Min Chen

    Today I joined the SecureToronto meeting. When the presenter Nish Bhalla was talking about SQL injection, an audience rasied a question about LDAP injection. Nish explained the theory, but because of the tight schedule he didn’t show any examples.

    I may add some examples to this LDAP injection topic.

    LDAP v3 clearly defines the SEARCH operation. LDAP client has to provide a search FILTER for the LDAP server to locate the LDAP Entry (similar to the row in a relational database table). The syntax of the filter is also defined in the protocols. For example, to search an entry of user with userid=foo, and password=bar, the filter is
    (&(uid=foo)(userPassword=bar)). The LDAP search returns all entries that match this condition, or nothing (some LDAP may return an exception) if there’s no match.

    In this example, if someone uses userid=* and password=*, the filter would be (&(uid=*)(userPassword=*)) and this would match all entries that have both ‘uid’ and ‘userPassword’ attributes present.

    Another example, if someone uses userid=foo)(!, and password=bar), the filter would be (&(uid=foo)(!(userPassword=bar))), this would match all entries with uid=foo and password != bar.

    It’s very dangerous to verify userid and password by just checking the number of returned entries.

    Generally there are two approaches to authenticate a user using LDAP:

    1. Direct LDAP authentication. The application manages to get the user Distinguished Name and the password, then calls LDAP BIND operation to let the LDAP server verify the DN and password.
    2. The application searches LDAP for the user id. If there’s a match, the application gets the LDAP entry, and then compares the password in the LDAP entry with the user provided one.

    Unlike SQL injection, in either case, unless the application does something wrong in password comparison, it’s pretty hard to bypass the authentication by manipulated the user id and password.

    Using USB devices in VirtualBox

    June 2nd, 2008 by Min Chen

    I had problem using USB devices in VirtualBox 1.6.0 on my Fedora 8 laptop. All USB devices were grayed out in VirtualBox. By following the VirtualBox online help “11.5.7. USB not working”, I managed to get all USB devices working.

    Steps:

    1. Make sure your uid is in “vboxusers” group. You can do this using the “System->Administration->Users and Groups”, or edit the /etc/group directly, etc. My /etc/group has this line “vboxusers:x:501:mchen,root” where mchen is my uid
    2. Edit /etc/rc.sysinit, search for “mount - n -t usbfs /proc/bus/usb”, append “-o devgid=501,devmode=664″ at the end . Of course you need to change 501 to your vboxusers gid. In my situation, I changed
      if [ ! -d /proc/bus/usb ]; then
      modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
      else
      mount -n -t usbfs /proc/bus/usb /proc/bus/usb
      fi

      To

      if [ ! -d /proc/bus/usb ]; then
      modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb -o devgid=501,devmode=664
      else
      mount -n -t usbfs /proc/bus/usb /proc/bus/usb -o devgid=501,devmode=664
      fi

    3. Reboot the Fedora box and now all USB devices should be available for VirtualBox