script

Powershell Variablen schützen

Written by  on Juli 17, 2016

Weiß jemand, wann sich das Verhalten von Powershell verändert hat? Ich habe in ein paar älteren Scripten Abschnitte ähnlich wie

$X="blabla"
write-output "$X"

Hängt man im Output jetzt einen Doppelpunkt an, passiert auf Windows 2012 R2:

write-output "$X:"
In Zeile:1 Zeichen:15
+ write-output "$X:"
+               ~~~
Ungültiger Variablenverweis. Nach ":" folgte kein Zeichen, das für einen Variablennamen gültig ist. Verwenden Sie ggf.
"${}", um den Namen zu begrenzen.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

Die Lösung ist einfach:

write-output "${X}:"

Aber warum sich das Verhalten auf einmal ändert ist unklar.

Powershell Switch

Written by  on Juli 16, 2016

Wie geht ein select-case auf der Powershell?

switch ($(hostname))
{
  hosta
  {
    [string]$Config="hostA.example.com"
  }
  hostb
  {
	[string]$Config="hostB.example.com"
  }
  default
  {
    write-output "Script not configured for $(hostname)!"
	exit 1
  }
}

write-output $Config

Von Batch zu PowerShell

Written by  on Juli 12, 2016

Ein guter Grund Scripte zu migrieren könnte sein, dass viele Konstrukte einfacher und lesbarer geworden sind.

Es ist eher schwierig auf einen Blick genau zu sagen, was hier für Dateinamen erzeugt werden. Weil scheinbar Strings an festen Positionen zerschnitten werden. Das könnte ausserdem Probleme geben, wenn verschiedene Sprachversionen zum Einsatz kommen:

"%PATH%\%DATE%_%HOUR: =0%.%TIME:~3,2%.%TIME:~6,2%.zip"

Eleganter auf der Powershell

"$PATH\$(get-date -format dd.mm.yyyy_HH.mm.ss).zip"

Download a Variable with PHP

Written by  on April 12, 2016

You have a variable in php which should get into a file for download. But you don’t want to put it actually into a file? Then call the following script which does exactly that for you:

$filename=htmlspecialchars($_GET["name"]);
$content=$_GET['content'];
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=$filename");
echo("$content");

bash timeout

Written by  on Februar 14, 2016

Was tun, wenn man in einem Script nicht ewig auf einen externen Timeout warten möchte?
Wir rufen timeout auf. Das sieht z.B. so in einer Busy Box aus:
$ timeout -t 5 sleep 10
Terminated
$ echo $?
143

Hingegen auf einem Ubuntu 14.04 so:
$ timeout 2 sleep 5
$ echo $?
124

Auch wenn das nicht ganz einheitlich ist, doch ein tolles tool um z.B. curl nach ein paar Sekunden abzubrechen, wenn ein Download nicht funktioniert.

How to logrotate

Written by  on Januar 28, 2016

Just a short example of how some basic logrotate in bash could work:

# Logrotate with more than 5 Megabytes
LOGFILESIZE=$(stat -c%s "$LOGFILE")
if [[ $((LOGFILESIZE/1024/1024)) -gt 5 ]]; then
mv $LOGFILE $LOGFILE.old
fi

wget and the http protocol version

Written by  on Dezember 4, 2015

Found some scripts using wget, which suddenly stopped working after a server upgrade. Strange thing, using curl still worked. Using a proxy server also worked.
So what was the problem?
The new server version didn’t support HTTP version 1.0 and discarded the requests with HTTP status 403, forbidden. But why in all the possible worlds would wget send an HTTP 1.0 header? Oh, it’s just because its a freaking old wget version 1.12 used by Red Hat which simply won’t support HTTP 1.1!
Check out the version information at Wikidpedia.

Wget 1.13, released August 2011, supports HTTP/1.1

Any newer version than this old version from September 2009 wouldn’t have caused troubles.
If I understand this question at Stackoverflow correctly HTTP 1.1 was introduced in 1996 … but the wget version from 2009 won’t support that.

What’s obfuscation and what is it good for?

Written by  on Dezember 3, 2015

To see how PHP code obfuscation works, you might check out the Free Online PHP Obfuscator. What happens there, makes your code completeley unreadable. But, it creates an enormous base64 encoded file, which simply might be too much. Also I had some troubles with *larger* php scripts with around 50 lines.
Nevertheless, you’ll be able to copy a neat trick from there. First a function name gets decoded in hex. If you have no idea about how to do it check out string to hex. So now you are able to encode your function name into a variable

$a="\x73\x79\x73\x74\x65\x6d"

Now we are able to replace the calls to the system function with our variable from

system("openssl req -noout -modulus -in testfile.csr >/dev/null 2>&1",$ret);

to

$a("openssl req -noout -modulus -in testfile.csr >/dev/null 2>&1",$ret);

So what is it good for? Could you hide from the disabled function in the php.ini? That’s a clear no! But this helps, when there is some nasty „maleware scanner“ that would delete your scripts from your webspace because of containing a call of the system function.
Is it secure? As always it depends. If you use PHP to execute some hardcoded shell script, this might work in a secure way. If you hand input to your scripts, you should be extra sure, that you know what you are doing!
Will it scale? I guess no. There is no protection from PHP in a way that you might DDOS yourself i.e. your webspace.
Also check for race conditions. Just think about what will happen, when you write into a file – and another call to the webserver will write into the same file.

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