shell

Testing OCSP

Written by  on Dezember 23, 2015

To test OCSP you need three things: The issuer certificate, the certificate you’d like to check and the path to the OCSP. All this information seems to be slightly redundant, as the certificate itself already contains the information about the OCSP URL and most of the time also the path to the issuer certificate. It would be very nice if openssl would read all this information from the certificate itself, nevertheless you need this three things to do some basic checks.
Get the required information from the AIA and download the issuer certificate:

[1]Authority Info Access
     Access Method=On-line Certificate Status Protocol (1.3.6.1.5.5.7.48.1)
     Alternative Name:
          URL=http://ocsp.startssl.com/sub/class1/server/ca
[2]Authority Info Access
     Access Method=Certification Authority Issuer (1.3.6.1.5.5.7.48.2)
     Alternative Name:
          URL=http://aia.startssl.com/certs/sub.class1.server.ca.crt

Write the request into a file

openssl ocsp -issuer startssl.cer -cert www.höllrigl.at.cer -no_nonce -url http://ocsp.startssl.com/sub/class1/server/ca -reqout ocsp.req

Convert the request to base64

openssl enc -in ocsp.req -out ocsp.req.b64 -a

The file should look something like this

MEswSTBHMEUwQzAJBgUrDgMCGgUABAR571c85Prvkggz7EWCVSbdzaAjFQQUFbqc
WolwWaxKVTlvLQA1YeCBz7MCChnePRMAAAAArhA=

Then you URL encode that file – you might use some free online decoder like http://meyerweb.com/eric/tools/dencoder/ – but here you have to remove the line breaks in advance. Or you do it directly at the shell

tr -d '\n' < ocsp.req.b64 | php -R 'echo urlencode($argn);'

Your output should look something like

MEswSTBHMEUwQzAJBgUrDgMCGgUABAR571c85Prvkggz7EWCVSbdzaAjFQQUFbqcWolwWaxKVTlvLQA1YeCBz7MCChnePRMAAAAArhA%3D

Now you can build your request to submit in a webbrowser or using curl at the shell.

curl http://ocsp.startssl.com/sub/class1/server/ca/MEswSTBHMEUwQzAJBgUrDgMCGgUABAR571c85Prvkggz7EWCVSbdzaAjFQQUFbqcWolwWaxKVTlvLQA1YeCBz7MCChnePRMAAAAArhA%3D --proxy http://path.to.proxy:8080 > ocsp.resp

With the output file you are able to verify the output with something like

openssl ocsp -respin ocsp.resp -text
OCSP Response Data:
    OCSP Response Status: successful (0x0)
...

Why all this effort, when openssl might do this on it’s own?
Just because openssl won’t work too well in an envrionment where a proxy is required.
The –proxy option seems only to work starting with version openssl 1.1

Alternatively you could also try telnet to connect via a proxy

telnet path.to.proxy 8080
CONNECT ocsp.startssl.com:80 HTTP/1.0
GET /sub/class1/server/ca/MEswSTBHMEUwQzAJBgUrDgMCGgUABAR571c85Prvkggz7EWCVSbdzaAjFQQUFbqcWolwWaxKVTlvLQA1YeCBz7MCChnePRMAAAAArhA%3D

But using curl and writing into a file might be more useful.

Compile OpenSSL

Written by  on Dezember 20, 2015

Most distributions contain only an old version of openssl. If you need a newer version for your scripts, ther often is no other way than compiling it from source. Here I’ll show you, how to do it. And don’t be afraid, it’s super easy! The latest version is a 1.1 development version.
There are only a few prerequisits: You need the packets make and gcc. First you download the source from github and unpack it:

wget https://github.com/openssl/openssl/archive/master.zip
unzip master.zip
cd openssl-master

You simply do the steps for compiling software on unix/linux:

./config
make
make install

You need to do at least the 3rd step as root, so it can install the files for you. By default, everything gets installed into /usr/local/ssl – so it won’t destroy your system OpenSSL.

# /usr/local/ssl/bin/openssl version
OpenSSL 1.1.0-pre2-dev  xx XXX xxxx

Have a lot of fun!

wget isn’t checking CRLs?

Written by  on Dezember 1, 2015

Just a wild theory, but wget is not checking for revoked certificates.
How did I come to this conclusion – and how did I try to verify that?

First some infos about versions – which may be rather important on that topic.

wget --version
GNU Wget 1.15 built on linux-gnu.
openssl version
OpenSSL 1.0.1f 6 Jan 2014

Check about the current certificate, like mine here at https://www.höllrigl.at -> you’ll see a certificate from „StartCom Class 1 Primary Intermediate Server CA“ CA and a CRL at http://crl.startssl.com/crt1-crl.crl

