Skip to content


iptables rules to redirect incoming external requests to loopback service

The problem: some web-server listens on 127.0.0.1:8090. How to make in accessible from certain external IP using iptables rules?

Solution:

# 1. Let eth0 accept and route packets to/from 127.0.0.0/8
sysctl -w net.ipv4.conf.eth0.route_localnet=1
echo 'net.ipv4.conf.eth0.route_localnet = 1' > /etc/sysctl.d/90-route-localnet.conf
 
# 2. Redirect the allowed client's traffic to the loopback listener
iptables -t nat -A PREROUTING -i eth0 -p tcp -s 100.64.0.2 --dport 8090 -j DNAT --to-destination 127.0.0.1:8090
 
# 3. Close the hole route_localnet opens (see below), then accept the DNATed flow.
# After DNAT, INPUT sees dst=127.0.0.1, so match on that, not the public IP.
iptables -I INPUT 1 -i eth0 -d 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
iptables -I INPUT 2 -i eth0 -p tcp -s 100.64.0.2 -d 127.0.0.1 --dport 8090 -m conntrack --ctstate DNAT -j ACCEPT

We can’t DNAT to 127.0.0.1 with iptables rules alone. By default the kernel drops any packet that arrives on a non-loopback interface with a destination in 127.0.0.0/8, because it treats it as a “martian” address. You have to allow this with route_localnet on the external interface first. After that, it’s a standard DNAT.

In the rules above, eth0 is the external interface and 100.64.0.2 is the allowed client.

The risky part - route_localnet=1 means any host that can put packets on that interface’s L2 segment can send traffic with destination 127.0.0.1 to your MAC. That traffic would reach every loopback-only service on the box, not just this one. (See CVE-2020-8558 in kube-proxy for details). The DROP rule in step 3 handles this by dropping everything to 127/8 on eth0 that wasn’t created by our DNAT. We have to keep that rule above any broad ACCEPT in INPUT.

Test from 100.64.0.2 with curl -v http://:8090/ should work. Curling the public IP from the server won’t work, because locally generated traffic skips PREROUTING.

For debugging: conntrack -L -p tcp --dport 8090 shows whether the DNAT is happening. sysctl -w net.ipv4.conf.all.log_martians=1 will log drops in dmesg if route_localnet isn’t taking effect for some reason. If the OUTPUT policy is DROP, we’ll also need to allow the reply traffic.

There are two cleaner alternatives to the proposed solution:

1. Rebind the server to the public IP or 0.0.0.0 and filter with a plain -s 100.64.0.2 --dport 8090 -j ACCEPT. Then we don’t need route_localnet and NAT.
2. A reverse proxy (HAProxy, nginx, or even socat) listening on the public IP and forwarding to 127.0.0.1:8090. This keeps the loopback boundary intact. The trade-off is that the app sees the proxy’s IP unless we pass X-Forwarded-For HTTP-header or the PROXY protocol.

Posted in *nix, Howto.

Tagged with , , , .


NTFS partition with windows 7 data recovery in linux

When I tried to mount NTFS partition with windows 7 in fedora linux 42, I got the error: "The MFT is not self-contained: No such file or directory Failed to load $MFT: No such file or directory".

Here are some ideas how to deal with this problem.

This error typically means the NTFS filesystem is in an inconsistent state — most commonly because Windows was not shut down cleanly (hibernate, fast startup, or crash). Windows Fast Startup / Hibernation — Windows 8+ (and sometimes 7) leaves the filesystem in a "dirty" state when using fast startup or hibernate. The partition is still "owned" by Windows and locked. Unclean shutdown means power loss or crash left the journal unfinished. Corrupted MFT — Master File Table is damaged (less common, more serious).

Fix options

Option 1 — Boot into Windows 7 and do a full shutdown (not hibernate): Start → Shutdown (hold Shift if needed to force full shutdown). Then try mounting in Fedora again.

Option 2 — Fix with ntfsfix (quick Linux-side fix)

umount /dev/sdXN # First make sure the partition is NOT mounted
ntfsfix /dev/sdXN
mount /dev/sdXN /mnt/windows # Try mounting again

ntfsfix clears the dirty flag and replays the journal. It is not a full fsck — it just makes the partition mountable. Replace sdXN with your actual partition (e.g. sda2, nvme0n1p3).

Option 3 — Run chkdsk from Windows. If ntfsfix doesn't help, boot from a Windows installation media or recovery environment and run:

chkdsk C: /f /r

This does a thorough repair of the filesystem including MFT issues.

Option 4 — Force mount (read-only, data recovery only). If you just need to read files and can't repair right now:

