Skip to content

Commit 06e4a67

Browse files
authored
Merge pull request #1487 from HackTricks-wiki/update_Compiling_static_Nmap_binary_for_jobs_in_restricte_20251014_124512
Compiling static Nmap binary for jobs in restricted environm...
2 parents 2b22114 + ebe70bf commit 06e4a67

File tree

1 file changed

+138
-2
lines changed

1 file changed

+138
-2
lines changed

src/generic-methodologies-and-resources/pentesting-network/nmap-summary-esp.md

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ By default Nmap launches a discovery phase consisting of: `-PA80 -PS443 -PE -PP`
4545
- **`--badsum`:** It sends the sum wrong, the computers would discard the packets, but the firewalls could answer something, it is used to detect firewalls.
4646
- **`-sZ`:** "Weird" SCTP scanner, when sending probes with cookie echo fragments they should be dropped if open or responded with ABORT if closed. It can pass through firewalls that init does not pass through, the bad thing is that it does not distinguish between filtered and open.
4747
- **`-sO`:** Protocol Ip scan. Sends bad and empty headers in which sometimes not even the protocol can be distinguished. If ICMP unreachable protocol arrives it is closed, if unreachable port arrives it is open, if another error arrives, filtered, if nothing arrives, open|filtered.
48-
- **`-b <server>`:** FTPhost--> It is used to scan a host from another one, this is done by connecting the ftp of another machine and asking it to send files to the ports that you want to scan from another machine, according to the answers we will know if they are open or not. \[\<user>:\<password>@]\<server>\[:\<port>] Almost all ftps servers no longer let you do this and therefore it is of little practical use.
48+
- **`-b <server>`:** FTPhost--> It is used to scan a host from another one, this is done by connecting the ftp of another machine and asking it to send files to the ports that you want to scan from another machine, according to the answers we will know if they are open or not. [\<user>:\<password>@]\<server>\[:\<port>] Almost all ftps servers no longer let you do this and therefore it is of little practical use.
4949

5050
### **Focus Analysis**
5151

@@ -257,7 +257,143 @@ Moreover, probes which do not have a specifically defined **`servicewaitms`** us
257257
If you don't want to change the values of **`totalwaitms`** and **`tcpwrappedms`** at all in the `/usr/share/nmap/nmap-service-probes` file, you can edit the [parsing code](https://github.com/nmap/nmap/blob/master/service_scan.cc#L1358) such that these values in the `nmap-service-probes` file are completely ignored.
258258

259259

