Postfix: Automatic UFW Firewall Updates

If you use a Mail Server with Postfix you got daily Spam Attacks by Scripts:

How to fix?

  • Install ufw Firewall
  • Run a Scanner Script as  cronjob

On Debian/Ubuntu:

Install ufw:


sudo apt-get update && sudo apt-get install ufw && sudo ufw enable && sudo  ufw logging off

Scan Script:
sudo nano /home/user/firewall-update.sh:


#!/bin/bash
# scan rejected
cat /var/log/mail.log | grep rejected | cut -d"[" -f3 | cut -d"]" -f1|grep -v '^$' > /tmp/firewall.txt
# insert to Firewall
while read line; do sudo ufw insert 1 deny from $line to any; done < /tmp/firewall.txt
# scan "denied"
cat /var/log/mail.log | grep denied | cut -d"[" -f3 | cut -d"]" -f1|grep -v '^$' > /tmp/firewall2.txt
# insert to Firewall
while read line; do sudo ufw insert 1 deny from $line to any; done < /tmp/firewall2.txt
service ufw restart
exit 0

Remark:

  • add to root’s crontab run hourly
  • add lines cat..+ while.. replace rejected by other failed commands login etc..!

Test:


sudo ufw status numbered

Should echo list of banned IP’s! Screenshot of one DAY!

Status: active

To Action From
— —— —-
Anywhere DENY 87.98.131.120
Anywhere DENY 187.178.174.1
Anywhere DENY 46.148.88.115
Anywhere DENY 31.28.86.59
Anywhere DENY 200.6.213.125
Anywhere DENY 200.35.185.180
Anywhere DENY 37.49.227.221
Anywhere DENY 203.60.1.21
Anywhere DENY 78.110.2.2
Anywhere DENY 192.140.8.21
Anywhere DENY 66.240.219.146
Anywhere DENY 171.49.178.169
Anywhere DENY 186.179.219.145
Anywhere DENY 119.235.53.122
Anywhere DENY 95.177.213.224
Anywhere DENY 14.161.43.66
Anywhere DENY 203.143.23.69
Anywhere DENY 104.215.8.206
Anywhere DENY 191.96.249.84
Anywhere DENY 190.190.167.206
Anywhere DENY 95.97.176.158
Anywhere DENY 200.77.219.250
Anywhere DENY 191.248.224.38
Anywhere DENY 91.237.124.222
Anywhere DENY 31.27.32.18
Anywhere DENY 122.174.172.246
Anywhere DENY 208.92.136.194
Anywhere DENY 39.52.234.157
Anywhere DENY 202.188.23.209
Anywhere DENY 154.72.169.179
Anywhere DENY 88.247.177.95
Anywhere DENY 98.116.114.130
Anywhere DENY 203.45.1.236
Anywhere DENY 115.74.190.217
Anywhere DENY 183.129.160.229
Anywhere DENY 203.143.23.66
Anywhere DENY 201.187.101.222
Anywhere DENY 46.13.150.215
Anywhere DENY 104.215.11.242
Anywhere DENY 77.119.247.86
Anywhere DENY 94.46.187.190
Anywhere DENY 78.89.186.118
Anywhere DENY 185.218.184.95
Anywhere DENY 103.10.44.223
Anywhere DENY 167.114.226.176
Anywhere DENY 185.38.154.18
Anywhere DENY 85.105.58.91
Anywhere DENY 149.135.117.174
Anywhere DENY 104.236.166.245
Anywhere DENY 27.115.124.2
Anywhere DENY 181.60.254.53
Anywhere DENY 196.22.248.246
Anywhere DENY 165.49.18.249
Anywhere DENY 159.147.100.63
Anywhere DENY 192.95.17.132
Anywhere DENY 137.74.89.35
Anywhere DENY 193.70.87.209
Anywhere DENY 216.36.187.97
Anywhere DENY 101.187.124.125
Anywhere DENY 2.42.219.63
Anywhere DENY 185.109.169.71
Anywhere DENY 201.26.128.167
Anywhere DENY 133.130.74.177
Anywhere DENY 2.139.229.39
Anywhere DENY 177.1.7.49
Anywhere DENY 103.227.88.130
Anywhere DENY 46.102.196.66
Anywhere DENY 109.230.219.194
Anywhere DENY 180.94.114.47
Anywhere DENY 212.170.109.162
Anywhere DENY 190.24.136.122
Anywhere DENY 82.152.228.49
Anywhere DENY 181.49.39.70
Anywhere DENY 103.240.181.210
Anywhere DENY 189.51.83.246
Anywhere DENY 61.19.16.144
Anywhere DENY 45.76.95.222
Anywhere DENY 178.90.55.176
Anywhere DENY 87.139.234.44
Anywhere DENY 200.116.164.5
Anywhere DENY 24.222.140.202
Anywhere DENY 103.100.209.234
Anywhere DENY 82.201.54.152
Anywhere DENY 84.241.1.21
Anywhere DENY 110.145.123.120
Anywhere DENY 185.32.183.141
Anywhere DENY 200.49.145.161
Anywhere DENY 213.156.120.22
Anywhere DENY 95.59.137.196
Anywhere DENY 185.229.227.5
Anywhere DENY 188.225.171.58
Anywhere DENY 211.24.107.177
Anywhere DENY 186.233.80.51
Anywhere DENY 187.178.242.154
Anywhere DENY 190.223.59.18
Anywhere DENY 202.181.207.212
Anywhere DENY 41.87.95.33
Anywhere DENY 52.175.252.79
Anywhere DENY 103.252.220.20
Anywhere DENY 212.230.98.37
Anywhere DENY 41.180.72.44
Anywhere DENY 196.191.131.50
Anywhere DENY 120.150.227.127
Anywhere DENY 181.211.10.202
Anywhere DENY 218.255.233.114
Anywhere DENY 181.143.94.74
Anywhere DENY 196.38.89.85
Anywhere DENY 190.187.134.246
Anywhere DENY 76.65.196.40
Anywhere DENY 222.102.154.172
Anywhere DENY 221.121.148.77
Anywhere DENY 203.191.174.55
Anywhere DENY 190.25.46.42
Anywhere DENY 169.55.213.44
Anywhere DENY 86.16.10.224
Anywhere DENY 89.96.222.27
Anywhere DENY 202.131.203.163
Anywhere DENY 200.85.52.74
Anywhere DENY 94.23.73.132
Anywhere DENY 41.193.16.218
Anywhere DENY 175.136.232.97
Anywhere DENY 118.219.45.141
Anywhere DENY 205.151.252.203
Anywhere DENY 82.113.59.26
Anywhere DENY 178.33.107.200
Anywhere DENY 82.185.149.169
Anywhere DENY 220.130.186.101
Anywhere DENY 201.33.193.166
Anywhere DENY 178.159.36.60
Anywhere DENY 74.125.82.65
Anywhere DENY 74.125.82.67
Anywhere DENY 209.85.128.193
Anywhere DENY 209.85.128.195
Anywhere DENY 178.32.217.0/24
Anywhere DENY 74.125.82.66
Anywhere DENY 209.85.128.194
Anywhere DENY 209.85.128.196
Anywhere DENY 74.125.82.68
Anywhere DENY 104.236.142.81
Anywhere DENY 60.191.38.77
Anywhere DENY 5.101.0.34
Anywhere DENY 209.126.136.5
Anywhere DENY 145.249.104.109
Anywhere DENY 37.49.226.113
Anywhere DENY 189.112.109.185
Anywhere DENY 172.104.155.22
Anywhere DENY 23.227.207.153
Anywhere DENY 142.4.196.32
Anywhere DENY 210.72.142.7
Anywhere DENY 46.21.174.130
Anywhere DENY 13.91.5.211
Anywhere DENY 212.53.206.58
Anywhere DENY 167.114.60.66
Anywhere DENY 144.217.126.187
Anywhere DENY 144.217.210.228
Anywhere DENY 104.236.163.154
Anywhere DENY 61.236.111.38

