Saturday, April 6, 2019

Shred USB Drive

To reset a complete drive...

Code:

sudo shred -fvzn0 /dev/sdX

where /dev/sdX is the drive to be cleared out.

Note that this will wipe all of the drive including the boot sector, and the system partition as well. If you accidentally use it on /dev/sda or your boot drive this will require you to re-install the boot sector and the main partition of the drive.

As for the switches in use above ...

f = force
v = verbose
z = zero  
n0 = 0 iterations   


*z performs a single pass of zeros written to the drive sometimes referred to as a "factory reset".
*n0 means do not do any passes using random data. This is very low data security but speeds up the resetting process drastically. Increase this value for higher data security protection if needed. If this value is NOT set then shred by default will do 3 passes of random data (very slow for the command to complete its run).

Simple VBox Creation Script

There has been something on my list for sometime and that is to preseed an installation of Ubuntu and VirtualBox. I've been wanting to automate the installation of a virtual machine using VirtualBox and configure everything that I need from CPU to Memory to Forwarded ports. Mostly I deal with Ubuntu Linux machines and end up installing a VM over and over ... from messed up configurations to maybe something different that I want to try. The problem with this is that I spend too much time configuring the settings, and installing the OS. So I finally decided to create something that would do most of the work for me. Now I know you're saying why not just learn to use Packer and do it that way where you create an image that you use multiple times and can port elsewhere. Well, mostly this is for testing and getting things to work. It has it's benefits and it has it's draw backs, but for the problem that I am trying to solve at the moment it works. Here is the script to create the VM, and if you have an automated iso for the install it will start that and then shutdown when it is complete.
 
    #!/bin/bash

    #TODO - ADD Input for VMName
    NAME="ubuntu"
    VMDIR="/home/$USER/Documents/vm/$NAME/"
    DVD="/home/$USER/Documents/vm/iso/ubuntu-18.04-mini-amd64.iso"

    echo -e "Creating VirtualBox VM"
    VBoxManage createvm --name $NAME --ostype Ubuntu_64 --register

    if [ ! -d "$VMDIR" ]; then
      mkdir /home/$USER/Documents/vm/$NAME/
    fi

    echo -e -e "\nCreating VM Disk VDI file"
    VBoxManage createmedium --filename $VMDIR/$NAME.vdi --size 40960

    echo -e "\nAttaching storage"
    VBoxManage storagectl $NAME --name SATA --add SATA --controller IntelAhci
    VBoxManage storageattach $NAME --storagectl SATA --port 0 --device 0 --type hdd --medium $VMDIR/$NAME.vdi
    VBoxManage storagectl $NAME --name IDE --add ide
    VBoxManage storageattach $NAME --storagectl IDE --port 0 --device 0 --type dvddrive --medium $DVD

    echo -e "\nModify VM Settings"
    VBoxManage modifyvm $NAME --memory 2048
    VBoxManage modifyvm $NAME --ioapic on
    VBoxManage modifyvm $NAME --boot1 dvd --boot2 disk --boot3 none --boot4 none
    VBoxManage modifyvm $NAME --cpus 1
    VBoxManage modifyvm $NAME --audio none
    VBoxManage modifyvm $NAME --usb off
    VBoxManage modifyvm $NAME --usbehci off
    VBoxManage modifyvm $NAME --usbxhci off
    #VBoxManage modifyvm $NAME --nic1 bridged --bridgeadapter1 wlan0 --nic2 nat
    VBoxManage modifyvm $NAME --nic1 nat

    echo -e "\nAdd SSH port forwarding to 2222"
    VBoxManage modifyvm $NAME --natpf1 "guestssh,tcp,,2222,,22"

    #Start VM
    VBoxManage startvm $NAME --type gui


This will launch the VM and mount the iso that is used for an unattended install with SSH keys and forwards the SSH port to the localhost on 2222 where you can connect using a private key or password similar to this: ssh -i ../.ssh/id_rsa root@127.0.0.1 -p 2222

Thursday, April 4, 2019

Simple bash to change /etc/resolv.conf

Here is a simple snippet to change the /etc/resolv.conf file on a Linux machine to be added into a shell script to change the DNS servers and other settings in the file. I went with this at the moment because the automation is not quite there and needed something quick to change the file on a small number of machines and will build more upon this in the coming days.

sudo su - <<HERE
rm /etc/resolv.conf &&
touch /etc/resolv.conf && \
printf "options timeout:2 attempts:5\n \
search dev.lab\n \
nameserver 172.0.0.100\n \
nameserver 172.0.0.101\n" >> /etc/resolv.conf
HERE