Friday, February 28, 2014

Open different file types with the same command (Linux)

Sometimes on the command prompt I want to open files without worrying about which application to use for it. Just like with double clicks and file associations. Set this function in your ~/.bashrc and then just use "view <filename>" to render it in the correct application.
function view() {
 if [ $# == 0 ]; then
  echo "usage: $0 <file1> [ <file2> ... ]"
 fi

 OLD_IFS=$IFS
 IFS=""

 for ARG in "$@"; do

  if [ -f "$ARG" ]; then

   MIME=$(file -b --mime-type "$ARG")
   MIME_1=${MIME%/*}

   case "$MIME" in
    application/pdf)
     evince "$ARG" &
     continue;;

    application/zip | application/x-gzip | application/x-bzip2)
     file-roller "$ARG" &
     continue;;

    application/vnd.ms-excel)
     libreoffice --calc "$ARG" &
     continue;;

    application/msword)
     libreoffice --writer "$ARG" &
     continue;;
   esac

   case "$MIME_1" in
    archive)
     file-roller "$ARG" &
     continue;;

    image)
     eog "$ARG" &
     continue;;

    text)
     sublime_text "$ARG"
     continue;;
   esac
   echo "No handler known for type $MIME"
  fi

 done
 IFS=$OLD_IFS
}
And then re-source you Bash shell:
source ~/.bashrc
You're good to go. You might want to modify the file handlers / add some more to match your own setup.

Thursday, February 27, 2014

Quickly test all supported SSLv3/TLSv1 ciphers

I know there’s a bunch of tools out there that do it well, but I like bash scripting, especially nice one-liners.

Here goes:
for cipher in $(openssl ciphers 'ALL:eNULL' | sed -e 's/:/ /g'); do
    echo -n | (openssl s_client -cipher "$cipher" -connect 10.0.0.1:443 >/dev/null 2>&1 && echo PASS $cipher) || echo FAIL $cipher;
done
You can definitely add a grep PASS or FAIL to filter results out.

Wednesday, February 26, 2014

Linux screenshot solution

I often work on different projects, switch between projects all the time, and need to take screenshots. If you’re in a similar situation you may find this useful.

1. Install scrot
sudo apt-get install scrot
2. Create a file with your current project name. it can also be an environment variable but I find the file solution easier to work with.
echo MyProject > $HOME/.current
3. Create a shell script with the following contents:
#!/bin/bash
PROJECT=$(cat ${HOME}/.current)
DATE=$(date +%Y-%m-%d.%s)

if [ ! -d "${HOME}/projects/$PROJECT/shots" ]; then
    mkdir -p "${HOME}/projects/$PROJECT/shots"
fi

scrot "${HOME}/projects/${PROJECT}/shots/${PROJECT}_${DATE}.jpg" -s -q 90
4. You can create a lancher to this shell script that you can put in your launcher bar or whatever:
bash $HOME/screenshot.sh
Your screenshots will be saved for each project under the appropriate folder. Don’t forget to update your project file whenever you switch projects.

Tuesday, February 25, 2014

Export a certificate from Windows registry to PKCS#12

Here’s how to export a certificate from the Windows registry, put it in a PKCS#12 bundle, and then do whatever you want with it, like use it as a client certificate in Burp Proxy.

Why would you want to do that? Well, suppose you have a thick client connecting to some SSL service. It may use a client cert stored in the Windows registry. You must export it properly to be able to perform man-in-the-middle.

1. Export the corresponding registry key for the needed cert from:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SystemCertificates\<YourApp>\Certificates
Keep only the hexa from the file (i.e. remove commas, backslashes and extra junk), copy this data in an hex editor, save the file under clear.cer.

2. Under windows run commands:
certutil -encode clear.cer b64.cer
certutil -dump b64.cer
3. Run the "certmgr.msc" MMC, and under Personal, choose "All Tasks > Import...". Place it under the "Personal" certificate store.

4. Choose the previously created file and import it. Now it should be added to the store. Right-click it to export it, choose PKCS#12, choose to export the priv key + all certs in the path.

5. Use this .pfx in burp as the Client Certificate. Enjoy.

Monday, February 24, 2014

Linux rdesktop fullscreen script

Under Linux with Gnome for example, you might want to rdesktop into a windows host and go full screen. However then it’s a real pain to reduce the window and switch between running apps, so you end up trying to find out the most appropriate resolution for your rdesktop session.

Here is a shell script to detect the screen dimensions, excluding the window manager’s top and bottom bars, and remove the rdesktop window decorations. Also it will map your /tmp and /home/user into the remote desktop session.

Adjust the number of pixels to exclude according to your configuration. I found the value 50 appropriate for a standard gnome-shell in classic mode, each of the bars being 25px.
#!/bin/bash
Xaxis=$(xdpyinfo | grep dimensions | awk '{print $2}' | cut -dx -f1)
Yaxis=$(xdpyinfo | grep dimensions | awk '{print $2}' | cut -dx -f2)
MaxRes=$Xaxis"x"$(($Yaxis-50))
rdesktop $1 -5 -u "$2" -g $MaxRes -r disk:home=${HOME} -r disk:tmp=/tmp -r clipboard:PRIMARYCLIPBOARD -D -K
And then you can run:
sh rdesktop.sh 192.168.0.10 MyUsername
Now go create a launcher for that.

Sunday, February 23, 2014

Mount Windows shared folders on a Linux VirtualBox guest

Here’s how to mount a windows host shared folder from a linux guest, making the subfolders writable by the current non-root user:
mount -t vboxsf SharedFolderName /media/mountpoint -o uid=1000,gid=1000,rw,dmode=777
Change 1000 to your user’s UID.

Saturday, February 22, 2014

Convert VMware virtual machine to VirtualBox

1. Clone the VMware virtual disk to the VDI format:
VBoxManage clonehd source.vmdk dest.vdi —format VDI
2. Create a new VirtualBox VM with the new VDI disk. Set the same amout of memory, number of CPUs etc.

3. Enable IO/APIC under the motherboard settings. In most cases use an IDE controller instead of the default SATA. Attach the VDI as primary master and leave a cdrom slot empty as the primary slave.

4. Boot into the new VM and remove VMware Guest Additions. If the usual program uninstall doesn’t work, attach the VMware Tools ISO as an optical drive and run the following to force uninstallation:
setup.exe /c
5. Install VirtualBox Guest Additions and reboot