- How do I invoke the MinGW cross-compiler on Linux?
- 4 Answers 4
- How To : Use MinGW to Compile Windows Exploits on Kali Linux
- Step 1: Installing MinGW-w64
- Cross Compiling with MinGW-64
- Step 2: Getting the Exploit Code
- Step 3: Compiling the Code
- Step 4: Running Your Compiled Program
- And This Is Just the Beginning
- Compile mingw on linux
- Overview
- Installing the prerequisites for cross-compilation
- Setting up the build directory and getting the sources
- Making cross-compilation easy
- Compiling LZO
- Compiling Zlib
- Compiling LibreSSL
- Compiling tinc
- Testing tinc
- Compiling Gnu11 for Windows with MinGW on Linux
- 1 Answer 1
- How to build using mingw32 on Linux? #154
- Comments
- Hacksawfred3232 commented Jul 31, 2019
- mosolovsa commented Aug 6, 2019 •
- dbaarda commented Aug 24, 2019
- Hacksawfred3232 commented Sep 29, 2019
- deajan commented Sep 30, 2019
- dbaarda commented Oct 1, 2019
- deajan commented Oct 1, 2019
- dbaarda commented Oct 2, 2019
- dbaarda commented Oct 2, 2019
How do I invoke the MinGW cross-compiler on Linux?
I have a project that I want to cross-compile for Windows. I have the appropriate Makefile and everything works with g++ . I’ve run
and downloaded 500 MB of packages, but I cannot find out how to actually run it. There is no mingw executable, so how do I actually compile with it?
4 Answers 4
If you look at the file lists on the Ubuntu package webserver for mingw-w64 ‘s constituent packages:
You can see that mingw-w64 provides a toolchain, i.e. a set of alternative tools (compiler, linker, headers, etc.) used to compile your code for another system.
Assuming you want to compile C++ code for a 64-bit system, you’ll need to use /usr/bin/x86_64-w64-mingw32-g++-win32 . You can use the CXX environment variable to tell most Makefiles to use that compiler to compile code.
Another option is to take a look at Mingw Cross Environment (MXE), which is specifically targetting at cross compiling from Linux to Windows (and lately also to Mac). The package has bult-in xupport for a large number of libraries and is actively being developed. Just take a look at the website to find out if it suits your needs.
By the way,it is suggested you use the development rather than the release version. This is because release versions are generally outdated very fast, due to package maintainers (of the libraries) changing URLs resulting in the MXE release version becoming broken. The development version is generally more up-to-date.
Источник
How To : Use MinGW to Compile Windows Exploits on Kali Linux
Cross-compilation allows you to develop for one platform (like Kali) and compile to run on a different platform (such as Windows). For developers, it means that they can work on their platform of choice and compile their code for their target platform. For hackers, it means we can compile exploit code for Windows from Kali.
The majority of available hacking tools are targeted towards Linux, assuming most hackers will be operating on some form of Linux environment. Most Linux distros include a compilation toolchain, which will happily build local exploit code for Linux targets all day. If the machine we’re targeting is Windows, Metasploit has plenty of Windows exploits, but this leaves us unable to compile any Windows exploits ourselves. This is where cross-compilation comes into play.
In this article, we’ll go through the installation and basic usage of MinGW-w64 on Kali Linux to compile local exploit code. The MinGW-w64 project is a complete runtime environment for GCC (GNU Compiler Collection) to support binaries native to Windows 64-bit and 32-bit operating systems.
Step 1: Installing MinGW-w64
First, we will need a root shell. Kali Linux defaults to a single root user on install. If you have made no changes to the system, you already have a root shell when you open your terminal! In my case, I will be using SSH to connect to my headless Kali system.
Before we install MinGW-w64, we will want to update our available packages and upgrade out-of-date packages on our system. This can be done by typing the following command.
Some of our users may be more familiar with apt-get update && apt-get upgrade. Both of these commands would accomplish the same task. My preference is towards apt. If you haven’t looked into the many features of apt, I would recommend doing some reading on it.
Once our package lists are updated and our system is upgraded, it’s time to install MinGW-w64. Simply issue the command below.
Kali will prompt you to confirm the installation. Press enter to proceed.
Cross Compiling with MinGW-64
Cross-compiling exploits can be challenging. Exploit code is developed in varying environments and intended to work on a specific version and patch level of the software it is targeting. Many public exploits do not work straight out of the box. When cross-compiling, you will run across bad code, shell code that needs to be swapped, and other issues. With that out of the way, let’s try out our new cross-compiler.
Step 2: Getting the Exploit Code
First, the exploit code itself. The best place to grab raw exploit code when using Kali Linux is the SearchSploit tool. Exploits can also be found on the web at exploit-db.com, securityfocus.com, and on many more sites. When compiling and running pre-written exploits, it is important that you trust the source or analyze the code yourself. There are plenty of malicious exploits out there and you don’t want to run into one!
First, I search using SearchSploit, by typing the following into the terminal.
The «e» argument specifies exact match.
In the results from my search, I can see matching exploits. Though the highlighted result is labeled as a text file by SearchSploit, it is, in fact, C code.
To copy the code into your directory, type the following command.
Next, it’s important to view the code before attempting compilation. I viewed the code with the command:
Immediately, there is a problem. The banner in the code isn’t commented out, which would lead to the compiler trying to read it, creating compilation errors. Good thing we checked.
Beyond fixing that, there’s nothing special happening here. This is just basic shell code. It would give us cmd.exe. If we wanted to spice it up a bit, we could remove the existing shell code and replace it with something generated by Msfvenom, though Msfvenom can also generate to PE format.
Since we know this code won’t compile without removing the banner, we will make a copy of it to edit. Type the following command in the terminal.
Next, open your favorite text editor and remove the uncommented text banner. When you have finished, your code should look like the image below.
The banner has been cleaned out, leaving behind around 40 lines of C.
Step 3: Compiling the Code
To compile the code, run the command:
This command works for C files on x86 64-bit architecture. The «-o» determines the name of the compiled binary. If we were compiling for 32 bit, we would use a command that looks like this.
Since this code is for 64-bit machines, we use the 64-bit version of the cross compiler. MinGW comes with quite a few options for cross-compilation. In order to see a list of available compilers installed with the MinGW package, enter the following command.
As we can see, there are quite a few options available.
Step 4: Running Your Compiled Program
The final step is to run your compiled program. I’ll be testing it out on my Windows VM.
There are many ways to transfer files to your VM, but the easiest right now is simply by hosting the file on my Kali box and pointing IE at it. In order to do this, run the commands:
On your target system, navigate to http://your.kali.machine/shell.exe (with the «your.kali.machine» replaced by your Kali system’s IP address). You will be presented with a prompt. Click on the save button.
Lastly, run the resulting «shell.exe» from a command prompt. When shell.exe is run, it immediately terminates, though we do see the cmd window open for a second. This could be adjusted by changing the shell code in the source and then recompiling it.
And This Is Just the Beginning
This article covers extremely basic usage of MinGW-64. Compiling exploits can be very difficult and time-consuming. Exploits found on the web can be riddled with bugs, syntax errors, even malicious code. Due to the time it takes to go through and fix broken exploit code, I usually keep working compiled exploits on my system and back them up regularly.
I usually will only cross-compile an exploit if it is absolutely necessary. Sometimes, it’s less time-consuming to find another route than it is to debug someone else’s exploit code. In cases where the code is so bad it might require a near re-write in order to function in your environment, I would recommend taking some time and porting it into Metasploit. If you’re already rewriting it, why not make it modular?
Stay tuned for more, you can leave any questions in the comments.
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Источник
Compile mingw on linux
This howto describes how to create a Windows binary of tinc. Although it is possible to compile tinc under Windows itself, cross-compiling it under Linux is much faster. It is also much easier to get all the dependencies in a modern distribution. Therefore, this howto deals with cross-compiling tinc with MinGW under Linux on a Debian distribution.
The result is a 32-bit executable. If you want to create a 64-bit executable, have a look at the 64-bit cross-compilation example.
Overview
The idea is simple:
- Install MinGW and Wine.
- Create a directory where we will perform all cross-compilations.
- Get all the necessary sources.
- Cross-compile everything.
Installing the prerequisites for cross-compilation
There are only a few packages that need to be installed as root to get started:
Other Linux distributions may also have MinGW packages, use their respective package management tools to install them. Debian installs the cross-compiler in /usr/i686-w64-mingw32/ . Other distributions might install it in another directory however, for example /usr/i686-pc-mingw32/ . Check in which directory it is installed, and replace all occurences of i686-w64-mingw32 in this example with the correct name from your distribution.
Setting up the build directory and getting the sources
We will create a directory called mingw/ in the home directory. We use apt-get to get the required libraries necessary for tinc, and use git to get the latest development version of tinc.
Making cross-compilation easy
To make cross-compiling easy, we create a script called mingw that will set up the necessary environment variables so configure scripts and Makefiles will use the MinGW version of GCC and binutils:
If $HOME/bin is not already part of your $PATH , you need to add it:
We use this script to call ./configure and make with the right environment variables, but only when the ./configure script doesn’t support cross-compilation itself. You can also run the export commands from the mingw script by hand instead of calling the mingw script for every ./configure or make command, or execute $HOME/bin/mingw $SHELL to get a shell with these environment variables set, but in this howto we will call it explicitly every time it is needed.
Compiling LZO
Cross-compiling LZO is easy:
Compiling Zlib
Cross-compiling Zlib is also easy, but a plain make failed to compile the tests, so we only build the static library here:
Compiling LibreSSL
Tinc can use either OpenSSL or LibreSSL. The latter is recommended.
Compiling tinc
Now that all the dependencies have been cross-compiled, we can cross-compile tinc. Since we use a clone of the git repository here, we need to run autoreconf first. If you want to cross-compile tinc from a released tarball, this is not necessary.
Testing tinc
Since Wine was installed, you can execute the resulting binary even on Linux. Wine does not provide a TAP-Win32 device, but you can use the DeviceType = dummy option to test it without. The following command should work in any case:
Источник
Compiling Gnu11 for Windows with MinGW on Linux
I have a C program; it compiles and links with gcc -std=gnu11 iter.c -o iter because I am using some GNU string.h extensions like strndup , strnlen and strsep .
I want to compile this program for Windows using the package i686-w64-mingw32-gcc on Ubuntu Linux.
I have installed all the required MinGW runtime and libraries, but MinGW seems to ignore the -std=gnu11 directive. It’s not ignoring the option entirely, because -std=asdf is «unrecognised», but it refuses to cross-compile GNU C.
Is there a way I can cross-compile for Windows a program using GNU extensions?
1 Answer 1
As discussed in comments, although MinGW provides many GNU programs, including GCC, it does not provide the GNU C library. C programs built by GCC for the MinGW environment rely on Microsoft’s C runtime library. They can use GNU language extensions, but not, generally, GNU library extensions that are not also implemented by Microsoft. On the other hand, they can rely on Microsoft library extensions that are not implemented by GNU, though they may need to provide their own headers.
If your program relies on functions that may or may not be available in the target environment (e.g. Windows / MinGW) then the usual approach is to use conditional compilation directives and maybe macros to substitute alternatives to those functions in builds for targets that do not provide them. Depending on the specific function, that may mean you conditionally provide your own implementation.
How you integrate conditional compilation into your build system is a much broader question. If you are using the Autotools then facilities for this sort of thing are among its core features. You can do similar with CMake, and probably with most other popular systems. If you’re using a hand-rolled build system then of course you have both the freedom and the obligation to come up with your own approach.
Источник
How to build using mingw32 on Linux? #154
Comments
Hacksawfred3232 commented Jul 31, 2019
Requesting this because I need rdiff as part of a requirement on a python lib I’m building. The API that my lib talks to requires a rsync signature file along with the main file or a rsync delta when uploading to their services. I only have a small server with about 3 GB of memory and a old processor, so I can’t use WINE. Don’t even know if cygwin works under wine and if anything that is built under cygwin can be ran on plain Windows.
The text was updated successfully, but these errors were encountered:
mosolovsa commented Aug 6, 2019 •
I’m not sure if this is what you want, but:
directory with CMakeFile.txt located
mkdir build
cd build
cmake -DMINGW=ON -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY -DCMAKE_C_COMPILER=i686-w64-mingw32.static-gcc ..
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY — to prevent compiling simple executable and trying to run it during cmake build.
i686-w64-mingw32.static-gcc must be path to your mingw c compiler.
dbaarda commented Aug 24, 2019
With the merge of #161 a bunch of problems building with MinGW have been fixed. It should now be possible to build it using mingw32 and cmake without forcing a static library.
Please try it and let us know if there are any remaining problems or tricks to make it work. I welcome any pull requests with documentation to make this easier.
Hacksawfred3232 commented Sep 29, 2019
Sorry for not responding sooner! Trying to compile with mosolovsa’s command minus the «STATIC_LIBRARY» flag returns this:
(Oh and by the way, static-gcc does not exist on my system)
deajan commented Sep 30, 2019
I also cannot build latest librsync 2.1.0 with mingw.
I need to build a static librsync lib using mingw, so I change CMakeLists.txt from
This used to work with librsync 2.0.2 but does not anymore.
I’ve applied patch from #161 and tried again, I get the same following output:
I’ve also tried to build current git master, got the same results.
Does anyone have an idea what’s changed so that static builds fail ?
dbaarda commented Oct 1, 2019
To make librsync static compile you need to also add
target_compile_options(rsync PRIVATE -DLIBRSYNC_STATIC_DEFINE)
This ensures that LIBRSYNC_EXPORT is defined correctly for static linking in librsync_export.h.
However, I’d also like to figure out why dynamic linking is not working.
deajan commented Oct 1, 2019
@dbaarda static compilation now works with the option you provided.
Btw, shared compilation works out of the box for me with current git master.
Maybe you could consider releasing librsync 2.1.1 soon, or at least an interim build with the mingw fixes applied ?
Thank you for your help.
dbaarda commented Oct 2, 2019
Do we want to make static compilation a CMake configurable option? It looks like the way to do this is make BUILD_SHARED_LIBS an option and remove the explicit SHARED|STATIC from add_library();
We’d also need to conditionally set LIBRSYNC_STATIC_DEFINE if BUILD_SHARED_LIBS is OFF.
dbaarda commented Oct 2, 2019
It’s not entirely clear to me what is/isn’t working with git master right now.
deajan: are you using mingw on windows, or cross-compiling to windows on linux? Could you give a quick
5 line howto on what you are doing that works?
Hacksawfred3232: Is it still broken for you? The following seems to have good information on how to make mingw cross-compiling work with cmake;
I’m happy to accept pull requests to make this easier, either by documenting it better, or improving our cmake configuration for that use-case.
I also have a pull request on the way to add BUILD_SHARED_LIBS as a cmake option for building librsync as a static library.
Источник