Wednesday, July 17, 2013

Multiple Operating Systems on One Flashdrive from Linux

Recently I decided to take some old flash drives I have not been using and turn them into tools for things like security and data recovery. The problem became I had so many tools I wanted to cram on them, but only a handful of flashdrives. Furthermore, a good portion of what I wanted was small and putting them on their own dedicated drive was a bad use of space.

So now the hunt was on to turn one of the larger flash drives into a multi-boot. Normally I use UNetBootin to create bootable flash drives. I did this for Kali and PartedMagic because I started with making those tools first. Now after much trying to use UNetBootin to create multiple operating systems on one drive, tried multiple partitions, I went to Google. Google gave some possible ways to do it, but they were inefficient. So finally I found something late at night.

Multisystem is a quick and easy way to set this up. Despite it being in French (according to Google), but I only speak English and can work it with no problem, so it's all good. You can load operating systems onto it by dragging and dropping. You can view more options by clicking the button on the left hand side with the eyeball. To see what any of the buttons do, just hover over them. Everything else should be pretty straight-forward.

Now after doing all of this, I found one slight problem. Installing tinycore is done slightly incorrect and makes it so that you can't setup programs for loading on the boot as a result. So if you want to set up tinycore, here's what you should do after it is all set up. First, you want to browse the files. Change the folder "cde" to "tce". Next, open up boot/grub/grub.cfg. Look for something along the line of this in the file:

#MULTISYSTEM_MENU_DEBUT|12-07-2013-00:13:33-278461639|cde|multisystem-tinycore|12Mio|
menuentry "Tiny Core Linux" {
linux /cde/boot/vmlinuz quiet cde showapps desktop=flwm_topside
initrd /cde/boot/core.gz
}
#MULTISYSTEM_MENU_FIN|12-07-2013-00:13:33-278461639|cde|multisystem-tinycore|12Mio|

Now you want to change it so that all the places it says "cde" say "tce". So the end result of the example posted looks like this.

#MULTISYSTEM_MENU_DEBUT|12-07-2013-00:13:33-278461639|tce|multisystem-tinycore|12Mio|
menuentry "Tiny Core Linux" {
linux /tce/boot/vmlinuz quiet tce showapps desktop=flwm_topside
initrd /tce/boot/core.gz
}
#MULTISYSTEM_MENU_FIN|12-07-2013-00:13:33-278461639|tce|multisystem-tinycore|12Mio|

I have not come across any other problems yet, but I haven't really messed around with everything on it yet.

Wednesday, July 3, 2013

Damn Vulnerable Linux Full Install on Hard Drive

Damn Vulnerable Linux (DVL) is a Linux distribution that was made purposefully insecure and configured poorly for a learning experience to teach things like exploitation. I managed to find an ISO of it off of some torrent a while back and never really did anything with it. Recently, I got an old laptop that was too broken to do what was wanted with it, so I figured I may as well use it for something. So I installed DVL on to it so I have a machine I can experiment with and get back to learning hacking and other such things. Now most of what I found online was installing into a virtual machine, and the directions were also wrong on quite a few steps. So using my limited knowledge, I played around until I got it right. So here's a quick step-by-step to install DVL on a USB flash drive. I'll try to set up a download link at some point as well.

Put the image on a flash drive, I used Unetbootin.

When booting up the computer with the flash drive, remember to check the boot order in the BIOS so that the flash drive is booted first.

When the system loads, log in. Username is root, password is toor.

Now you should be in the prompt. These instructions are for a clean install. First thing we want to do here is identify what device is where, as far as the hard drive and the USB drive. For me, the hard drive was /dev/hda and the USB drive was /dev/sda. This may change if you have multiple hard drives. We need to tweak some stuff on the hard drive, so we want to unmount it first. Check what is mounted in /mnt, then unmount every folder related to the hard drive. For me, it was just this.

umount /mnt/hda

Next we want to repartition  everything because why not. Run this command, change the hda part if you're setting up on a different system.

