π€Cross-compiling Binaries
Compiling exploits for windows on Linux can be a bit of a hassle. It depends on OS (windows, linux,...) and the architecture (x86, x64). Compiling executable on one OS/arch to be used on different one is called cross-compiling. Thanks to diingus, i'll cover here all four most common cross-compile combination. I assume that you're using gcc with all dependencies preinstalled.
Windows
x86
x86 means 32 bit architecture
In case when code uses/includes winsock 2
x64
x64 means 64 bit architecture. Sometimes also refered as amd64.
Linux
x86
-m32 is optional if host architecture is x32
x64
target specific
--hash-style=both
Depending on how your distro's GCC was configured, hash tables can be different. AFAIK most use either both or gnu styles by default. Both simply means that linked files will include, both gnu and sysv hashtables. This shouldn't matter unless you try to run your program on a system with dynamic linker which does not understand GNU hashtables. In that case, if program was built with -Wl,--hash-style=gnu, you'll get an error at startup about unsupported hashtable format.
compiling dynamic/static libraries
If you need to build a library and link it to the program, this is the way to go.
NOTE: all different build types from above are applicable here as well (win/lin, x86/x64)
These are the files used in all examples, so you can repeat everything by copy/paste commands.
library.h
library.c
program.c
Build a dynamically linkable library (so)
Check the output file
Compile and link the program, using previously built library
NOTE: -lrary means library; 'lib' will be added by the compiler, so if you want to link something called 'library', you need to remove 'lib' part coz it will be added automatically.
Check the output file
NOTE: in case of an error (like shown above), it means that loader doesn't know how to find library.so (the fact that it is in the same folder as program wasn't enough for loader to find it). The loader searches for libraries based on environment variable LD_LIBRARY_PATH; so, if you want to use the library.so (or any other missing library), you need to either add the folder which contains the missing to LD_LIBRARY_PATH, or to copy library.so to the place where other libraries (so files) reside.
WORKAROUND
In some cases you can link statically by compiling static library (.a) and then compile the program using and including that static library. That means that the library itself will be included in the final program. That may not work in some cases + it increases size of the final file. The example follows:
Compile to an object file
Make an archive out of it
Check the output files
compile with static libs
IMPORTANT: the order of the arguments is VERY IMPORTANT; if you make a change in order, build will fail !!!
Check the output file
'not a dynamic executable' - means that we did what we wanted - no dynamic executable was required, which means that everything app needs is already inside the executable.
_GNU_SOURCE macro
Sometimes, you will have to use additional functionalities in C which are not provided by standard C. In that case, you need to use additional functionalities, which can be turn on by macro _GNU_SOURCE.
From manual:
Note: _GNU_SOURCE needs to be defined before including header files so that the respective headers enable the features. For example:
_GNU_SOURCE can be also be enabled per compilation using -D flag:
(-D is not specific to _GNU_SOURCE but any macro be defined this way).
Last updated