Skip to content

Commit b5a668c

Browse files
committed
fixed fail2ban. Used this PR as a basis, atmoz#189 and based on this atmoz#195 using buster-slim seems to be working well
tails both auth.log and fail2ban.log to stdout for docker and kub
1 parent d0a1639 commit b5a668c

File tree

9 files changed

+85
-62
lines changed

9 files changed

+85
-62
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
keys
2+
examples/sftp-data

Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM debian:stretch
1+
FROM debian:buster-slim
22

33
# Steps done in one RUN layer:
44
# - Install packages
@@ -8,17 +8,22 @@ RUN apt-get update \
88
&& apt-get upgrade -y \
99
&& apt-get dist-upgrade -y \
1010
&& apt-get install -y \
11-
fail2ban \
11+
rsyslog \
12+
supervisor \
1213
openssh-server \
14+
fail2ban \
1315
&& apt-get clean \
1416
&& rm -rf /var/lib/apt/lists/* \
17+
&& rm -rf /var/log/*.log \
1518
&& mkdir -p /var/run/sshd \
1619
&& rm -f /etc/ssh/ssh_host_*key*
1720

1821
COPY files/sshd_config /etc/ssh/sshd_config
1922
COPY files/create-sftp-user /usr/local/bin/
2023
COPY files/jail.local /etc/fail2ban/
2124
COPY files/entrypoint /
25+
COPY files/sshd.conf /etc/rsyslog.d/sshd.conf
26+
COPY files/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
2227

2328
EXPOSE 22
2429

README.md

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# SFTP
22

3-
**Forked from atmoz to make it easier to setup on kubernetes. also add fail2ban. merges in PRs to fix a number of issues**
3+
**Forked from atmoz to make it easier to setup on kubernetes. adds fail2ban from [this pr](https://github.com/atmoz/sftp/pull/189). merges in PRs to fix a number of issues**
44

5-
![Docker Automated build](https://img.shields.io/docker/automated/atmoz/sftp.svg) ![Docker Build Status](https://img.shields.io/docker/build/atmoz/sftp.svg) ![Docker Stars](https://img.shields.io/docker/stars/atmoz/sftp.svg) ![Docker Pulls](https://img.shields.io/docker/pulls/atmoz/sftp.svg)
5+
![Docker Automated build](https://img.shields.io/docker/automated/yakworks/sftp.svg) ![Docker Build Status](https://img.shields.io/docker/build/yakworks/sftp.svg) ![Docker Stars](https://img.shields.io/docker/stars/yakworks/sftp.svg) ![Docker Pulls](https://img.shields.io/docker/pulls/yakworks/sftp.svg)
66

77
![OpenSSH logo](https://raw.githubusercontent.com/atmoz/sftp/master/openssh.png "Powered by OpenSSH")
88

9-
# Supported tags and respective `Dockerfile` links
9+
## Supported tags and respective `Dockerfile` links
1010

11-
- [`debian-stretch`, `debian`, `latest` (*Dockerfile*)](https://github.com/atmoz/sftp/blob/master/Dockerfile) [![](https://images.microbadger.com/badges/image/atmoz/sftp.svg)](http://microbadger.com/images/atmoz/sftp "Get your own image badge on microbadger.com")
12-
- [`debian-jessie` (*Dockerfile*)](https://github.com/atmoz/sftp/blob/debian-jessie/Dockerfile) [![](https://images.microbadger.com/badges/image/atmoz/sftp:debian-jessie.svg)](http://microbadger.com/images/atmoz/sftp:debian-jessie "Get your own image badge on microbadger.com")
13-
- [`alpine` (*Dockerfile*)](https://github.com/atmoz/sftp/blob/alpine/Dockerfile) [![](https://images.microbadger.com/badges/image/atmoz/sftp:alpine.svg)](http://microbadger.com/images/atmoz/sftp:alpine "Get your own image badge on microbadger.com")
11+
- [`debian`, `latest` (*Dockerfile*)](https://github.com/yakworks/docker-sftp/blob/master/Dockerfile) [![](https://images.microbadger.com/badges/image/yakworks/sftp.svg)](http://microbadger.com/images/yakworks/sftp "Get your own image badge on microbadger.com")
1412

15-
# Securely share your files
13+
## Securely share your files
1614

1715
Easy to use SFTP ([SSH File Transfer Protocol](https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol)) server with [OpenSSH](https://en.wikipedia.org/wiki/OpenSSH).
18-
This is an automated build linked with the [debian](https://hub.docker.com/_/debian/) and [alpine](https://hub.docker.com/_/alpine/) repositories.
16+
This is an automated build linked with the [debian](https://hub.docker.com/_/debian/) repositories.
1917

20-
# Usage
18+
19+
## Example Quickstart
20+
21+
to run the example in this project `./examples/docker-run.sh`
22+
23+
## Usage
2124

2225
- Define users in (1) command arguments, (2) `SFTP_USERS` environment variable
2326
or (3) in file mounted as `/etc/sftp/users.conf` (syntax:
@@ -35,51 +38,35 @@ This is an automated build linked with the [debian](https://hub.docker.com/_/deb
3538
want them to upload files.
3639
- For consistent server fingerprint, mount your own host keys (i.e. `/etc/ssh/ssh_host_*`)
3740

38-
# Examples
39-
40-
to run the example in this project `./examples/docker-run.sh`
41-
42-
## Simplest docker run example
41+
### Simplest docker run example
4342

4443
```
45-
docker run -p 22:22 -d atmoz/sftp foo:pass:::upload
44+
docker run -p 22:22 -d yakworks/sftp foo:pass:::upload
4645
```
4746

4847
User "foo" with password "pass" can login with sftp and upload files to a folder called "upload". No mounted directories or custom UID/GID. Later you can inspect the files and use `--volumes-from` to mount them somewhere else (or see next example).
4948

50-
## Sharing a directory from your computer
49+
### Sharing a directory from your computer
5150

5251
Let's mount a directory and set UID:
5352

5453
```
5554
docker run \
56-
-v /host/upload:/home/foo/upload \
57-
-p 2222:22 -d atmoz/sftp \
55+
-v /host/upload:/data \
56+
-p 2222:22 -d yakworks/sftp \
5857
foo:pass:1001
5958
```
6059

61-
### Using Docker Compose:
62-
63-
```
64-
sftp:
65-
image: atmoz/sftp
66-
volumes:
67-
- /host/upload:/home/foo/upload
68-
ports:
69-
- "2222:22"
70-
command: foo:pass:1001
71-
```
72-
7360
### Logging in
7461

7562
The OpenSSH server runs by default on port 22, and in this example, we are forwarding the container's port 22 to the host's port 2222. To log in with the OpenSSH client, run: `sftp -P 2222 foo@<host-ip>`
7663

77-
## Store users in config
64+
### Store users in config
7865

7966
```
8067
docker run \
8168
-v /host/users.conf:/etc/sftp/users.conf:ro \
82-
-v mySftpVolume:/home \
69+
-v mySftpVolume:/data \
8370
-p 2222:22 -d atmoz/sftp
8471
```
8572

@@ -91,7 +78,7 @@ bar:abc:1002:100
9178
baz:xyz:1003:100
9279
```
9380

94-
## Encrypted password
81+
### Encrypted password
9582

9683
Add `:e` behind password to mark it as encrypted. Use single quotes if using terminal.
9784

@@ -105,7 +92,7 @@ docker run \
10592
Tip: you can use [atmoz/makepasswd](https://hub.docker.com/r/atmoz/makepasswd/) to generate encrypted passwords:
10693
`echo -n "your-password" | docker run -i --rm atmoz/makepasswd --crypt-md5 --clearfrom=-`
10794

108-
## Logging in with SSH keys
95+
### Logging in with SSH keys
10996

11097
Mount public keys in the user's `.ssh/keys/` directory. All keys are automatically appended to `.ssh/authorized_keys` (you can't mount this file directly, because OpenSSH requires limited file permissions). In this example, we do not provide any password, so the user `foo` can only login with his SSH key.
11198

@@ -118,7 +105,7 @@ docker run \
118105
foo::1001
119106
```
120107

121-
## Providing your own SSH host key (recommended)
108+
### Providing your own SSH host key (recommended)
122109

123110
This container will generate new SSH host keys at first run. To avoid that your users get a MITM warning when you recreate your container (and the host keys changes), you can mount your own host keys.
124111

@@ -138,12 +125,12 @@ ssh-keygen -t ed25519 -f ssh_host_ed25519_key < /dev/null
138125
ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key < /dev/null
139126
```
140127

141-
## Execute custom scripts or applications
128+
### Execute custom scripts or applications
142129

143130
Put your programs in `/etc/sftp.d/` and it will automatically run when the container starts.
144131
See next section for an example.
145132

146-
## Bindmount dirs from another location
133+
### Bindmount dirs from another location
147134

148135
If you are using `--volumes-from` or just want to make a custom directory available in user's home directory, you can add a script to `/etc/sftp.d/` that bindmounts after container starts.
149136

@@ -169,16 +156,3 @@ bindmount /data/docs /home/peter/docs --read-only
169156
```
170157

171158
**NOTE:** Using `mount` requires that your container runs with the `CAP_SYS_ADMIN` capability turned on. [See this answer for more information](https://github.com/atmoz/sftp/issues/60#issuecomment-332909232).
172-
173-
# What's the difference between Debian and Alpine?
174-
175-
The biggest differences are in size and OpenSSH version. [Alpine](https://hub.docker.com/_/alpine/) is 10 times smaller than [Debian](https://hub.docker.com/_/debian/). OpenSSH version can also differ, as it's two different teams maintaining the packages. Debian is generally considered more stable and only bugfixes and security fixes are added after each Debian release (about 2 years). Alpine has a faster release cycle (about 6 months) and therefore newer versions of OpenSSH. As I'm writing this, Debian has version 7.4 while Alpine has version 7.5. Recommended reading: [Comparing Debian vs Alpine for container & Docker apps](https://www.turnkeylinux.org/blog/alpine-vs-debian)
176-
177-
# What version of OpenSSH do I get?
178-
179-
It depends on which linux distro and version you choose (see available images at the top). You can see what version you get by checking the distro's packages online. I have provided direct links below for easy access.
180-
181-
- [List of `openssh` packages on Alpine releases](https://pkgs.alpinelinux.org/packages?name=openssh&branch=&repo=main&arch=x86_64)
182-
- [List of `openssh-server` packages on Debian releases](https://packages.debian.org/search?keywords=openssh-server&searchon=names&exact=1&suite=all&section=main)
183-
184-
**Note:** The time when this image was last built can delay the availability of an OpenSSH release. Since this is an automated build linked with [debian](https://hub.docker.com/_/debian/) and [alpine](https://hub.docker.com/_/alpine/) repos, the build will depend on how often they push changes (out of my control). Typically this can take 1-5 days, but it can also take longer. You can of course make this more predictable by cloning this repo and run your own build manually.

examples/dock-run.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
# docker build -t yakworks/sftp .
66
docker stop sftp || true && docker rm sftp || true
77

8-
docker run --name sftp --cap-add=SYS_ADMIN \
8+
docker run --name sftp --cap-add=SYS_ADMIN --cap-add=NET_ADMIN \
99
-p 30022:22 \
1010
-e DATA_MOUNT_NAME=ninebox \
1111
-v $(pwd)/examples/users.conf:/etc/sftp/users.conf \
1212
-v $(pwd)/examples/sftp-data:/data \
13-
-d yakworks/sftp
13+
-d yakworks/sftp
14+
15+
#to brute force test to see if fail2ban is working
16+
#hydra -l ftp -x 3:3:a -t 4 -s 30022 ssh://127.0.0.1/

files/entrypoint

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ touch /var/log/auth.log
9191
service fail2ban start
9292

9393
if $startSshd; then
94-
log "Executing sshd"
95-
exec /usr/sbin/sshd -D -e
96-
tail -f /var/log/auth.log
94+
#log "Executing sshd"
95+
#exec /usr/sbin/sshd -D -e
96+
#tail -f /var/log/auth.log
97+
log "Executing supervisord with sshd"
98+
mkdir -p /var/log/supervisor
99+
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
97100
else
98-
log "Executing $*"
101+
log "Executing custom startup $*"
99102
exec "$@"
100103
fi

files/jail.local

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
[DEFAULT]
22
# fail2ban bantime is the total number of seconds that a host is banned
3-
bantime = 3600
3+
bantime = 600
4+
#I think we want this to be 1 less than the sshd_config so its picked up and banned
5+
maxretry = 6

files/sshd.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Create an additional socket for some of the sshd chrooted users.
2+
$AddUnixListenSocket /home/sftp.log.socket
3+
# Log internal-sftp in a separate file
4+
:programname, isequal, "internal-sftp" -/var/log/sftp.log
5+
:programname, isequal, "internal-sftp" ~

files/sshd_config

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Secure defaults
22
# See: https://stribika.github.io/2015/01/04/secure-secure-shell.html
3+
MaxAuthTries 6
4+
35
Protocol 2
46
HostKey /etc/ssh/ssh_host_ed25519_key
57
HostKey /etc/ssh/ssh_host_rsa_key
@@ -14,13 +16,14 @@ X11Forwarding no
1416
AllowTcpForwarding no
1517

1618
# Force sftp and chroot jail
17-
Subsystem sftp internal-sftp
18-
ForceCommand internal-sftp -u 0002 # umask for user|group rwx|rwx permisions
19+
Subsystem sftp internal-sftp -f AUTH -l VERBOSE
20+
# umask for user|group rwx|rwx permisions
21+
ForceCommand internal-sftp -u 0002 -f AUTH -l VERBOSE
1922

2023
ChrootDirectory /home/%u
2124

2225
# Match Group users
2326
# ChrootDirectory %h
2427

2528
# Enable this for more logs
26-
#LogLevel VERBOSE
29+
LogLevel VERBOSE

files/supervisord.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[supervisord]
2+
nodaemon=true
3+
user=root
4+
5+
[program:rsyslog]
6+
command=/usr/sbin/rsyslogd -n
7+
redirect_stderr=true
8+
stdout_logfile=/var/log/rsyslogd.log
9+
stdout_logfile_maxbytes=0
10+
autorestart=true
11+
priority=1
12+
13+
[program:sshd]
14+
command=/usr/sbin/sshd -D
15+
redirect_stderr=true
16+
stdout_logfile=/dev/stdout
17+
stdout_logfile_maxbytes=0
18+
autorestart=true
19+
priority=10
20+
21+
[program:logger]
22+
command=tail -f /var/log/auth.log /var/log/fail2ban.log
23+
redirect_stderr=true
24+
stdout_logfile=/dev/stdout
25+
stdout_logfile_maxbytes=0
26+
autorestart=true
27+
priority=100

0 commit comments

Comments
 (0)