fdisk /dev/hda

Now in fdisk, you want to delete any partitions. Press p then enter to see all partitions. Delete all of them. The letter d then enter should do the trick, if there's more than one, just repeat starting at 1 and moving up until it says there are no more. Now we want to write a partition. Press n then enter sticking with the default values (just press enter for the prompts) because that makes life easy. Now to save it, press w then enter and it should take a moment to write the partitions. After this, you should be back at the original prompt.

Okay, now we have a partition, but we have not formatted it to a file system type. So if you've been working on hda, just follow along, if not, remember to change hda to your device. Run this command.

mkfs.ext3 /dev/hda1

Let that run and BAM, ext3 partition. The tutorial I found didn't put the number, which lead to everything completely screwing up, so it's important. If not, it gives a warning message I should have listened to.

So now we want to mount back our newly partitioned and formatted space.

mkdir /mnt/dvl
mount /dev/hda1 /mnt/dvl

Okay, now we should be ready and raring to go to the GUI!

startx

Okay, now in the GUI, we do some fun stuff. In the bottom right hand corner is a German flag with a "de" written on it (short for deustch, for those that didn't know and are curious). Right click on that to bring up the language settings and change that to your locale. Trying to use an American keyboard on a German layout does not work at all.

Okay, now that that is out of the way, in the bottom left corner is the thing for the KDE start menu. Click on that, go to the "Be ReSlaxed" > System > BackTrack Installer.

Leave the first box blank, set the second box to /mnt/dvl if it isn't already. Change the radio buttons from Live to Real. Click the install button and let it do its thing. Once it's finished, hold and because we're not done yet. You can click the close button then go to the logout option under the start menu. Now we should be back at the prompt.

At this prompt, you want to put in this command.

lilo -v -b /dev/hda

If you have a different hard drive setup, this command may need to be different. If that is the case, take a moment to look more into the lilo man pages because I don't know much about it.

If all went well, type this command.

poweroff

After it shuts down, remove the USB drive, start it back up and make sure everything works. It took myself 3 times because I kept goofing up, first time I accidentally wiped the USB drive. Second time I forgot to put lilo on. Third time was the charm.

Wednesday, April 10, 2013

Resistor Calculator wxPython

So recently I decided to take a serious attempt at programming with using a GUI. While it has not been easy, the results are very satisfying. This program was entirely from scratch to get my feet wet, but I think my next project will be converting an already existing program to a GUI.

I chose wx over Tkinter for a couple reasons. The first was wx looks native while Tkinter looks like a Java application. Also, much like a Java application, however the windows are made with Tkinter, they have a bad habit of glitching out and making pieces illegible. The downside to wx is that it is not included with Python, however the wx packages are easy enough to get a hold of (in many different languages) and easy to install.

I'm happy with the end result, both the GUI and the code. I spent some extra time afterwards cleaning up the code, expanding variable names and commenting. This proved useful since I deleted a bunch of code that didn't do anything. Granted it's not perfect, I'm happy that overall it works.

You can check it out on my site here, or just copy/paste the source code.

#!/usr/bin/env python
# Author: Matthew DeSantis
#
# Resistor Calc

import wx

# Colors aranged by value (index=value)
COLOR = [
    'black',
    'brown',
    'red',
    'orange',
    'yellow',
    'green',
    'blue',
    'violet',
    'grey',
    'white']

# Colors because the default ones suck
COLORCODE = {
    "black":    "#000000",
    "brown":    "#a52a2a",
    "red":      "#ff0000",
    "orange":   "#ffa500",
    "yellow":   "#ffff00",
    "green":    "#008000",
    "blue":     "#0000ff",
    "violet":   "#800080",
    "grey":     "#808080",
    "white":    "#ffffff",
    "silver":   "#c0c0c0",
    "gold":     "#d4a017"}

# Multipliers in a dictionary, organized by value for ease of reading
MULTIPLIER = {
    1e-2:   'silver',
    1e-1:   'gold',
    1e0:    'black',
    1e1:    'brown',
    1e2:    'red',
    1e3:    'orange',
    1e4:    'yellow',
    1e5:    'green',
    1e6:    'blue',
    1e7:    'violet',}

# Converts a number to a list of resistor colors of equivalent value
def getColors(num):
    # To-be return value
    returnVals = []
    failReturnVals = [COLOR[0], COLOR[0], MULTIPLIER[1e0]]
    # Process the number
    indexPlaceholder = 0
    numStr = str(num)
    bandValues = ""

    # For anything smaller than 0
    if num < 1 and num >= 0.01:
        while indexPlaceholder < len(numStr):
            # Discard leading 0's and decimal
            if numStr[indexPlaceholder] == "0" or numStr[indexPlaceholder] == ".":
                indexPlaceholder += 1
            else:
                break

        # It's all zeroes, time to leave
        else:
            return failReturnVals

        bandValues = numStr[indexPlaceholder:]

        # Short a value, needs a black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        # Ready return color values
        before = True

        for value in bandValues:
            if len(returnVals) < 2:
                returnVals.append(COLOR[int(value)])
            elif value != "0":
                ret.append(COLOR[int(value)])
            else:
                break

    # And everything else
    elif num >= 1 and num <= 9990000000:
        indexPlaceholder = 2
        bandValues = numStr[:indexPlaceholder]

        # If there's 4 stripes
        if (num / float(bandValues)) % 10:
            indexPlaceholder = 3
            bandValues = numStr[:indexPlaceholder]

        # If there's a third band to add
        if len(numStr) > indexPlaceholder:
            if numStr[indexPlaceholder] != "0":
                bandValues = numStr[:indexPlaceholder + 1]

        # Needs another black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        for value in bandValues:
            if value == ".":
                continue

            returnVals.append(COLOR[int(value)])

    # Outside the limit
    else:
        return failReturnVals

    # Get the multiplier
    returnVals.append(MULTIPLIER[round(num / float(bandValues.replace(".", "")), 2)])

    return returnVals

#List of colors to Ohms
def getOhms(colors):
    bandValues = ""
    # Key storage
    multiply = 0

    # Get the index numbers of the colors
    while len(colors) > 1:
        bandValues = bandValues + str(COLOR.index(colors.pop(0)))

    # Find the key (multiplier) based on color (no easy way to do this)
    for key, value in MULTIPLIER.items():
        if value == colors[0]:
            multiply = key
            break

    # Color value * multiplier
    return float(bandValues) * multiply

def main():
    # Initialize app
    app = wx.App()
    frame = Window(None, "Resistor Calc")
    frame.display()
    app.MainLoop()

# The GUI class setup
class Window(wx.Frame):
    def __init__(self, parent, title):
        # Initialize the frame
        super(Window, self).__init__(
            parent,
            title=title,
            style=wx.DEFAULT_FRAME_STYLE,
            size=(550, 200))

        # UI setup
        self.uiInit()
        self.evtInit()
        self.colorSet()

        # Control variables
        self.lock = False
        self.prvs = self.ohmstxt.GetValue()

    def uiInit(self):
        # Third band list (needs optional added)
        self.r3colors = COLOR[:]
        self.r3colors.insert(0, "-")

        # Fourth band has some extra as well (organize by values)
        self.r4colors = COLOR[:-2]
        self.r4colors.insert(0, "gold")
        self.r4colors.insert(0, "silver")

        # Create panel and sub panels for colored boxes
        self.panel = wx.Panel(self)

        self.pr1 = wx.Panel(self.panel)
        self.pr2 = wx.Panel(self.panel)
        self.pr3 = wx.Panel(self.panel)
        self.pr4 = wx.Panel(self.panel)

        # Initialize font
        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(12)

        # Boxes for resizing and aligning
        # 3 tall
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        # First row
        lblohms = wx.StaticText(self.panel, label="Ohms:")
        lblohms.SetFont(font)

        self.ohmstxt = wx.TextCtrl(self.panel, value="0.0")

        # Second row
        lblcolor = wx.StaticText(self.panel, label="Colors:")
        lblcolor.SetFont(font)

        self.r1 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r2 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r3 = wx.ComboBox(
            self.panel,
            choices=self.r3colors,
            style=wx.CB_READONLY)

        self.r4 = wx.ComboBox(
            self.panel,
            choices=self.r4colors,
            style=wx.CB_READONLY)

        # Create first row
        hbox1.Add(lblohms, flag=wx.RIGHT, border=8)
        hbox1.Add(self.ohmstxt, proportion=1)

        # Create second row
        hbox2.AddMany([
            (lblcolor, wx.RIGHT, 8),
            (self.r1,),
            (self.r2,),
            (self.r3,),
            (self.r4,)])

        # Create third row
        hbox3.AddMany([
            (self.pr1, 1, wx.EXPAND),
            (self.pr2, 1, wx.EXPAND),
            (self.pr3, 1, wx.EXPAND),
            (self.pr4, 1, wx.EXPAND)])

        # Insert rows with padding
        self.vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox3, 1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))

        # Sets everything in place on the window
        self.panel.SetSizer(self.vbox)

        # Set combobox selections (not intialized by default
        self.r1.SetSelection(0)
        self.r2.SetSelection(0)
        self.r3.SetSelection(0)
        self.r4.SetSelection(2)

    # Initialize events
    def evtInit(self):
        self.Bind(wx.EVT_COMBOBOX, self.onCBChange)
        self.Bind(wx.EVT_TEXT, self.onTextChange)

    # Combobox was changed
    def onCBChange(self, e):
        self.lock = True

        bandColors = []
        thirdBand = self.r3.GetValue()

        bandColors.append(self.r1.GetValue())
        bandColors.append(self.r2.GetValue())

        # Third band is optional, check to see if it's actually used
        if thirdBand != "-":
            bandColors.append(thirdBand)

        bandColors.append(self.r4.GetValue())

        self.ohmstxt.SetValue(str(getOhms(bandColors)))
        self.colorSet()

    # Text change
    def onTextChange(self, e):
        ohms = self.ohmstxt.GetValue()

        # This is because event fires twice for some unknown reason
        if self.lock or ohms == self.prvs:
            self.prvs = ohms
            self.lock = False
            return None

        # If no value given, default to zero
        if not ohms:
            ohms = "0"

        try:
            bandColors = getColors(float(ohms))
            self.r1.SetSelection(COLOR.index(bandColors[0]))
            self.r2.SetSelection(COLOR.index(bandColors[1]))

            # All 4 bands are present
            if len(bandColors) > 3:
                self.r3.SetSelection(self.r3colors.index(bandColors[2]))
                self.r4.SetSelection(self.r4colors.index(bandColors[3]))
            # Third band is not present
            else:
                self.r3.SetSelection(0)
                self.r4.SetSelection(self.r4colors.index(bandColors[2]))

            self.colorSet()

        # Not a valid number or possible value
        except (ValueError, KeyError):
            pass

    # Display the colors for the bands
    def colorSet(self):
        self.pr1.SetBackgroundColour(COLORCODE[self.r1.GetValue()])
        self.pr2.SetBackgroundColour(COLORCODE[self.r2.GetValue()])

        # If band 3 isn't used, don't show it
        if self.r3.GetValue() == "-":
            self.pr3.SetBackgroundColour(wx.NullColour)
        else:
            self.pr3.SetBackgroundColour(COLORCODE[self.r3.GetValue()])

        self.pr4.SetBackgroundColour(COLORCODE[self.r4.GetValue()])

    def display(self):
        self.Centre
        self.Show()

