Wednesday 19 October 2011

X86 assembly with GAS on Windows

I've been reading "Professional Assembly Language" by Richard Blum. The book gives a good overview of Intel architecture and takes you through from very basic samples. The sample assembly programs in the book assume you are on Linux and are using the gnu assembler however it is simple enough to get working on Windows. I do have a Linux box but I wanted to try the sample programs on my laptop which is Windows 7 (I couldn't get Ubuntu stable, too much hassle with the WiFi card and also the battery life is way better with Windows). Firstly download MinGW from:

http://www.mingw.org/

This will provide you with the Gnu Assembler (as.exe under MinGW\bin). Now most of the samples will run fine up-to the last part where they try to output to console. The book gives the following instructions to output a string:

movl $4, %eax
movl $1, %ebx
movl $output, %ecx
movl $42, %edx
int $0x80.

This is from Page 80 of the book. EAX contains the system call value, EBX is the file descriptor to write to, ECX is the start of the string and EDX is the length. Now to do the same on Windows we are going to utilise a Win32 system call from the kernel32.dll:


pushl $-11
call _GetStdHandle@4
mov %eax, handle
pushl $0
pushl $written
pushl $42
pushl $output
pushl handle
call _WriteConsoleA@20


pushl $0
call _ExitProcess@4

This is equivalent to the following C code:


handle = GetStdHandle(-11);
WriteConsole(handle, &msg[0], 13, &written, 0);
ExitProcess(0);

I got the above from this site http://www.cs.lmu.edu/~ray/notes/x86assembly/. Its a very useful page showing you how to call C libraries / system calls on Windows and Linux.

I will now apply the above to a sample from the book. The sample program on page 77 will call the CPUID instruction and output the result. The code below is the same as the sample bar the final output. To build this you need to link in the kernel32.dll by issuing the ld command after calling the assembler:


as -o cpuid.o cpuid.s
ld -o cpuid.exe cpuid.o -lkernel32

Contents of cpuid.s:

.section .data

output:
  .ascii "The processor vendor ID is 'xxxxxxxxxxxxx'\n"
handle: .int 0
written: .int 0


.section .text
.globl _start
_start:
  movl $0, %eax
  cpuid
  movl $output, %edi
  movl %ebx, 28(%edi)
  movl %edx, 32(%edi)
  movl %ecx, 36(%edi)
  pushl $-11
  call _GetStdHandle@4
  mov %eax, handle
  pushl $0
  pushl $written
  pushl $42
  pushl $output
  pushl handle
  call _WriteConsoleA@20


  pushl $0
  call _ExitProcess@4

Saturday 8 October 2011

Installing OpenLDAP on Windows 7

The following is a simple guide to installing OpenLDAP for the purpose of trying it out in a dev environment. The installation is on Windows 7. I've written this up as it is something I have to do infrequently and so forget the detail each time. The other online tutorials never seem to go as far as connecting an LDAP browser to the directory server you have just installed.

Download the MSI from http://www.userbooster.de/en/download/openldap-for-windows.aspx

The installation notes are http://www.userbooster.de/en/support/feature-articles/openldap-for-windows-installation.aspx. The LDAP requires a database repository, the "Backend Configuration"dialog allows you to choose from BDB, LDAP, LDIF, SQL-Server:



The easiest option is the LDIF backend as this is merely a file directory of LDIF files

Running the LDAP

You can either start with windows service or just run the "run.cmd" file that is provided in the C:\Program Files (x86)\OpenLDAP\run folder. I prefer to just run the cmd file as its not something I need running all the time and with a command console any errors are displayed immediately. Launch "run.cmd" as Administrator (right click the icon and select "run as Administrator"). If you have windows firewall running it will prompt you to allow it access. Select the "Private networks, such as my home or work network" option. You need to leave that command window open, it is the Open LDAP process. To stop Open LDAP just close the window.

Connect an LDAP browser

Once installed the cn=Manager,dc=maxcrc,dc=com user is available to bind with but the dc=maxcrc needs adding before you can successfully connect an LDAP browser. OpenLDAP has command line utils in the ClientTools folder to allow you to perform this. CD to this folder (c:\Program Files (x86)\OpenLDAP\ClientTools) then paste the following command:
ldapmodify.exe -a -x -D cn=Manager,dc=maxcrc,dc=com -w secret -f ..\maxcrc.ldif


If successful you'll see the following output:


adding new entry "dc=maxcrc,dc=com"


adding new entry "ou=People,dc=maxcrc,dc=com"


You now have an organisational unit called "People" under the dc maxcrc. This is somewhere you can start creating new user objects (or whatever type of object you want.) Now we are going to connect the LDAP browser Jxplorer. Download and install from this site http://jxplorer.org/downloads/users.html

There are no configuration options during the install. Run Jxplorer and then from the File menu select connect. You will see and "Open LDAP/DSML Connection" dialog. Enter the details as follows:



and then click ok. After a short pause (5-10 seconds on my laptop but it is only an i3 1.33ghz) and the explorer pane on the left should be populated with a small tree structure:


We are now going to add a new user. Select the People Organisational unit then type Ctrl+n. Select inetorgperson from the "Available classes" window and enter cn=user1 for the RDN:


Click the OK button. You will now see the "Table Editor" in the right hand pane. The fields in bold are mandatory for the given object class. We need to populate the sn field before we can add our new user:



enter a surname and then press the "Submit" button at the bottom of the pane. Our user has now been added to the directory server. If you check this folder "C:\Program Files (x86)\OpenLDAP\ldifdata\dc=maxcrc,dc=com\ou=people" you will see a new file created called "cn=user1". Do not tweak these files direct, use the LDAP browser.