Raspberry Pi: Resize Shrink SDCard Image Clone Backup Noobs

If you own a raspberry pi, in some cases it is useful to shrink the sdcard, cause you need often not more than 8/16GB.

What do you need and how to solve shinking or extending the OS:

Hardware you need:

  • 2 empty SDCards (Source + Dest.)
  • 2 SDCard Adaptors Micro-SD to SD Fullsize
  • 2 SD-USB Card Readers, i use them for stable work, cause USB SDCard Readers on Laptops often fails (Shared Sources)
  • 1 Laptop with Ubuntu CD inside the CD-Rom or installed ubuntu with gparted
gparted raspian shrink sdcard
gparted raspian shrink sdcard
gparted shrink progress pi
gparted shrink progress pi

Howto:

  • Plug the Cards into the Adaptors and into the Readers, connect to the Laptop
  • Boot ubuntu and rund $sudo gparted
  • On the destination SDCard (/dev/sdd) create a new msdos Partition Table, (Top Menu gparted)
  • Switch on gparted to Source SDCard (/dev/sdc) where noobs / raspian is
  • Copy first the primary and the last partition to the sdd-disk on same area! (start Bootloader + end Config)
  • Create on sdd at the middle area a new „extended“ partition
  • Go to Source Card sdc and Shrink the patition at the middle near to the „yellow“ area!
  • Enter at Menu the green arrow to start gparted work! Take a coffee now..
  • If gparted is ready the LED Controls of the Adaptors stop blinking! Copy the shrinked partition to sdd paste it into the extended partition
  • Here you can expand the middle partition to all free grey space
  • Enter the green arrow on menu to start gparted work again..
  • If ready (LEDS not blinking), close gparted plug out the cards, and use them to boot the PI