if __name__ == '__main__':
    main()

Sunday, April 7, 2013

Shared Object with GHC 7

I was hitting my face on my desk for a few hours today trying to work this out. So here's my post to hopefully save anyone else who ended up stuck on this. I made a bash script to do it all, so here's the script. Note that this makes dynamic shared objects (.so files), still trying to figure out how to do it statically.

#!/bin/bash

TMPHELP="module_init.c"
FILENAME=${1%.*}
declare -i EXITCODE

cat <<TMPHELPER > $TMPHELP
#define CAT(a,b) XCAT(a,b)
#define XCAT(a,b) a ## b
#define STR(a) XSTR(a)
#define XSTR(a) #a

#include <HsFFI.h>

extern void CAT(__stginit_, MODULE)(void);

static void library_init(void) __attribute__((constructor));
static void library_init(void) {
    /* This seems to be a no-op, but it makes the GHCRTS envvar work. */
    static char *argv[] = { STR(MODULE) ".so", 0 }, **argv_ = argv;
    static int argc = 1;

    hs_init(&argc, &argv_);
    hs_add_root(CAT(__stginit_, MODULE));
}

static void library_exit(void) __attribute__((destructor));
static void library_exit(void) {
    hs_exit();
}
TMPHELPER

ghc -O2 --make -dynamic -shared -fPIC $2 -o "${FILENAME}.so" -optl-Wl,-rpath,/usr/lib/ghc/ -lHSrts_debug-ghc7.4.1 $1 $TMPHELP  >&1