So I’m checking about where to find the CRL and get a nice list

# dig crl.startssl.com +short
www.startssl.com.edgesuite.net.
a1603.g1.akamai.net.
92.122.206.27
92.122.206.10

So let’s see if we find some traffic that goes there – first I tried a ping:

# tcpdump -i eth0 host crl.startssl.com
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:34:23.967704 IP 192.168.3.1 > a92-122-206-10.deploy.akamaitechnologies.com: ICMP echo request, id 18687, seq 1, length 64
21:34:24.016819 IP a92-122-206-10.deploy.akamaitechnologies.com > 192.168.3.1: ICMP echo reply, id 18687, seq 1, length 64

Next thing, fetch that URL with wget

# wget https://www.höllrigl.at
--2015-11-30 21:35:46--  https://www.xn--hllrigl-90a.at/
Resolving www.höllrigl.at (www.xn--hllrigl-90a.at)... 193.239.248.170, 2a04:5540:1:41::10
Connecting to www.höllrigl.at (www.xn--hllrigl-90a.at)|193.239.248.170|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.2’

    [ <=>                                                                                                                                                                                             ] 29,620      --.-K/s   in 0.05s

2015-11-30 21:35:48 (594 KB/s) - ‘index.html.2’ saved [29620]

Now I’d like to show to you the dumped packages in tcpdump – but there are none.
Other strong indicators are, that there only started to be a ‚–crl-file=file‘ option to wget starting with 1.16, which allows you to check your certificate against a locally stored CRL file.

Batch Script und die Prozentzeichen

Written by  on November 25, 2015

Ich hab mich wieder Mal über ein Script geärgert.
Folgendes Beispiel hat beim Testen auf der Shell wunderbar funktioniert

C:\tmp\test>for /f %f in ('dir *.txt') DO echo %f
Datei nicht gefunden

C:\tmp\test>echo Volume
Volume

C:\tmp\test>echo Volumeseriennummer:
Volumeseriennummer:

C:\tmp\test>echo Verzeichnis
Verzeichnis

Das macht Output mit dem Befehl zwischen den einfachen Anführungszeichen.
Also ab in ein Script damit und nochmal ausgeführt

C:\tmp\test>test

Was? Kein Output – sollte das Script nicht das gleiche tun, wie wenn die Zeile auf der Shell geschrieben geht?
Eine kleine Änderung im Script – %f wird mit %%f ersetzt:

for /f %%f in ('dir *.txt') DO echo %%f

Und jetzt kommt aus dem Script das gleiche raus, wie vorher auf der Commandline:

C:\tmp\test>test

C:\tmp\test>for /F %f in ('dir *.txt') DO echo %f
Datei nicht gefunden

C:\tmp\test>echo Volume
Volume

C:\tmp\test>echo Volumeseriennummer:
Volumeseriennummer:

C:\tmp\test>echo Verzeichnis
Verzeichnis

Quelle und Erklärung bei Stackoverflow

grep -v

Written by  on Oktober 29, 2015

Wie sucht man ob ein Prozess läuft?

# ps aux | grep smb
root       699  0.0  0.4  28800  4036 ?        Ss   Oct22   0:43 smbd -F
root       768  0.0  0.1  28800  1604 ?        S    Oct22   0:04 smbd -F
root     21411  0.0  0.0   4680   772 pts/0    S+   18:06   0:00 grep --color=auto smb

Das ist zuviel Output, weil auch der grep-Prozess selber angezeigt wird. Dafür gibt es eine einfältige Lösung:

# ps aux | grep smb | grep -v grep
root       699  0.0  0.4  28800  4036 ?        Ss   Oct22   0:43 smbd -F
root       768  0.0  0.1  28800  1604 ?        S    Oct22   0:05 smbd -F

Aber wie geht es besser?

ps aux | grep [s]mb
root       699  0.0  0.4  28800  4036 ?        Ss   Oct22   0:43 smbd -F
root       768  0.0  0.1  28800  1604 ?        S    Oct22   0:05 smbd -F

Warum funktioniert das? Ganz einfach, weil in der Prozessliste jetzt der String „smb“ nicht mehr vorkommt beim grep-Befehl, sondern der String „[s]mb“. Gesucht wird von dem Befehl aber trotzdem nach „smb“.
Braucht man nur die PID vom Prozess geht es aber einfacher:

# pgrep smb
699
768

Soll das ganze nur zum Beenden der Prozesse genutzt werden, braucht man gar keinen grep mehr.

# killall smbd

Dateien nach Datum Sortieren

Written by  on Oktober 22, 2015

Dateien sortieren nach Datum geht mit dem simplen „ls“ mit der Option „-t“.
Aber wie geht das Übergreifend über verschiedene Ordner?

