Calling Windows APIs

Due playing with MS-Windows Exploitation development, I was using some C applications that calling Windows APIs and I wanted to give it a try and take it step by step.

The simplest example came to my mind is calling the MessageBoxA function. If we take a look at the MSDN of MessageBoxA function, we’ll find at very beginning the function description and its arguments and returns. At the Requirements section, we’ll find the required DLL to call MessageBoxA function which is User32.dll library.

  1. int WINAPI MessageBox(
  2. _In_opt_ HWND hWnd,
  3. _In_opt_ LPCTSTR lpText,
  4. _In_opt_ LPCTSTR lpCaption,
  5. _In_ UINT uType
  6. );

Let’s do it,

  1. require "Win32API"
  2. title = "Rubyfu!"
  3. message = "You've called the Windows API Successfully! \n\n@Runyfu"
  4. api = Win32API.new('user32', 'MessageBoxA',['L', 'P', 'P', 'L'],'I')
  5. api.call(0,message,title,0)

Calling Windows APIs - 图1

Source and explaination

That’s was really easy! but, Win32API is going to be deprecated or it’s already deprecated at the moment you read this part. Ruby have moved all dealing with C, dll functions to Fiddle class which is a wrapper of libffi C library which provides a portable interface to allow languages to call code from another language.

If we build our MessageBoxA script again using Fiddle it will be like

  1. # Load importer part of fiddle (ffi) library
  2. require 'fiddle/import'
  3. # int WINAPI MessageBox(
  4. # _In_opt_ HWND hWnd,
  5. # _In_opt_ LPCTSTR lpText,
  6. # _In_opt_ LPCTSTR lpCaption,
  7. # _In_ UINT uType
  8. # );
  9. # Create module as body for an importer instance
  10. module User32
  11. # Extend this module to an importer
  12. extend Fiddle::Importer
  13. # Load 'user32' dynamic library into this importer
  14. dlload 'user32'
  15. # Set C aliases to this importer for further understanding of function signatures
  16. typealias 'HWND', 'HANDLE'
  17. typealias 'LPCSTR', 'const char*'
  18. typealias 'LPCWSTR', 'const wchar_t*'
  19. typealias 'UINT', 'unsigned int'
  20. typealias 'HANDLE', 'void*'
  21. # Import C functions from loaded libraries and set them as module functions
  22. extern 'int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT)'
  23. end
  24. title = "Rubyfu!"
  25. message = "You've called the Windows API Successfully! \n\n@Runyfu"
  26. User32::MessageBoxA(nil, message, title, 0)

Source

As you can the script is getting much bigger but, important thing to mention is, Using Win32API is going to be a real pain for bigger or more complicated tasks, in another hand Fiddle is more elegant and readable than Win32API

At that point, I was wondering if I can write something like an old frind application call arwin which finds a Function location in a Windows library. With the help of MSDN LoadLibrary and GetProcAddress documentations let’s do it.

arwin.rb

  1. require 'fiddle/import'
  2. #
  3. # KING SABRI | @KINGSABRI
  4. #
  5. if ARGV.size == 2
  6. lpfilename = ARGV[0] # Library Name
  7. lpprocname = ARGV[1] # Function Name
  8. else
  9. puts "ruby arwin.rb <Library Name> <Function Name>"
  10. puts "example:\n arwin.rb user32.dll MessageBoxA"
  11. exit 0
  12. end
  13. module Kernel32
  14. # Extend this module to an importer
  15. extend Fiddle::Importer
  16. # Load 'user32' dynamic library into this importer
  17. dlload 'kernel32'
  18. # HMODULE WINAPI LoadLibrary(
  19. # _In_ LPCTSTR lpFileName
  20. # );
  21. typealias 'lpfilename', 'char*'
  22. extern 'unsigned char* LoadLibrary(lpfilename)'
  23. # FARPROC WINAPI GetProcAddress(
  24. # _In_ HMODULE hModule,
  25. # _In_ LPCSTR lpProcName
  26. # );
  27. typealias 'lpfilename', 'char*'
  28. typealias 'lpprocname', 'char*'
  29. extern 'unsigned char* GetProcAddress(lpfilename, lpprocname)'
  30. end
  31. address = Kernel32::GetProcAddress(Kernel32::LoadLibrary(lpfilename), lpprocname).inspect.scan(/0x[\h]+/i)[1]
  32. unless address.hex.zero?
  33. puts "\n[+] #{lpprocname} is location at #{address} in #{lpfilename}\n"
  34. else
  35. puts "[!] Could find #{lpprocname} in #{lpfilename}!"
  36. puts "[-] Function's name is case sensitive"
  37. end

Results

  1. [+] MessageBoxA is location at 0x77d8050b in user32.dll

Calling Windows APIs - 图2