EXITCODE=$?

set -x

if [[ $EXITCODE -eq 0 ]] ; then
    rm "${FILENAME}.hi"
    rm "${FILENAME}.o"
fi

if [[ -f ${FILENAME}_stub.h ]] ; then
    rm "${FILENAME}_stub.h"
fi

rm $TMPHELP
exit $EXITCODE

I'm honestly not 100% sure how everything works, but my main goal was to create shared objects so I could use them in Python. One thing to note if you are loading a shared object into Python made with Haskell, the IO functions don't seem to display on IDLE (works fine on the terminal).

Tuesday, April 2, 2013

Arduino Wii Nunchuck on Linux

I came across a DIY project over on the website for Maker magazine to connected a Wii Nunchuck to your computer as a mouse using an Arduino board and Python. Problem was, they only made it for Windows (they also suck at Python programming, but whatever). So after quite a bit of work over the past three days, I finally got it working.

The problem is Windows is not very secure and anything running on it can really do whatever it pleases, like say move the mouse around or click on things, without much effort. Linux has a bit more to it.

I went in originally planning on using libX11.so through ctypes.cdll, but setting up an event on that was not only pure hell, I don't even know if it is possible. Plan B presented itself as Xlib. The documents are not that complete, examples seem to be scarce and I don't have much in the way of patience.