$ ls Windows/* Programme/* -ldt
drwxr-x---    1 42949672 42949672         0 Oct 22 20:48 Windows/Prefetch
-rwxrwx---    1 SYSTEM   42949672       275 Oct 22 20:24 Windows/WindowsUpdate.log
drwxr-x---    1 42949672 42949672         0 Oct 22 20:14 Windows/Temp
drwxrwx---    1 42949672 42949672         0 Oct 22 20:10 Windows/Microsoft.NET
drwxrwx---    1 SYSTEM   42949672         0 Oct 22 19:28 Windows/AppReadiness
-rwxrwx---    1 SYSTEM   42949672     67584 Oct 22 19:18 Windows/bootstat.dat
drwxrwxr-x    1 Administ 42949672         0 Oct 21 21:08 Windows/Installer
drwxrwx---    1 SYSTEM   42949672         0 Oct 21 21:07 Programme/Java
drwxrwx---    1 42949672 42949672         0 Oct 21 21:03 Windows/INF
-rwxrwx---    1 SYSTEM   42949672     34668 Oct 20 21:18 Windows/setupact.log
drwxr-x---    1 42949672 42949672         0 Oct 20 19:23 Windows/WinSxS
drwxrwx---    1 Administ 42949672         0 Oct 20 19:20 Windows/CbsTemp
drwxrwx---    1 42949672 42949672         0 Oct 20 19:20 Windows/SysWOW64
drwxrwx---    1 42949672 42949672         0 Oct 20 19:20 Windows/System32
drwxrwx---    1 SYSTEM   42949672         0 Oct 14 19:13 Programme/NVIDIA Corporation
drwxrwx---    1 SYSTEM   42949672         0 Oct 14 19:13 Windows/Help
-rwxrwx---    1 Administ 42949672      4882 Oct 13 22:18 Windows/PFRO.log
drwxrwx---    1 42949672 42949672         0 Oct 13 22:18 Windows/AppPatch
drwxrwx---    1 42949672 42949672         0 Oct 13 20:57 Windows/Fonts
drwxrwx---    1 42949672 42949672         0 Oct 13 20:57 Windows/twain_32
drwxrwx---    1 SYSTEM   42949672         0 Oct 13 20:57 Programme/HP
drwxrwx---    1 Administ 42949672         0 Oct 13 20:49 Windows/assembly
-rwxrwx---    1 SYSTEM   42949672       478 Oct 13 20:06 Windows/win.ini
drwxrwx---    1 SYSTEM   42949672         0 Oct 13 20:04 Windows/PCHEALTH
drwxrwx---    1 42949672 42949672         0 Oct  5 18:48 Windows/L2Schemas
drwxrwx---    1 42949672 42949672         0 Oct  5 18:48 Windows/Provisioning
drwxrwx---    1 42949672 42949672         0 Oct  5 18:48 Windows/PurchaseDialog
drwxrwx---    1 SYSTEM   42949672         0 Sep 20 08:30 Programme/Oracle
drwxrwx---    1 Administ Administ         0 Sep 19 08:58 Windows/Tasks
drwxrwx---    1 42949672 42949672         0 Sep  9 18:28 Programme/Windows Journal
drwxrwx---    1 SYSTEM   42949672         0 Sep  6 08:52 Programme/Calibre2
drwxrwx---    1 42949672 42949672         0 Aug 22 08:51 Windows/DevicesFlow
drwxrwx---    1 Administ UsersGrp         0 Aug 18 22:16 Programme/FileZilla FTP Client
drwxrwx---    1 SYSTEM   42949672         0 Aug 15 17:30 Programme/7-Zip

...

Sortierung umdrehen:

ls Windows/* Programme/* -ldrtR
-rwxrwx---    1 SYSTEM   42949672      1405 Jun 10  2009 Windows/msdfmap.ini
-rwxrwx---    1 SYSTEM   42949672       219 Jun 10  2009 Windows/system.ini
-rwxrwx---    1 SYSTEM   42949672    302448 Mar  8  2012 Windows/WLXPGSS.SCR
drwxrwx---    1 Administ 42949672         0 Feb  2  2015 Windows/CSC
-rwxrwx---    1 SYSTEM   42949672      1313 Feb  2  2015 Windows/TSSysprep.log
lrwxrwxrwx    1 SYSTEM   42949672        36 Feb  2  2015 Programme/Gemeinsame Dateien -> /drives/c/Program Files/Common Files
drwxrwx---    1 SYSTEM   42949672         0 Feb  2  2015 Programme/Microsoft Analysis Services
drwxrwx---    1 SYSTEM   42949672         0 Feb  2  2015 Programme/Microsoft Sync Framework
drwxrwx---    1 SYSTEM   42949672         0 Feb  2  2015 Programme/Microsoft SQL Server Compact Edition
drwxrwx---    1 SYSTEM   42949672         0 Feb  2  2015 Programme/Microsoft Office
drwxrwx---    1 SYSTEM   42949672         0 Feb  2  2015 Programme/Microsoft Synchronization Services
-rwxrwx---    1 Administ UsersGrp       202 Feb  3  2015 Windows/DirectX.log
drwxrwx---    1 SYSTEM   42949672         0 Feb  3  2015 Programme/Windows Live
-rwxrwx---    1 SYSTEM   42949672     15936 Feb  4  2015 Windows/IE11_main.log
drwxrwx---    1 Administ UsersGrp         0 Feb  7  2015 Programme/Tracker Software
-rwxrwx---    1 Administ UsersGrp     10446 Apr 28 10:36 Windows/DPINST.LOG
drwxrwx---    1 Administ UsersGrp         0 Apr 28 10:52 Windows/Hewlett-Packard
...

Bash: Variablen Bearbeiten

Written by  on September 24, 2015

Die Bash kann einiges mit Variablen anstellen, was man in vielen Scripten relativ kompliziert gelöst sieht.

Ein Beispiel, das in vielen Scripten vorkommt: Eine Ausgabedatei soll genau so heißen wie die Eingabedatei, aber eine andere Erweiterung haben.

A="test.txt"
B="${A%%.txt}.csv"

Was steht jetzt in Variable B?

echo $B
test.csv

Wie funktioniert das?
Mit dem ‚%‘-Operator kann man von einer Variable Zeichen vom Ende her löschen.
Mit ‚%%‘ werden auch mehrere Vorkommen der Variable vom Ende her gelöscht.

Vom Anfang her löschen geht mit ‚#‘

Ersetzen ähnlich wie mit sed geht über den ‚/‘-Operator, ‚^‘ und ‚,‘ machen Groß- bzw. Kleinbuchstaben.

Schöne Beispiele dazu finden sich im Web z.B. beim Linuxmagazin.

Näheres dazu ist unter dem Suchbegriff „Parameter Expansion“ zu finden. Auch in der Manpage von bash.

Verschlüsseln auf der Shell

Written by  on Mai 3, 2015

Um mal eben eine Datei oder einen Stream auf der Shell zu verschlüsseln kann man bcrypt oder ccrypt verwenden. Das ganze funktioniert etwa so:

~ echo hallo | ccrypt -f > output
Enter encryption key:
Enter encryption key: (repeat)
~ cat output
u~�-�x��
O���Ļ_U
~ ccrypt -f -d < output
Enter decryption key:
hallo

Beschrieben zum Beispiel unter Encrypt Files on Linux

Locking auf der Bash

Written by  on November 29, 2014

Dies ist eine aktualisierte Version von Locking auf der Bash vom  19. Mai 2012. Flock für File Locking zu benutzen hat sich seitdem leicht verändert.

Dazu ein neues Testscript test.sh:

#!/bin/bash
echo start
sleep 20
echo end

Der Aufruf lautet jetzt

flock -w1 -x ./test.lock ./test.sh

Das Lockfile wird neu angelegt. Es ist nicht mehr möglich das Scriptfile selber auch als Lockfile zu benutzen. Versucht man es doch führt das zur Fehlermeldung

# flock -w1 -x ./test.sh ./test.sh
flock: ./test.sh: Text file busy

Jetzt zwei Mal das Script starten – wegen dem Timeout mit -w1 läuft es nur ein Mal

$ flock -w1 -x ./test.lock ./test.sh &amp;
[1] 19423
$ start
flock -w1 -x ./test.lock ./test.sh &amp;
[2] 19426
$ jobs
[1]- Running flock -w1 -x ./test.lock ./test.sh &amp;
[2]+ Exit 1 flock -w1 -x ./test.lock ./test.sh
$ end
[1]+ Done flock -w1 -x ./test.lock ./test.sh

 

Bücher zur Shell Programmierung bei Amazon

Bash Array

Written by  on Januar 26, 2014

 

Nur ein Beispiel, wie man ein Array auf der Bash benutzen kann.

Als erstes die Definition von zwei Arrays:

XX=("x1" "x2")
XY=("y1" "y2")

Dann das Anwendungsbeispiel, wichtig vor allem, dass die Elemente gezählt werden:

for (( i = 0; i &lt; ${#XX[@]}; i++ ))
do
echo "${XX[$i]} ${XY[$i]}"
done

Dann das Anwendungsbeispiel, wichtig vor allem, dass die Elemente gezählt werden:

x1 y1
x2 y2