oriolrius.cat

Des del 2000 compartiendo sobre…

Author: Oriol Rius

Serving Static Files with Docker and Darkhttpd

Reading time: 3 – 5 minutes

In this blog post, we’ll explore how to use Docker and the lightweight HTTP server, Darkhttpd, to serve static files. This setup is particularly useful when you need a simple web server for sharing files or hosting a static website. We’ll also discuss how to use a reverse proxy like Traefik to route external traffic to the Darkhttpd service.

Docker Compose Configuration

Below is the docker-compose.yml file that defines the Darkhttpd service:

version: '3.3'
services:
  darkhttpd:
    image: p3terx/darkhttpd
    container_name: darkhttpd
    restart: unless-stopped
    volumes:
      - './site:/www:ro'
    entrypoint: ["/darkhttpd","/www"]
    networks:
      your_network:
        ipv4_address: your_ipv4_address
networks:
  your_network:
    external:
      name: your_network_name

Here’s a brief overview of the configuration:

  • The image field specifies the Docker image to use for the service.
  • The container_name field sets the name of the container.
  • The restart field configures the restart policy for the container.
  • The volumes field defines the volume mounts for the service.
  • The entrypoint field overrides the default entrypoint of the image.
  • The networks field specifies the networks that the service is connected to.

Setting Up the Service

  1. Create a directory named site in the same directory as the docker-compose.yml file. Place the static files you want to serve in this directory.
  2. Replace your_network, your_ipv4_address, and your_network_name in the docker-compose.yml file with the appropriate values for your setup.
  3. Run the following command to start the Darkhttpd service:
docker-compose up -d
  1. Access the static files by navigating to the IP address specified in the docker-compose.yml file.

Using a Reverse Proxy

To route external traffic to the Darkhttpd service, you can use a reverse proxy like Traefik. Configure the reverse proxy to forward requests to the IP address specified in the docker-compose.yml file.

Conclusion

Using Docker and Darkhttpd to serve static files is a simple and efficient solution for sharing files or hosting a static website. By adding a reverse proxy, you can easily route external traffic to the Darkhttpd service. This setup is ideal for scenarios where you need a lightweight web server without the overhead of a full-fledged web server like Apache or Nginx.

Enhancing SSH Security with StealthSSHAccess

Reading time: 4 – 7 minutes

In today’s interconnected world, maintaining the security of your server infrastructure is paramount. One critical point of vulnerability is the SSH (Secure Shell) service, which allows remote administration of servers. Despite using a non-default port, many administrators still find their servers bombarded with brute-force and denial-of-service attacks. To address this challenge, I’ve developed a solution called StealthSSHAccess.

The Problem

Attackers often employ brute force attacks to gain unauthorized access to servers via SSH. Even if you’ve changed the default SSH port, determined attackers can still discover the new port and target it. These attacks can lead to service disruption, unauthorized data access, and potential breaches of sensitive information.

The Solution: StealthSSHAccess

StealthSSHAccess is an innovative approach to managing remote SSH access while mitigating the risks associated with brute-force attacks. Let’s delve into how it works and why it’s an effective solution:

Dynamic Access Control

StealthSSHAccess takes a dynamic and personalized approach to SSH access control. It operates as a smart gateway between potential attackers and your SSH service. Here’s a simplified breakdown of how it functions:

  1. Monitoring for Intent: Instead of directly exposing the SSH port, StealthSSHAccess monitors a non-SSH TCP port for connection attempts. Attackers, unaware of this, can’t target the SSH port directly.
  2. Capture and Response: When an attempt is made on the monitored port, StealthSSHAccess captures the IP address of the requester. This initial connection attempt fails, serving as a signal of intent to access SSH.
  3. Secure Access Window: Based on this signal, StealthSSHAccess temporarily opens the SSH port exclusively for the captured IP address. This allows for a secure connection from that specific source.
  4. Time-Bound Access: Access is granted for a predetermined duration. If SSH access isn’t established within this timeframe, the port is automatically closed for that specific IP. This tightens the window of exposure and bolsters security.
  5. Automatic Closure: If the port remains unused during the allowed time, StealthSSHAccess automatically revokes access and closes the port. A continuous monitoring mechanism controls this process.