You can check everything out here, I'm going to skim over the details and then show the Python code, all commented.

First thing first, get the library for interfacing the Nunchuck here. The readme there should tell you how to use it.

Next you want to jam some wires into your Nunchuck (the jumper wires from the starter kit fit fine without breaking anything) like so and connect it to your board to the corresponding holes.


From there you want to upload the example code for the Wii Nunchuck library so we can just process the data as a string rather than a bytestream.

Now we just need to run some Python code and you get all the mouse control I could jam into the thing (two buttons, joystick and accelerometer gave me just enough room for everything a standard modern day mouse has).

#!/usr/bin/env python
import serial, sys, Xlib, Xlib.display, Xlib.ext.xtest

# This will be later on, setting it now
global d, root

# The port to which your Arduino board is connected
# Change for your system and board
port = '/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_64131383331351306291-if00'

# The baudrate of the Arduino program
baudrate = 19200

# Variables indicating the center position (no movement) of the controller
# Increase the amount of play if to sensitive, decrease if not sensitive enough
# Increase speed to cut it down, decrease to raise it (1 is the highest)
midAccelX   = 515 # Accelerometer X
midAccelY   = 590 # Accelerometer Y
midAccelZ   = 710 # Accelerometer Z (not used)
midAnalogY  = 134 # Analog Y
midAnalogX  = 127 # Analog X
tollerance  = 5   # Amount of play
accelToll   = 80  # Amout of accelerometer play
speed       = 4   # difference / speed = movement

