<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by MOHAMED ANSAF TP on Medium]]></title>
        <description><![CDATA[Stories by MOHAMED ANSAF TP on Medium]]></description>
        <link>https://medium.com/@ansafmattathur?source=rss-5951cf76eddb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YOv-WZG9NZEu6kH0</url>
            <title>Stories by MOHAMED ANSAF TP on Medium</title>
            <link>https://medium.com/@ansafmattathur?source=rss-5951cf76eddb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 09:41:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ansafmattathur/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Build a Docker Lab for Lateral Movement: Three Containers, Two Networks, One Pivot]]></title>
            <link>https://medium.com/@ansafmattathur/build-a-docker-lab-for-lateral-movement-three-containers-two-networks-one-pivot-696fdfd949f1?source=rss-5951cf76eddb------2</link>
            <guid isPermaLink="false">https://medium.com/p/696fdfd949f1</guid>
            <category><![CDATA[tunneling]]></category>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[lateral-movement]]></category>
            <category><![CDATA[red-team]]></category>
            <category><![CDATA[ssh-tunneling]]></category>
            <dc:creator><![CDATA[MOHAMED ANSAF TP]]></dc:creator>
            <pubDate>Wed, 21 Oct 2026 12:49:38 GMT</pubDate>
            <atom:updated>2026-10-21T13:15:07.394Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Practice SSH local, dynamic, and remote forwarding without building a fleet of virtual machines.</em></p><p>You do not need a stack of virtual machines to start learning how a network pivot works.</p><p>Picture the exercise: you have an attacker machine, an internal web service, and a host sitting between them. Your attacker cannot reach the service directly. The middle host can.</p><p>The interesting question is not, “Can I ping another machine?” It is, “Can I use an authorized foothold to reach a service my own machine cannot access?”</p><p>You could build this with several VMs, install their operating systems, configure their adapters, and manage snapshots. For a focused Linux SSH exercise, Docker gives us a smaller starting point: three containers and two networks.</p><p>We will first prove that the target is unreachable from the attacker. Then we will reach it using three different SSH forwarding techniques. Each successful request will make the same networking idea visible from a different angle.</p><p>One distinction matters before we begin: this is a <strong>network-pivoting lab</strong>, not a complete enterprise lateral-movement simulation. We start with valid lab credentials. There is no initial compromise, credential theft, or privilege escalation. We are learning how access through an intermediate host changes what we can reach.</p><blockquote>&gt; Run this only on a machine you control. The credentials and SSH settings below are deliberately simplified for a disposable local lab, not for production or public hosting.</blockquote><p><strong>## 1. Meet the Lab: Attacker, Pivot, and Target</strong></p><p>Our topology has two Docker bridge networks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*chysqO-YT9x-jeqy72hrpQ.png" /><figcaption>Topology of Docker bridge network</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*ttgSGuKV3b-uVzY0-sivJw.png" /></figure><p>The attacker and target do not share a Docker network. Only the pivot belongs to both.</p><p>We are not configuring the pivot as an IP router. Instead, SSH will carry traffic through an authenticated connection, and one end of that connection will open a new connection to the target.</p><p>That difference is the heart of this exercise: <strong>a tunnel does not require your attacker container to have a direct route to the destination service.</strong></p><p><strong>## 2. What You Need Before Starting</strong></p><p>Use a Linux host with Docker Engine and the Docker Compose plugin installed, with the daemon running. The commands below assume your account can run Docker commands.</p><p>Check the tools:</p><pre>docker version<br>docker compose version</pre><p>The first command should show both client and server information. An unreachable daemon or permission error needs to be resolved before continuing. Follow the official Docker installation instructions for your distribution if Docker is not installed.</p><p>You will also need:</p><p>- Internet access during the initial image build to download Ubuntu and its packages.</p><p>- Two terminal windows for testing each tunnel.</p><p>- Subnets `<strong><em>172.30.10.0/24</em></strong>` and `<strong><em>172.30.20.0/24</em></strong>` that do not conflict with existing Docker, LAN, or VPN networks.</p><p>These steps target a standard Linux Docker Engine setup. Docker Desktop, rootless Docker, and customized host firewalls can behave differently. Treat the segmentation check later in the article as a required test, not an assumption.</p><p>Create a working directory:</p><pre>mkdir -p docker-lateral-lab<br>cd docker-lateral-lab</pre><p>The lab needs three implementation files:</p><pre>docker-lateral-lab/<br>compose.yaml<br>Dockerfile<br>entrypoint.sh</pre><p><strong>## 3. Define the Network Boundaries</strong></p><p>Create `<strong><em>compose.yaml</em></strong>` with the following content:</p><pre>services:<br>  attacker:<br>    build: .<br>    container_name: lab-attacker<br>    hostname: attacker<br>    networks:<br>      attacker_pivot:<br>        ipv4_address: 172.30.10.10<br><br>  pivot:<br>    build: .<br>    container_name: lab-pivot<br>    hostname: pivot<br>    networks:<br>      attacker_pivot:<br>        ipv4_address: 172.30.10.20<br>      pivot_target:<br>        ipv4_address: 172.30.20.20<br><br>  target:<br>    build: .<br>    container_name: lab-target<br>    hostname: target<br>    environment:<br>      ROLE: target<br>    networks:<br>      pivot_target:<br>        ipv4_address: 172.30.20.30<br><br>networks:<br>  attacker_pivot:<br>    driver: bridge<br>    internal: true<br>    ipam:<br>      config:<br>        - subnet: 172.30.10.0/24<br>  pivot_target:<br>    driver: bridge<br>    internal: true<br>    ipam:<br>      config:<br>        - subnet: 172.30.20.0/24</pre><p>Here is what makes this configuration useful:</p><p>- <strong>Separate networks:</strong> the attacker joins only `attacker_pivot`; the target joins only `pivot_target`.</p><p>- <strong>A dual-homed pivot:</strong> the pivot has an interface on each network, so it can connect to both sides.</p><p>- <strong>Static addresses:</strong> the commands remain easy to follow across rebuilds.</p><p>- <strong>Internal networks:</strong> `internal: true` creates externally isolated Docker networks. Containers are not given normal external connectivity through these networks.</p><p>- <strong>No published ports:</strong> there is no `ports:` mapping exposing SSH or HTTP on the host.</p><p>- <strong>One shared image definition:</strong> all three services use the same Dockerfile; `ROLE: target` selects the target’s extra web-server behavior.</p><p>This is a learning boundary, not a hardened security sandbox. The Docker host has privileged control over the containers and their networks, and its access is not equivalent to the attacker’s access. Run the tests from the specified containers, not from the host.</p><p><strong>## 4. Build the Shared Toolbox</strong></p><p>Create `<strong>Dockerfile</strong>`:</p><pre>FROM ubuntu:24.04<br><br>RUN apt-get update &amp;&amp; DEBIAN_FRONTEND=noninteractive apt-get install -y \<br>    openssh-server openssh-client curl iproute2 iputils-ping netcat-openbsd python3 procps \<br>    &amp;&amp; rm -rf /var/lib/apt/lists/* \<br>    &amp;&amp; mkdir -p /run/sshd \<br>    &amp;&amp; useradd -m -s /bin/bash labuser \<br>    &amp;&amp; echo &#39;labuser:labpass&#39; | chpasswd \<br>    &amp;&amp; printf &#39;\nPasswordAuthentication yes\nPermitRootLogin no\nAllowTcpForwarding yes\nGatewayPorts no\nPermitTunnel no\n&#39; &gt;&gt; /etc/ssh/sshd_config<br><br>COPY entrypoint.sh /entrypoint.sh<br>RUN chmod +x /entrypoint.sh<br>EXPOSE 22 8080<br>ENTRYPOINT [&quot;/entrypoint.sh&quot;]</pre><p>The image starts with Ubuntu 24.04 and installs the tools we need:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*ZciuouL-nLdOn4DRYDhKLA.png" /></figure><p>Every container receives the disposable account `labuser` with password `labpass`.</p><p>The SSH settings are also intentional:</p><p>- `PasswordAuthentication yes` lets us use the lab password.</p><p>- `PermitRootLogin no` prevents direct root SSH login.</p><p>- `AllowTcpForwarding yes` enables the forwarding exercises.</p><p>- `GatewayPorts no` keeps server-side remote-forward listeners loopback-bound.</p><p>- `PermitTunnel no` disables SSH tunnel-device forwarding. We are using TCP forwarding, which is a different feature.</p><p>Finally, `EXPOSE 22 8080` documents the intended ports. It does <strong>not</strong> publish those ports on your host. Publishing requires an explicit mapping, which this lab does not define.</p><p><strong>## 5. Give the Target Something to Serve</strong></p><p>Create `<strong>entrypoint.sh</strong>`:</p><pre>#!/usr/bin/env bash<br>set -e<br>ssh-keygen -A<br>/usr/sbin/sshd<br>if [ &quot;${ROLE:-node}&quot; = &quot;target&quot; ]; then<br>  mkdir -p /srv/web<br>  cat &gt; /srv/web/index.html &lt;&lt;&#39;HTML&#39;<br>&lt;!doctype html&gt;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Target service reached&lt;/h1&gt;&lt;p&gt;You accessed the isolated target through the pivot container.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;<br>HTML<br>  cd /srv/web<br>  exec python3 -m http.server 8080 --bind 0.0.0.0<br>fi<br>exec sleep infinity</pre><p>On startup, each container generates any missing SSH host keys and starts its SSH server.</p><p>The target additionally creates a small HTML page and runs Python’s HTTP server on port `8080`, listening on all of its container interfaces. The other two containers stay running with `sleep infinity` so we can enter them and run commands.</p><p>The page contains our success marker: <strong>Target service reached</strong>.</p><p>The marker alone does not prove a pivot occurred. The proof comes from combining it with a failed direct request and a successful tunneled request.</p><p><strong>## 6. Start the Lab and Prove the Separation</strong></p><p>From the project directory on your host, validate the Compose file and build the lab:</p><pre>docker compose config - quiet<br>docker compose up -d - build<br>docker compose ps</pre><p>All three containers should be running. Container startup is not the same as application readiness, so use the following requests to check the actual services.</p><p>First, verify that the attacker can reach the pivot:</p><pre>docker exec lab-attacker ping -c 2 172.30.10.20</pre><p>Next, verify that the pivot can reach the target’s web service:</p><pre>docker exec lab-pivot curl http://172.30.20.30:8080</pre><p>You should receive HTML containing `Target service reached`.</p><p>Now try the same service directly from the attacker:</p><pre>docker exec lab-attacker curl - connect-timeout 3 http://172.30.20.30:8080</pre><p><strong>This request should fail or time out.</strong>The exact error can vary with the host’s networking configuration.</p><p>That failure is our baseline. If it succeeds, stop here and investigate network membership, host routing, and firewall rules before continuing. A successful request after enabling a tunnel is only meaningful if the direct path was unavailable first.</p><p><strong>## 7. Local Forwarding: Bring One Internal Service to the Attacker</strong></p><p>Start with the most concrete use case: make one internal HTTP service available on a local port in the attacker container.</p><p>In terminal 1 on your host, open an attacker shell and start the tunnel:</p><pre>docker exec -it lab-attacker ssh -N -o StrictHostKeyChecking=no -L 127.0.0.1:18080:172.30.20.30:8080 labuser@172.30.10.20</pre><p>Enter `labpass` when prompted. Leave this terminal open. A quiet terminal is expected: `-N` tells SSH not to run a remote command.</p><p>The local-forward syntax is:</p><pre>-L local_bind_address:local_port:destination_host:destination_port</pre><p>Here, SSH listens at `127.0.0.1:18080` <strong>inside the attacker container</strong>. Connections to that listener travel through SSH to the pivot, which opens the connection to `172.30.20.30:8080`.</p><p>In terminal 2 on your host:</p><pre>docker exec lab-attacker curl http://127.0.0.1:18080</pre><p>The target page should appear.</p><pre>attacker curl -&gt; attacker localhost:18080 -&gt; SSH -&gt; pivot -&gt; target:8080</pre><p>Notice what changed: the attacker still does not have direct target connectivity. It now has a local listener whose traffic the pivot forwards.</p><p>Also notice what `localhost` means. Running `curl <a href="http://127.0.0.1:18080`">http://127.0.0.1:18080`</a> directly on your host would test the host’s loopback interface, not the attacker’s. That is why the verification uses `docker exec`.</p><p>When finished, press `Ctrl+C` in terminal 1 to stop SSH, then run `exit` to return from the container shell to your host.</p><blockquote>&gt; `StrictHostKeyChecking=no` simplifies this disposable exercise but weakens SSH host verification. Outside this lab, verify host fingerprints and use normal host-key checking. Never reuse the lab password.</blockquote><p><strong>## 8. Dynamic Forwarding: Use the Pivot as a SOCKS Proxy</strong></p><p>Local forwarding gives us one fixed destination. Dynamic forwarding lets a SOCKS-aware application choose destinations through the SSH connection.</p><p>In terminal 1 on your host:</p><pre>docker exec -it lab-attacker ssh -N -o StrictHostKeyChecking=no -D 127.0.0.1:1080 labuser@172.30.10.20</pre><p>Enter `labpass` and leave the session running.</p><p>The `-D` option creates a SOCKS listener on the attacker’s loopback address at port `1080`.</p><p>In terminal 2 on your host:</p><pre>docker exec lab-attacker curl - socks5-hostname 127.0.0.1:1080 http://172.30.20.30:8080</pre><p>You should receive the same target page.</p><pre>attacker curl -&gt; attacker SOCKS :1080 -&gt; SSH -&gt; pivot -&gt; requested destination</pre><p>The destination is selected by `curl`, not hard-coded into the SSH command. With a hostname URL, ` — socks5-hostname` also delegates name resolution through the proxy; our numeric target address does not require DNS.</p><p>This does not automatically proxy every application or every protocol. The client must support SOCKS or be configured to use it, and OpenSSH dynamic forwarding here carries TCP connections, not a general-purpose IP or UDP tunnel.</p><p>Stop SSH with `Ctrl+C`, then run `exit` to return to the host before the next exercise.</p><p><strong>## 9. Remote Forwarding: Let the Pivot Initiate the Connection</strong></p><p>So far, the attacker has initiated SSH connections to the pivot. Remote forwarding changes which side initiates SSH and where the listener is created.</p><p>In this exercise, the pivot connects to the attacker’s SSH server and asks it to listen on port `19080`. Traffic arriving there travels back through SSH, and the pivot connects to the target.</p><p>In terminal 1 on your host:</p><pre>docker exec -it lab-pivot ssh -N -o StrictHostKeyChecking=no -R 127.0.0.1:19080:172.30.20.30:8080 labuser@172.30.10.10</pre><p>Enter `labpass` and leave the tunnel running.</p><p>In terminal 2 on your host:</p><pre>docker exec lab-attacker curl http://127.0.0.1:19080</pre><p>The target page should appear again.</p><pre>SSH connection: pivot -&gt; attacker<br>HTTP path: attacker localhost:19080 -&gt; SSH -&gt; pivot -&gt; target:8080</pre><p>The word <strong>remote</strong> is relative to the SSH client. Because the client runs on the pivot, the remote listener is created on the attacker. The destination connection is opened from the pivot side.</p><p>This models a useful directionality concept: a host with internal access can initiate the connection that makes a service available to the other side. It does not demonstrate an outbound-only firewall policy; our Compose file does not enforce one.</p><p>Stop the tunnel with `Ctrl+C`, then run `exit` to leave the pivot shell.</p><p><strong>## 10. Compare the Three Techniques</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*RsYXrBaHXUuABB8hdD32bg.png" /></figure><p>In all three examples, the pivot opens the final TCP connection to the target. What changes is how traffic enters the tunnel and which side initiates SSH.</p><p>The simplest way to reason about a forwarding command is to ask three questions:</p><p>1. Which machine runs the SSH client?</p><p>2. Which machine owns the listening port?</p><p>3. Which machine opens the destination connection?</p><p>Answer those before memorizing flags, and the commands become much easier to understand.</p><p><strong>## 11. Troubleshooting: Check the Path in Order</strong></p><p><strong>### A container exits or HTTP does not respond</strong></p><p>Start with status and logs:</p><pre>docker compose ps<br>docker compose logs - tail=50<br>docker exec lab-target ss -lntp</pre><p>The target should have a listener on `0.0.0.0:8080`. Then repeat the direct HTTP request from the pivot before investigating SSH.</p><p><strong>### SSH fails or the tunnel is silent</strong></p><p>Silence after authentication is normal with `-N`. For a failed connection, check SSH listeners:</p><pre>docker exec lab-attacker ss -lntp<br>docker exec lab-pivot ss -lntp</pre><p>For local and dynamic forwarding, the pivot needs its SSH listener. For remote forwarding, the attacker needs its SSH listener too.</p><p>To see connection and forwarding details, rerun the relevant SSH command with `-vvv`. For example, from an attacker shell:</p><pre>ssh -vvv -N -o StrictHostKeyChecking=no -L 127.0.0.1:18080:172.30.20.30:8080 labuser@172.30.10.20</pre><p>If a port is already in use, stop the earlier tunnel or choose another unused listener port and update the matching `curl` command. You can also add <strong><em>`-o ExitOnForwardFailure=yes`</em></strong> to make SSH exit if it cannot establish a requested listener; this does not guarantee that the destination service is reachable.</p><p><strong>### The direct attacker-to-target request unexpectedly works</strong></p><p>Check network membership:</p><pre>docker network ls<br>docker network inspect docker-lateral-lab_attacker_pivot<br>docker network inspect docker-lateral-lab_pivot_target</pre><p>Those names assume the default Compose project name `docker-lateral-lab`. A different directory name or project-name override changes the prefix; use the names reported by `docker network ls`.</p><p>The attacker and target should not appear on the same network. If membership is correct, investigate custom host forwarding and firewall rules. Do not continue treating the lab as segmented until the direct request fails.</p><p><strong>### Docker reports an address-pool conflict</strong></p><p>Choose two unused, non-overlapping private subnets. Update both network definitions, all static container addresses, and every command that references those addresses. Remove this project’s containers and networks with `docker compose down` before recreating them.</p><p><strong>## 12. Clean Up When You Are Done</strong></p><p>Stop any active SSH sessions, return to your host, and run this from the project directory:</p><pre>docker compose down -v - remove-orphans</pre><p>This removes the project’s containers and networks, and any applicable Compose-managed volumes. This configuration does not define persistent volumes. Built images remain cached; the command does not remove them.</p><p>The lab is disposable. Recreate it with `docker compose up -d — build` whenever you want to repeat the exercise.</p><p><strong>## 13. What Docker Teaches Well, and Where VMs Still Matter</strong></p><p>For this exercise, Docker gives us a compact environment to practice network segmentation, SSH forwarding, listener placement, and client-versus-destination perspective.</p><p>It also makes repetition straightforward: change a forwarding command, observe the result, stop the tunnel, and try again.</p><p>But containers share the host kernel. This lab does not reproduce independent operating-system kernels, Windows domains, Active Directory authentication, endpoint security tooling, or a realistic enterprise attack path. Docker networks are not a substitute for every firewall or routing design, either.</p><p>Use containers for this focused networking exercise. Use VMs or a hybrid lab when your learning objective depends on full operating systems, domain infrastructure, or stronger isolation.</p><p><strong>## The Takeaway: Learn the Path, Not Just the Command</strong></p><p>At the beginning, the attacker could not reach the web service. By the end, we reached that same service through a local forward, a SOCKS proxy, and a remote forward.</p><p>We did not give the attacker another network interface or publish the target’s port on the host. We used an authenticated SSH connection and the pivot’s existing access.</p><p>That is the lesson worth keeping: <strong>network access depends not only on where you are, but also on which authorized host can make the connection on your behalf.</strong></p><p>Three containers are enough to start seeing that path clearly. The VMs can wait until the next learning objective needs them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=696fdfd949f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>