Benefits and Features

1. Enhanced Security: By hiding the SSH port from attackers, StealthSSHAccess reduces the attack surface and minimizes exposure to potential threats.

2. Selective Accessibility: With StealthSSHAccess, you control who gains access by simply attempting a connection to a specific port. This provides an additional layer of security.

3. Minimal Configuration: Implementing StealthSSHAccess is easy thanks to its Docker-based deployment. This means you can integrate it seamlessly into your existing system.

4. Persistence Across Restarts: StealthSSHAccess ensures continuity by persisting IP timer information across service interruptions or restarts. This keeps the system aware of pending access requests.

Getting Started with StealthSSHAccess

To deploy StealthSSHAccess, follow these steps:

  1. Requirements: Ensure you have Docker and Docker Compose installed.
  2. Configuration: Set up environment variables using the provided .env file. Customize parameters like LOGLEVEL, IFACE, PORT_TO_MONITOR, and more to match your environment.
  3. Building and Running: Build the images using docker-compose build, and then launch the services with docker-compose up -d.
  4. Data Persistence: IP timer data is stored in the ./data directory, so make sure it’s writable by the Docker user.
  5. Security Note: Be aware that these services run with privileged access due to their interaction with the system’s network configuration. Understand the security implications before deployment.

Conclusion

In the ongoing battle against cybersecurity threats, StealthSSHAccess stands as a beacon of innovative protection for your servers. By intelligently managing SSH access and responding dynamically to legitimate requests, this solution offers heightened security without sacrificing convenience. Whether you’re an administrator or a security-conscious user, consider integrating StealthSSHAccess into your infrastructure to safeguard your servers from the persistent threats of the digital landscape.

To explore the project, access the source code, and learn more about its implementation, visit the StealthSSHAccess GitHub repository. Remember, security is a journey, and with StealthSSHAccess, you’re taking a proactive step toward a more resilient and secure server environment.

Implementing Per-Domain DNS Configuration in macOS Using Resolver Configuration Files

Reading time: 3 – 4 minutes

In macOS, managing network traffic is essential for a robust and secure IT infrastructure. The operating system allows users to have a granular level of control over Domain Name System (DNS) settings, improving network functionality. Today’s post will walk through setting up per-domain DNS configuration on macOS using resolver configuration files.

Adding a DNS Rule Per Domain

MacOS has a powerful feature that allows you to specify DNS servers for individual domains. This is accomplished by creating resolver configuration files in the /etc/resolver/ directory. Each file in this directory corresponds to a domain and specifies the DNS servers to be used for that domain.

Let’s add a DNS rule for the “ymbihq.local” domain:

  1. Open Terminal.
  2. Use the sudo command to create a new file in the /etc/resolver/ directory with the same name as your domain:
sudo bash -c 'echo "nameserver 10.0.0.1" > /etc/resolver/ymbihq.local'

This command will create a new resolver file named ymbihq.local and adds a line specifying 10.0.0.1 as the nameserver. As a result, all DNS queries for “ymbihq.local” will be resolved by the DNS server at the IP address 10.0.0.1.

Reviewing DNS Rules

To verify that the resolver configuration file was created successfully and to review its content, use the cat command:

cat /etc/resolver/ymbihq.local

This will output the contents of the ymbihq.local file, which should look like this:

# Sample output:
nameserver 10.0.0.1

This confirms that the DNS server for the “ymbihq.local” domain has been set to 10.0.0.1.

Removing a DNS Rule

If you need to remove a DNS rule, you can simply delete the corresponding resolver configuration file. Use the rm command for this:

sudo rm /etc/resolver/ymbihq.local