Remark:

  •   Do ALWAYS have a 2nd working Card for the PI as FULL BACKUP!! If a Update fails you are online by restore the SDCard in 10Minutes!!!

Feedback:

  • Mail me Errors if iam wrong..

Rasperry Pi: SD Card Partitions Layout Noobs Backup Restore

If you use a raspberry pi for projects which is shipped with a 8GB SDCard it is very useful to know basics about the root, file and recovery system on the preinstalled card.

  • Major Basics about the Partitioning you find here
  • To save time the raspberry pi offers possibility to run a Full Backup if the pi is ONLINE! Cause the OS is running from RAM like Live Mode!
  • For Backup you need a USB to SDCard Reader with Adaptor Micro SD to SD, and a second SDCard i use 16GB Class 10
  • Plugin the new SDCard into the Adaptor and into the Reader and connect to the raspi USB Hub
  • Open Terminal and run  Full Backup $sudo dd if=/dev/mmcblk0 of=/dev/sda bs=1M
  • Take a Cup of Coffee and wait..~17000sec/8GB  after finish plug off the SDcard Adaptor and keep save
  • If you want to migrate from 8GB to 16Gb Card do a dd-dump and replace the Card inside the pi, after sucessful boot on the 16GB Card, you can allocate a fresh 8gb primary partition with $sudo fdisk /dev/mmcblk0 and do $sudo mkfs.ext4 /dev/mmcblk4 (cause noobs place it into 4!!)

Noobs Partition Layout:

Remark:

  • A firmware upgrade will override the recovery.img on partition_1 too
  • Prefer a full recovery reset  after a raspian version upgrade! (wheezy > jessie)
  • A Kernel Only upgrade can be runned with $sudo rpi-update NO OS UPDATE!
  • Backup can be started over SSH Login to, here you need a program called „tmux“ to hold the process online on disconnect!

Amazon: Cloud Drive Sharing with NFS on Local Network as Photo Uploader Backup

Current the acd_cli tool for amazon drive mount offers no „fsid“ (device /dev) point, that you cant export and share the amazon drive at your home local network.

On the latest raspberry pi OS (jessie) you can use a small workaround to get a NFS Shared Amazon Cloud Backup Uploader (needs python3.4 !)

  • install acd_cli tools, create the auth file (read) and  run $ sudo acd_cli mount /amazoncloud
  • now create a second Folder /sendtoamazon
  • install nfs-kernel-Server and share this Folder with NFS exports to your LAN
  • on the raspberrypi you can use a cron „move“ Job running every 10 Minutes:
    #crontab -e
    */10 * * * * mv /sendtoamazon/* /amazoncloud
    

If you now put files on the /sendtoamazon nfs share from a PC,  the raspberry pi will grab and push it into the cloud via the „acd_cli mount“ a python3 script, remark 10 minutes is ok for small files! on bigger files use over „40“ minutes between pushes. If acd_cli fails check for correct python version! and last python modules!!! (python3-appdirs python3-dateutil python3-requests python3-sqlalchemy python3-pip pip3)

If you have a SMALL BANDWITH for upload you can use „rsync -avz“ instead mv (move) with „throttle“ option to upload files slowly!!

Links:

Howto acd_cli for amazon mount on git

Raspberry Pi: Raspian disadvantages of embedded OS

The last 12 month have been a „golden“ time of the upcomming embedded linux devices, but the custom OS’s offers some problems, you should know for projects:

  • embedded PC’s like the ARM based pi2 / pi3 uses a own compiled OS
  • not every known debian package is available
  • not all compile scripts to rebuild are available on git
  • slow performance can let you run into unuseable problems (heat, load, I/O), correct written scripts run into freezes without any error made by you!
  • you should take time for a closer look at buying them
  • calc the costs of non-x86 systems, later you cant often upgrade, cause embedded systems are not made for
  • dealers take too much money for less performance!
  • embedded devices can’t be grown up for additional precs like compression based on chips!
  • embedded devices need less energy, but can hangup by load over 65%, cause no cooling
  • the are made for learning and small control jobs
  • for NAS / Server jobs take x86 m-atx / micro-atx, cause you can access easy full debian packages
  • not all python + libs packages are available to run projects (example: acd_cli amazon cloud nas backup)
  • current embedded pc livetime at multimedia usage may be 24month cause to much and faster changes

May be that the arm os package trees may grow up, then we can talk again, but current x86 is the cheapest way to build systems without stress! And you have always the reserve to buildin new things of hardware..