mount -t ntfs-3g -o ro,recover /dev/sdXN /mnt/windows
mount -t ntfs3 -o ro /dev/sdXN /mnt/windows # Or with ntfs3 kernel driver

Option 5 — MFT is actually corrupted (serious case). If all above fails, the MFT itself may be damaged. Try using the MFT mirror backup:

ntfsfix -b /dev/sdXN  # restore MFT from backup
dnf install testdisk
testdisk /dev/sdX     # Or use testdisk for deeper recovery

Option 6 — Mount read-only ignoring errors. Try forcing the ntfs3 kernel driver with recovery options:

mount -t ntfs3 -o ro,norecover,force /dev/sdXN /mnt/recovery # Try with different options
mount -t ntfs-3g -o ro,rescue,ignore_safe_hibernation /dev/sdXN /mnt/recovery # Or with ntfs-3g

Option 7 — If standard mounting failed, here are progressively more aggressive recovery approaches:

dnf install testdisk # includes both photorec and testdisk tools
testdisk /dev/sdX    # testdisk - for partition/filesystem structure recovery
photorec /dev/sdX    # photorec - for file content recovery by scanning raw data

photorec doesn't need a working filesystem — it scans raw sectors looking for known file signatures (PDF, JPEG, DOCX, etc.). It recovers content but loses original filenames and directory structure.

Option 8 — Scalpel (file carving). Similar to photorec but more configurable:

dnf install scalpel 
vim /etc/scalpel/scalpel.conf # uncomment file types you need
scalpel /dev/sdXN -o /mnt/recovered/

Option 9 — ntfsundelete / ntfscat (ntfs-3g tools)

dnf install ntfs-3g
ntfsundelete /dev/sdXN -s # List recoverable files without mounting
ntfscat /dev/sdXN -i 12345 > recovered_file.txt # Read a specific file by inode number
ntfsls -i /dev/sdXN # List all files with inodes

Pro tip: When SMART attributes are not so good (especially if the drive has bad sectors) always work on a disk image, not the real drive because working directly on a failing drive risks making things worse.

ddrescue -d -r3 /dev/sdXN /mnt/rec.img /tmp/rec.log # Clone to image file skipping bad sectors
losetup /dev/loop0 /mnt/external/recovery.img # Then work on the image instead of the real drive
ntfsfix /dev/loop0
mount -t ntfs-3g -o ro /dev/loop0 /mnt/recovery

Posted in Windows.

Tagged with , , .


How to control Windows 10 via SSH

  1. Download SSH-server from github https://github.com/PowerShell/Win32-OpenSSH/releases/ and install.
  2. Start and enable auto-start after booting in powershell console:
    Start-Service sshd
    Set-Service -Name sshd -StartupType 'Automatic'
  3. Allow incoming connection in Windows Firewall:
    New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
  4. Enable password-less authentication:
    Add-Content -Path "C:\ProgramData\ssh\administrators_authorized_keys" -Value "your_public_key_here"
    icacls "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "SYSTEM:(F)" /grant "ADMINISTRATORS:(F)"
  5. To make PowerShell the default shell permanently:
    New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" `
      -Name DefaultShell `
      -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
      -PropertyType String -Force

After that you can run from linux commands on remove windows-machine like this:

ssh myuser@192.168.0.13 'gps | where Name -like "*notepad*"'
ssh myuser@192.168.0.13 'Stop-Process -Name "notepad" -Force'

Posted in Misc.


Apache 2.4 doesn't start with AH00023: Couldn't create the rewrite-map mutex

Problem: Apache 2.4.6 doesn't start on CentOS 7. error.log shows this confusing error message:

[Mon Mar 16 08:55:03.254104 2026] [core:emerg] [pid 3946] (28)No space left on device: AH00023: Couldn't create the rewrite-map mutex
[Mon Mar 16 08:55:03.254178 2026] [:emerg] [pid 3946] AH00020: Configuration Failed, exiting

We don't have problems with free space on storage device, so this message looks really weird:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        895M     0  895M   0% /dev
tmpfs           920M  105M  815M  12% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
/dev/sda1        50G   14G   36G  28% /
 
# df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
devtmpfs         228953    296   228657    1% /dev
tmpfs            235265    521   234744    1% /run
tmpfs            235265     16   235249    1% /sys/fs/cgroup
/dev/sda1      14305488 480611 13824877    4% /
# cat /proc/sys/kernel/sem
250     32000   32      128

Solution:

# sysctl -w kernel.sem="250 32000 100 256"
kernel.sem = 250 32000 100 256

After that change apache starts normally without complains.

Also some of this apache config-file changes might help:

Mutex fcntl:/var/run/httpd default

or

Mutex flock:/var/run/httpd default

I haven't tested it yet.

Posted in *nix, Howto.

Tagged with .