After running this command, your macOS system will no longer have a custom DNS server set for the “ymbihq.local” domain, and it will default to using your standard DNS servers.

By creating and managing resolver configuration files, you can precisely control your DNS settings on a per-domain basis. This powerful feature of macOS allows you to optimize your network to fit your specific needs.

Implementing Per-Domain DNS Configuration in Windows Using PowerShell

Reading time: 2 - 4 minutes

The subtitle could be something like: Mastering DNS Client NRPT Rules with PowerShell

In today’s post, we will be looking at a compact, but powerful, the chunk of PowerShell code that allows us to interact with DNS Client Name Resolution Policy Table (NRPT) rules on a Windows machine. The commands in this code allow us to add, review, and remove rules, giving us control over the direction of our DNS traffic.

Adding a DNS Client NRPT Rule

Let’s take a look at the first command:

# add a Windows rule for ymbihq.local domain
Add-DnsClientNrptRule -Namespace ".ymbihq.local" -NameServers "10.0.0.1"

This command uses the Add-DnsClientNrptRule cmdlet to add a new rule for the “.ymbihq.local” namespace. The -Namespace parameter specifies the domain name for the rule, and the -NameServers parameter specifies the IP address of the DNS server that should be used for queries within this namespace.

In this instance, we’re setting a rule for any DNS queries under the “.ymbihq.local” domain to be resolved by the DNS server at the IP address 10.0.0.1. This can be especially useful in an enterprise environment where you have custom internal domains to be resolved by specific DNS servers.

Reviewing DNS Client NRPT Rules

After adding a rule, it’s essential to verify it. We can do this using the Get-DnsClientNrptRule command:

# get the list of rules
Get-DnsClientNrptRule

This command lists all the NRPT rules currently set on the machine. It will output the unique identifiers, names, namespaces, and other details for each rule. Here’s a sample output:

# Sample output:
Name                             : {A7CCF814-7492-4019-9FB1-27F61327AE93}
Version                          : 2
Namespace                        : {.ymbihq.local}
IPsecCARestriction               :
DirectAccessDnsServers           :
DirectAccessEnabled              : False
DirectAccessProxyType            :
DirectAccessProxyName            :
DirectAccessQueryIPsecEncryption :
DirectAccessQueryIPsecRequired   :
NameServers                      : 10.0.0.1
DnsSecEnabled                    : False
DnsSecQueryIPsecEncryption       :
DnsSecQueryIPsecRequired         :
DnsSecValidationRequired         :
NameEncoding                     : Disable
DisplayName                      :
Comment                          :

From this output, you can see various properties of the rule we’ve just added for the “.ymbihq.local” namespace, such as its unique identifier (Name) and the nameserver it’s associated with (NameServers).

Removing a DNS Client NRPT Rule

The final part of this block of code is dedicated to rule removal:

# remove the rule
Remove-DnsClientNrptRule -Name "{A7CCF814-7492-4019-9FB1-27F61327AE93}"

Here, we use the Remove-DnsClientNrptRule cmdlet with the -Name parameter followed by the unique identifier of the rule we wish to remove. After running this command, PowerShell will prompt you for confirmation before deleting the rule.

The process looks like this:


Confirm
Removing NRPT rule for namespace .ymbihq.local with
 DAEnable: Disabled,
 DnsSecValidationRequired: Disabled,
 NameEncoding: Disable
 NameServers: 10.0.0.1
 Do you want to continue?
[Y] Yes  [N] No

Avui ja n’he fet 20

Reading time: < 1 minute

Aprofito aquesta entrada per comentar una nova efemèride s’ha produït avui mateix. Ja que després de 46 anys menys dos dies he visitat de nou l’hospital on vaig néixer. Aquest cop per fer-me unes radiografies a les cervicals. Qui ho havia de dir?

Deploying gotop with Ansible

Reading time: 1 – 2 minutes

Gotop is a terminal based graphical activity monitor inspired by gtop and vtop; it’s available at:

https://github.com/xxxserxxx/gotop/

I published a role in Ansible Galaxy for deploying gotop in Linux servers. The role page in Ansible Galaxy is at:

https://galaxy.ansible.com/oriolrius/install_gotop

Role installation command and deployment command:

ansible-galaxy install oriolrius.install_gotop

# change SERVER_IP, for the IP address where you want to deploy gotop
ansible -i SERVER_IP, -u root -m include_role -a name=oriolrius.install_gotop all

Get the IP addresses of local Docker containers

Reading time: < 1 minute

We have docker running and the containers have their own private network, thanks to this command we’re going to get the private IP address of all of them:

$ sudo docker inspect $(docker ps -q ) \
--format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'
/zerotier
/ntp                                               10.3.10.8
/nodered                                           10.3.10.11
/n8n                                               10.3.10.4
/ssh                                               10.3.10.9
/code                                              10.3.10.7
/semaphore                                         10.3.10.6
/rproxy                                            10.3.10.2
/homer                                             10.3.10.10
/pihole                                            10.3.10.27
/pihole_googledns                                  10.3.10.24
/pihole_opendns                                    10.3.10.23

OpenSSH public key fingerprint

Reading time: < 1 minute

Quick and easy, how to get the fingerprint of your SSH RSA key.

# syntax:
openssl pkey -in PATH/PRIVATE_RSA_KEY -pubout -outform DER | openssl md5 -c

# example:
$ openssl pkey -in ~/.ssh/id_rsa -pubout -outform DER | openssl md5 -c
MD5(stdin)= a6:26:23:d9:c1:d3:d5:e5:c0:38:ab:3c:c1:6a:3f:ea

Mikrotik passwordless SSH with public key

Reading time: 2 – 4 minutes

Following the instructions described in the official documentation:

https://wiki.mikrotik.com/wiki/Use_SSH_to_execute_commands_(public/private_key_login)

The process is as always as easy as:

# upload the id_rsa.pub file
# then import the public key file for the user used for connecting via SSH
user ssh-keys import public-key-file=id_rsa.pub user=admin-ssh
# and it's done.

Everything was OK with my WSL Ubuntu 20.04. (I added WSL at the beginning of the versions because it runs in Windows Subsystem Linux).

But, with the newest WSL Ubuntu 22.04 I was unsuccessful.

Being precise, the SSH versions are:

# WSL Ubuntu 20.04
$ ssh -V
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020

# WSL Ubuntu 22.04
$ ssh -V
OpenSSH_8.9p1 Ubuntu-3, OpenSSL 3.0.2 15 Mar 2022

After connecting with verbose details, I found this message, that was the key for solving the problem:

debug1: Offering public key: /home/my_user/.ssh/id_rsa RSA SHA256:2******************************Y agent
debug1: send_pubkey_test: no mutual signature algorithm

Then, I discovered that newest SSH versions aren’t compatible with Mikrotik SSH version. It seems that version isn’t enough newest and are incompatible with how public keys are negotiated at the beginning of the connection.

Finally, the solution was to use an extra parameter for establishing the connection:

ssh -o 'PubkeyAcceptedAlgorithms +ssh-rsa' THE_USER@THE_HOST

Of course, an alternative is using ~/.ssh/config file or the system file: /etc/ssh/ssh_config and add this parameter for everything, or specific hosts. For instance, like this:

Host JUST_A_NAME_OF_THE_CONNECTION
  Hostname THE_IP_ADDRESS_OR_HOSTNAME_OF_THE_TARGET_HOST
  user THE_USER
  PubkeyAcceptedAlgorithms +ssh-rsa

Get the IP address of the WSL2 in Windows 10

Reading time: < 1 minute

Nothing else than what the title says. Simple PowerShell script for dumping the IP address:

wsl -- ip -o -4 -json addr list eth0 `
| ConvertFrom-Json `
| %{ $_.addr_info.local } `
| ?{ $_ }