260-
{{#include ../../banners/hacktricks-training.md}}
260+
## Build a static Nmap for restricted environments
261+
262+
In hardened or minimal Linux environments (containers, appliances), dynamically linked Nmap binaries often fail due to missing runtime loaders or shared libraries (e.g., /lib64/ld-linux-x86-64.so.2, libc.so). Building your own statically linked Nmap and bundling NSE data allows execution without installing system packages.
263+
264+
High-level approach
265+
- Use a clean amd64 Ubuntu builder via Docker.
266+
- Build OpenSSL and PCRE2 as static libraries.
267+
- Build Nmap linking statically and using the included libpcap/libdnet to avoid dynamic deps.
268+
- Bundle NSE scripts and data directories with the binary.
269+
270+
Discover target architecture (example)
271+
```bash
272+
uname -a
273+
# If building from macOS/ARM/etc., pin the builder arch:
274+
docker run --rm --platform=linux/amd64 -v "$(pwd)":/out -w /tmp ubuntu:22.04 bash -lc 'echo ok'
275+
```
276+
277+
Step 1 — Prepare toolchain
278+
```bash
279+
set -euo pipefail
280+
export DEBIAN_FRONTEND=noninteractive
281+
apt-get update && apt-get install -y --no-install-recommends \
282+
build-essential ca-certificates curl bzip2 xz-utils pkg-config perl python3 file git \
283+
automake autoconf libtool m4 zlib1g-dev
284+
```
285+
286+
Step 2 — Build static OpenSSL (1.1.1w)
287+
```bash
288+
OSSL="1.1.1w"
289+
curl -fsSLO "https://www.openssl.org/source/openssl-$OSSL.tar.gz"
290+
tar xzf "openssl-$OSSL.tar.gz" && cd "openssl-$OSSL"
291+
./Configure no-shared no-zlib linux-x86_64 -static --prefix=/opt/ossl
292+
make -j"$(nproc)" && make install_sw
293+
cd /tmp
294+
```
295+
296+
Step 3 — Build static PCRE2 (10.43)
297+
```bash
298+
PCRE2=10.43
299+
curl -fsSLO "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-$PCRE2/pcre2-$PCRE2.tar.bz2"
300+
tar xjf "pcre2-$PCRE2.tar.bz2" && cd "pcre2-$PCRE2"
301+
./configure --disable-shared --enable-static --prefix=/opt/pcre2
302+
make -j"$(nproc)" && make install
303+
cd /tmp
304+
```
305+
306+
Step 4 — Build static Nmap (7.98)
307+
```bash
308+
NMAP=7.98
309+
curl -fsSLO "https://nmap.org/dist/nmap-$NMAP.tar.bz2"
310+
tar xjf "nmap-$NMAP.tar.bz2" && cd "nmap-$NMAP"
311+
export CPPFLAGS="-I/opt/ossl/include -I/opt/pcre2/include"
312+
export LDFLAGS="-L/opt/ossl/lib -L/opt/pcre2/lib -static -static-libstdc++ -static-libgcc"
313+
export LIBS="-lpcre2-8 -ldl -lpthread -lz"
314+
./configure \
315+
--with-openssl=/opt/ossl \
316+
--with-libpcre=/opt/pcre2 \
317+
--with-libpcap=included \
318+
--with-libdnet=included \
319+
--without-zenmap --without-ndiff --without-nmap-update
320+
# Avoid building shared libpcap by accident
321+
sed -i -e "s/^shared: /shared: #/" libpcap/Makefile || true
322+
make -j1 V=1 nmap
323+
strip nmap
324+
```
325+
Key points
326+
- -static, -static-libstdc++, -static-libgcc force static linkage.
327+
- Using --with-libpcap=included/--with-libdnet=included avoids system-shared libs.
328+
- sed tweak neuters a shared libpcap target if present.
329+
330+
Step 5 — Bundle binary and NSE data
331+
```bash
332+
mkdir -p /out/nmap-bundle/nmap-data
333+
cp nmap /out/nmap-bundle/nmap-linux-amd64-static
334+
cp -r scripts nselib /out/nmap-bundle/nmap-data/
335+
cp nse_main.lua nmap-services nmap-protocols nmap-service-probes \
336+
nmap-mac-prefixes nmap-os-db nmap-payloads nmap-rpc \
337+
/out/nmap-bundle/nmap-data/ 2>/dev/null || true
338+
339+
tar -C /out -czf /out/nmap-linux-amd64-static-bundle.tar.gz nmap-bundle
340+
```
341+
342+
Verification and ops notes
343+
- Use file on the artifact to confirm it is statically linked.
344+
- Keep NSE data with the binary to ensure script parity on hosts without Nmap installed.
345+
- Even with a static binary, execution may be blocked by AppArmor/seccomp/SELinux; DNS/egress must still work.
346+
- Deterministic builds reduce supply-chain risk vs downloading opaque “static” binaries.
347+
348+
One-liner (Dockerized)
349+
<details>
350+
<summary>Build, bundle, and print artifact info</summary>
351+
352+
```bash
353+
docker run --rm --platform=linux/amd64 -v "$(pwd)":/out -w /tmp ubuntu:22.04 bash -lc '
354+
set -euo pipefail
355+
export DEBIAN_FRONTEND=noninteractive
356+
apt-get update && apt-get install -y --no-install-recommends \
357+
build-essential ca-certificates curl bzip2 xz-utils pkg-config perl python3 file git \
358+
automake autoconf libtool m4 zlib1g-dev
359+
360+
OSSL="1.1.1w"; curl -fsSLO "https://www.openssl.org/source/openssl-$OSSL.tar.gz" \
361+
&& tar xzf "openssl-$OSSL.tar.gz" && cd "openssl-$OSSL" \
362+
&& ./Configure no-shared no-zlib linux-x86_64 -static --prefix=/opt/ossl \
363+
&& make -j"$(nproc)" && make install_sw && cd /tmp
364+
365+
PCRE2=10.43; curl -fsSLO "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-$PCRE2/pcre2-$PCRE2.tar.bz2" \
366+
&& tar xjf "pcre2-$PCRE2.tar.bz2" && cd "pcre2-$PCRE2" \
367+
&& ./configure --disable-shared --enable-static --prefix=/opt/pcre2 \
368+
&& make -j"$(nproc)" && make install && cd /tmp
369+
370+
NMAP=7.98; curl -fsSLO "https://nmap.org/dist/nmap-$NMAP.tar.bz2" \
371+
&& tar xjf "nmap-$NMAP.tar.bz2" && cd "nmap-$NMAP" \
372+
&& export CPPFLAGS="-I/opt/ossl/include -I/opt/pcre2/include" \
373+
&& export LDFLAGS="-L/opt/ossl/lib -L/opt/pcre2/lib -static -static-libstdc++ -static-libgcc" \
374+
&& export LIBS="-lpcre2-8 -ldl -lpthread -lz" \
375+
&& ./configure --with-openssl=/opt/ossl --with-libpcre=/opt/pcre2 --with-libpcap=included --with-libdnet=included --without-zenmap --without-ndiff --without-nmap-update \
376+
&& sed -i -e "s/^shared: /shared: #/" libpcap/Makefile || true \
377+
&& make -j1 V=1 nmap && strip nmap
378+
379+
mkdir -p /out/nmap-bundle/nmap-data \
380+
&& cp nmap /out/nmap-bundle/nmap-linux-amd64-static \
381+
&& cp -r scripts nselib /out/nmap-bundle/nmap-data/ \
382+
&& cp nse_main.lua nmap-services nmap-protocols nmap-service-probes nmap-mac-prefixes nmap-os-db nmap-payloads nmap-rpc /out/nmap-bundle/nmap-data/ 2>/dev/null || true \
383+
&& tar -C /out -czf /out/nmap-linux-amd64-static-bundle.tar.gz nmap-bundle \
384+
&& echo "===== OUTPUT ====="; ls -lah /out; echo "===== FILE TYPE ====="; file /out/nmap-bundle/nmap-linux-amd64-static || true
385+
'
386+
```
387+
388+
</details>
389+
390+
## References
261391

392+
- [Compiling static Nmap binary for jobs in restricted environments](https://www.pentestpartners.com/security-blog/compiling-static-nmap-binary-for-jobs-in-restricted-environments/)
393+
- [Static Nmap Binary Generator (helper tool)](https://github.com/0x5ubt13/static_nmap_binary_generator)
394+
- [OpenSSL sources](https://www.openssl.org/source/)
395+
- [PCRE2 releases](https://github.com/PCRE2Project/pcre2/releases)
396+
- [Nmap source tarballs](https://nmap.org/dist/)
262397

263398

399+
{{#include ../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)