# Generic mouse movement
def move_mouse(x,y):
    x = int((x - midAnalogX) / speed)
    y = int((midAnalogY - y) / speed)

    # Make sure it's not just white noise
    if abs(x) < tollerance:
        x = 0
    # Move in the right direction the right amount
    else:
        if x < 0:
            x += tollerance
        else:
            x -= tollerance

    # Same deal, filtering white noise
    if abs(y) < tollerance:
        y = 0
    # Move in the right direction the right amount
    else:
        if y < 0:
            y += tollerance
        else:
            y -= tollerance

    # Save the Xorg processing if we're not moving
    if x != 0 or y != 0:
        d.warp_pointer(x, y, src_window = root)
        d.flush()

# Simulated button down
def mouse_down(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonPress, button)
    d.sync()

#simulated button up
def mouse_up(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonRelease, button)
    d.sync()

def main():
    # Connect to the serial port
    try:
        ser = serial.Serial(port, baudrate)
    except Exception as err:
        sys.stderr.write(str(err) + "\n")
        return 0

    # Mouse buttons
    leftDown = False
    rightDown = False
    middleDown = False

    # While the serial port is open
    while ser.isOpen():
        # Read and process the line
        line = ser.readline()
        line = line.strip('\r\n')
        line = line.split(' ')

        # Not sane yet, nothing to see here
        if len(line) != 7:
            continue

        # Alias the info so we can make sense of the rest
        try:
            analogX = int(line[0])
            analogY = int(line[1])
            accelX  = int(line[2])
            accelY  = int(line[3])
            accelZ  = int(line[4])
            zButton = int(line[5])
            cButton = int(line[6])

        # It hiccuped, ignore and move on
        except ValueError:
            continue

        # Left Mouse Button (Z)
        # Button down, don't need to press again if it's already down
        if zButton and not leftDown:
            leftDown = True
            mouse_down(Xlib.X.Button1)
        # Release the button
        elif leftDown and not zButton:
            leftDown = False
            mouse_up(Xlib.X.Button1)

        # Gestures to map more buttons since we only have two
        # It's the C button
        if cButton:
            # Tilt right, right click
            if (not rightDown) and accelX - midAccelX > accelToll:
                rightDown = True
                mouse_down(Xlib.X.Button3)
            elif rightDown and accelX - midAccelX <= accelToll:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            # Tilt left, middle click
            if (not middleDown) and midAccelX - accelX > accelToll:
                middleDown = True
                mouse_down(Xlib.X.Button2)
            elif rightDown and accelX - midAccelX <= accelToll:
                middleDown = False
                mouse_up(Xlib.X.Button2)

            # Tilt forward, scroll up
            if accelY - midAccelY > accelToll:
                mouse_down(Xlib.X.Button4)
                mouse_up(Xlib.X.Button4)
            # Tilt back, scroll down
            elif midAccelY - accelY > accelToll:
                mouse_down(Xlib.X.Button5)
                mouse_up(Xlib.X.Button5)
        # Cleanup any buttons down when gesture stops
        else:
            if rightDown:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            if middleDown:
                middleDown = False
                mouse_up(Xlib.X.Button2)

        # Move the mouse
        move_mouse(analogX, analogY)

    # After the program is over, close the serial port connection
    ser.close()

if __name__ == '__main__':
    # Create display and get the root
    d = Xlib.display.Display(None)
    root = d.screen().root
    try:
        main()

    except KeyboardInterrupt:
        print ""

    except Exception as err:
        sys.stderr.write(str(err) + "\n")

    # Regardless of what happens, close the display
    finally:
        d.close()

Remember to change your port for your board and system and adjust any values to adjust to your sensitivity and any excess give or lack there of that it may have.