Cybersecurity tools are not always ready to be used or run in operating systems, many times we will not find applications in their source code or binary, so know the ways to compile code in Linux is an essential convention on the way to becoming in a security expert.
A compiler allows us to translates the high-level code into low-level or machine code. There are many ways to compile code in Linux If you want to compile C code is necessary to have installed in your system a gcc compiler, which is responsible for translating the high-level code into binary code understandable by the machine. In the same way, programming languages such as Ruby, Python, and others use their own compiler to be able to be executed in the system.
Binary and Source Code
Before running a program in Unix-like systems, first, we should compile and then execute. A program could be available in a binary format or source code.
A binary is a package that has already been pre-compiled, in general, a binary match with the OS’s platform, which means that the application has been adjusted to a specific architecture, for example, we can find binary .deb for 64AMD or .rmp for i386. We need to put this binary into our filesystem and run a packages manager like apt or yum. In theory, it could already be executed for its use but if the dependencies or libraries are not installed in the system, the program will not work. That is why we must be sure that in the system there is everything necessary so that the application can be executed.
Is important to understand that not always we will find a package for a given system, so the option is to compile the source code since it is not adjusted to any architecture or system. The source code, we could say that it’s the RAW code with a defined format so it can be compiled. In general, the source code is packaging into a .tar, .zip archive compress.
Ways to compile source code in Linux:
In Linux we can compile code in different ways, using the language of the compiler itself or by using operating system tools.
Using a Language compiler
For our LAB we will use ubuntu-infosecaddicts VM and the exploit that we find in https://github.com/Eugnis/spectre-attack/archive/master.zip, this exploit is done in C language and we will use GNU gcc compiler. This exploit allows us to attack processors breaking the isolation and extracting data from a memory.
Most Unix-like systems are integrated with the gcc compiler. GCC is an integrated compiler of the GNU project for C, C ++, Objective C and Fortran; is able to receive a source program in any of these languages and generate a binary executable program in the language of the machine where it has to run. The acronym GCC means “GNU Compiler Collection”. Originally it meant “GNU C Compiler”; GCC is still used to designate a compilation in C. G ++ refers to a compilation in C ++.
First, we can check if gcc is installed:
gcc <sourcecode.c> [options] <output>
2. unzip master.zip
3. Enter to the directory uncompress
gcc Source.c -o spectre.out
(if we open spectre.out with any text edit we will see that it is not human readable).
If the processor is vulnerable we will see the message “The Magic Words are Squeamish Ossifrage” in memory.
In this case, gcc creates the binary spectre.out which is the executable program itself.
Using the operating system tools
Unix-like systems have several tools that help when compiling code manually, one of them is the very well know GNU make.
The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.
A project needs to meet some criteria so that it can be compiled with the make tool. Each project needs a Makefile. A Makefile is a script that describes the project structure, namely, the source code files, the dependencies between them, compiler arguments, and how to produce the target output.
Examining our master.zip file, we see the file makefile, next, we compile our source code:
- Into the directory cd spectre-attack-master
- Run make makefile
This action will create the binary spectre.out similar to the previous process using gcc as a compiler.