Pages

Saturday, January 22, 2011

Reinstall Ubuntu after installing Windows

   Long story here.  Ubuntu is well documented and documentation updated. Ubuntu forums are great also. So I had Ubuntu Maverick already installed, and installed Windows XP. It wrote over the mbr. Now, to restoration:
  • I booted using an USB live image
  • Mounted the ext4 partition containing the Ubuntu 10 installation and restored burg:
    sudo burg-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda
    
    (0d104aff-ec8c-44c8-b811-92b993823444 is the label of the mounted Ubuntu root partition. I use burg, so for grub it's the same, except grub-install instead.)
  • To list Windows also in the menu list of grub,
    sudo update-burg 
    
    or is using grub2:
    sudo update-grub2
    
  • I also had to modify /boot/burg/burg.cfg file, because Windows installation was automatically detected on /dev/sda3 (hd0,3), instead of /dev/sda4 ((hd0,4)).
    Note: changes made manually in the burg.cfg file are not persistent after burg-update, because burg-update automatically generates it based on /etc/burg.d files.
Hope I didn't miss any steps. For more detailed information:

Install Windows XP from USB

    After trying many tutorials to install XP from USB, I found something that worked. The WinSetupFromUSB tool.  I've tested with Windows XP 32bit SP2 and SP3 and worked fast and easy. Some prerequisites:

  • USB stick >1GB
  • Windows XP CD/DVD or iso
  • WinSetupFromUSB program
Notes:
  1. First I tried with an XP sp3 iso(XP Performance Edition), modified with nLite,  I got an error (missing winnt32...). A trick I've read, if you make modify your iso with nLite, is to NOT remove "Manual Install and Upgrade” under Operating System Options.
  2. It's better to integrate SP3 if you have just sp1 or sp2.  Details, reason and ste by step instructions  here. (the process is called slipstreaming drivers)
   The usage is straight-forward, click-click.., select the source, format the USB device (I used the HP format tool, already integrated with WinSetupFromUSB ), select USB, click Go. The program takes care of making the device bootable, after copying XP files. 
   Change from BIOS to select USB as first device to boot, and you'll get to a grub4dos where you choose XP setup first part. 

Note: I had a problem at the second part. There are 8 options, one default, with the suggestion to choose one that works. For me it was the 4th.

Next, how to get grub (or burg) back in the mbr, after XP installation, to be able to boot the existent Ubuntu also.

Tuesday, January 18, 2011

Trace function calls with GCC

     With gcc it is possible to trace offline the execution of a program. GNU GCC has a feature that allows to gather a function call tree. When you compile your program with a special instrumentation flag, and link with a trace library that contains 4 specific functions, you can mark every entrance end exit from functions. From GCC manual:
-finstrument-functions
Generate instrumentation calls for entry and exit to functions. Just 
after function entry and just before function exit, the following 
profiling functions will be called with the address of the current 
function and its call site. (On some platforms, __builtin_return_address
 does not work beyond the current function, so the call site information 
may not be available to the profiling functions otherwise.)
          void __cyg_profile_func_enter (void *this_fn,
                                         void *call_site);
          void __cyg_profile_func_exit  (void *this_fn,
                                         void *call_site);
     
The first argument is the address of the start of the current function, 
which may be looked up exactly in the symbol table.
    If a function does not need to be instrumented, it can be excluded, using __attribute__ ((no_instrument_function)) in the function definition. (This helps to tell gcc to not instrument __cyg_* profiling functions.)
Profiling functions for enter and exit:
void __attribute__ ((no_instrument_function))
__cyg_profile_func_enter (void *func, void *caller);
void __attribute__ ((no_instrument_function))
__cyg_profile_func_exit (void *func, void *call_site);

    When an instrumented function is called,__cyg_profile_func_enter is also called, passing in the address of the function called as func and the address from which the function was called as caller. Conversely, when a function exits, the __cyg_profile_func_exit function is called, passing the function's address as func and the actual site from which the function exits as call_site.
    Within these profiling functions, you can record the address pairs for later analysis. To request that gcc instrument all functions, every file must be compiled with the options -finstrument-functions and-g to retain debugging symbols.
    So, now you can provide profiling functions to gcc that it will transparently insert into your application's function entry and exit points. But when the profiling functions are called, what do you do with the addresses that are provided? One option would be to just write the addresses to a file, together with the time function enters and exits.
    There are other 2 functions invoked: constructor when main is called, and destructor when the application exits. To create them, create 2 functions and apply constructor and destructor attributes to them:
void __attribute__ ((constructor))
trace_begin (void);
void __attribute__ ((destructor))
trace_end (void)
    In constructor and destructor you can open the trace files, and in __cyg_profile_func_* register info like function address, caller address, and time. 
To resolve the addresses to actual function names, the Addr2line tool (which is part of the standard GNU Binutils) is an utility that translates an instruction address and an executable image into a filename, function name, and source line number. The usage for this tool is as follows:
$ addr2line.exe -f -e prog 0x004011FF
f
/cygdrive/d/work/prog.c:35
    Here prog is the binary name, and 0x004011FF is the function address. The information returned gives the function name and location.
Here is an example on how to use this feature to link a small problem with the trace library and view a call tree.

Tuesday, January 11, 2011

Ubuntu 10 remove encryption from home directory

     I just installed Ubuntu 10.10 (great choice!). One nice feature that can be enabled at install time is to encrypt your home directory. Full details about this and usage in this wiki.  Anyway, encrypting the whole home folder introduced very small delays and I decided to keep my home unencrypted, but atill have a Private folder there that will be encrypted.  The steps to achieve this (if you already have your whole home encrypted):

  • You should not be logged on the system. One way to do this is to start Ubuntu in recovery-mode, and chose root prompt. That will drop you at # prompt. 
  • Your account should not be mounted (and it isn't if yo used the root prompt). Btw, when logged in, if you use 'T' flag at df ( $df -hT )  you can see the type of your home partition as ecryptfs,  mounted in /home/username.
  • Create a new folder that can be used lated as a new encrypted private folder, within your home (mkdir /home/username/Private
  • chown username.username /home/username/Private
  • Now modify in the file /home/.ecryptfs/username/.ecryptfs/Private.mnt the new encrypted path, (/home/username/Private)
  • Switch from root to username (#su - username)
  • Mount private encrypted files, by running the wrapper script ecryptfs-mount-private.  This will prompt for the username password. Now your private encrypted home folder will be mounted under the new specified path, /home/username/Private
  • Synchronize that with your /home/username: $cd ~/Private; rsync -av ./ /home/username/   
  • Exit ( $exit ) the shell where you're logged in as username and #reboot from root prompt.
  • After restart check that all the files are there, in your /home/username, unencrypted.
  • The content of the ~/Private can be deleted now. This folder can be further used as a storage for encrypted files
To check that files created in ~/Private are really encrypted:
  • create a secret file there
  • verify (with $df -T) the type of the filesystem mounted under /home/username/Private. It should be ecryptfs.
  • if you log in as another user, (or as root at the root prompt from rescue mode) you won't be able to mount it..
Very nice thing to make this feature really easy to use and document it
GG! 

Monday, January 10, 2011

USB Camera in Skype in Ubuntu 10.10

    I've connected a basic USB webcam. It seemed to work correctly, but in Skype it didn't start showing anything. It's detected by lsusb as Microdia:
$ lsusb 
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 0c45:612a Microdia PC Camera (SN9C325)
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
To see that the camera it's working correctly (has modules installed), I used cheese (sudo apt-get install cheese  and then Applications->Sound and Video->Cheese Webcam Booth and I see myself:). 
The workaround to work also in Skype is to specify some libraries at start, in the LD_PRELOAD environment variable. If I run Skype with:


sh -c 'export XLIB_SKIP_ARGB_VISUALS=1 && LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/bin/skype'


I got image, so I modified the command to start the program (Right click on Applications Menu, Edit Menus and edited the command for Skype).


Saturday, January 8, 2011

Install drivers for SIS 771/671 graphic card in Ubuntu 10.10 and 11.04

   My Esprimo Mobile V5535 laptop came with an unsupported by default graphic card SiS 771/671.

$ lspci -nn |grep -i vga
01:00.0 VGA compatible controller [0300]: Silicon Integrated Systems [SiS] 771/671 PCIE VGA Display Adapter [1039:6351] (rev 10)

After infinite searching (that took almost 4 hours:) on forums about that problem,  I finally got some working drivers (still without 3d acceleration that doesn't seem to be supported in the near future) from the 5th post from here (I still have the files if the links are not working).  This contains instructions also:

  • copy the driver (sisimedia_drv.so)  to /usr/lib/xorg/modules/drivers
  • modify (or copy xorg.conf if you don't have that file) /etc/x11/xorg.conf with the lines from xorg.conf contained in the archive.

Warning!!
Check the refresh rate supported by your monitor!  An incorrect setting may damage the monitor.I modified mine like below, in the "Monitor" section of xorg.conf:
    .....

#      HorizSync 30-63
#      VertRefresh 50-75
        HorizSync 28-72
        VertRefresh 43-60
   .....


P.S.:

  • If possible stay away from SiS graphics cards, at least until they decide to support Linux. Stick with Intel, AMD, nVidia.
  • SiS Graphic on Linux site contains lot of other drivers, that have been used  successfully by others in Linux 
  • http://ubuntuforums.org is a great resource   

Enjoy and hope it helps!

EDIT for Ubuntu 11.04 Natty :
Using the instructions on this post worked for me, and I encountered 2 issues:

1. I had a blank screen just after reboot (instead of grub). I solved that (or it solved by itself ?!) by rebooting with an ubuntu live usb and deleting some resolution modes from xorg.conf. (dont know if this actually was the solution but he computer booted correctly.
2. I had to modify in xorg.conf in the Device section Driver "sis", after 'sudo make install'

After that, because this graphic card does not support 3d acceleration, you have to install Unity 2d, (which looks kindof ok compared to the real deal). Complete instructions on installing Unity 2D in Ubuntu 11.04 here.

EDIT2:
Still on that blog, the drivers compiled!

Sunday, December 5, 2010

"Hello, World!" first shellcode - Part 1

Simple C program to print "Hello, World!". Compiled with gcc version 3.3.6 on Ubuntu 7.04

#include <stdio.h>

char SHELLCODE[]="\xeb\x13\x59\x31\xc0\xb0\x04\x31\xdb\x43\x31\xd2"
                 "\xb2\x0f\xcd\x80\xb0\x01\x4b\xcd\x80\xe8\xe8\xff"
                 "\xff\xff\x48\x65\x6c\x6c\x6f\x2c\x20\x77\x6f\x72"
                 "\x6c\x64\x21\x0a\x0d";

typedef void (*func_ptr)();

int main() {
    func_ptr f = (func_ptr)SHELLCODE;
    f();
}
To compile the source:
$ gcc my_shellcode.c
$ ./a.out 
Hello, world!
The char array represents position-independent instructions, that, when injected into a process, will be executed. But where did that came from?  Here's an .asm source to generate position-independent code that prints something:
BITS 32             ; tell nasm this is 32-bit code

jmp short one       ; jump down to a call at the end

two:
; ssize_t write(int fd, const void *buf, size_t count);
  pop ecx           ; pop the return address (string ptr) into ecx
  xor eax, eax      ; zero out full 32-bits of eax register
  mov al, 4         ; write syscall #4 to the low byte of eax
  xor ebx, ebx      ; zero out ebx
  inc ebx           ; increment ebx to 1, STDOUT file descriptor
  xor edx, edx
  mov dl, 15        ; length of the string
  int 0x80          ; do syscall: write(1, string, 14)

; void _exit(int status);
  mov al, 1        ; exit syscall #1, the top 3 bytes are still zeroed
  dec ebx          ; decrement ebx back down to 0 for status = 0   
  int 0x80         ; do syscall:  exit(0)

one:
  call two   ; call back upwards to avoid null bytes
  db "Hello, world!", 0x0a, 0x0d  ; with newline and carriage return bytes

Some things to pay attention to:
  • the jmp technique with 2 labels (the call in the second label will jump back, will avoid \x00 bytes)
  • xor eax, eax, to zero a register (xor instruction does not modify the flags registry, so it's better than something like for example sub eax, eax that also clears eax.)
  • inc, dec,  instructions that take less machine bytes than mov, so the size of the shellcode  is reduced
  • system calls number can be found in /usr/include/asm-i386/unistd.h (found out with find command ls /usr/include/ -name "*.h" |xargs grep execve , for example)
The asm source cannot be linked into an elf executable. But it can be compiled into machine instructions:
$ nasm shell.asm
$ hexdump -C shell
$ ndisasm shell
The output shows the actual machine code, interpreted as instructions. 
To be continued..