From APK to readable java source code in 3 easy steps

Android applications are packed inside a APK file, which is just a ZIP file containing among other things a compact Dalvik Executable (.dex) file.
First step is to extract the “classes.dex” file from the APK:

$ unzip program.apk classes.dex
Archive:  program.apk
  inflating: classes.dex

Now, we use the tool dex2jar to convert the classes.dex file to Java .class files:

$ bash dex2jar/ ./classes.dex
2 [main] INFO - dex2jar ./classes.dex -> ./classes.dex.dex2jar.jar

From here we obtain the file “classes.dex.dex2jar.jar”, now we can use the java decompiler JD-GUI to extract the source code:

$ ./jd-gui classes.dex.dex2jar.jar

Now just go to “File -> Save all sources” and it will generate the zip file “” containing all the decompiled Java source code :)

Posted in android, linux, security | Tagged , , , , , , , , , | 25 Comments

Edit image resources inside an android APK file

Today I had to change some image resources in a APK file, the process is easy once you know it, so I just post it here for future reference:

First use the tool “aapt” from the android SDK to list the resources:
$ANDROID/tools/aapt list file.apk

Once we locate the resources that we need to change, we use “remove” and “add” to replace them:

$ANDROID/tools/aapt remove file.apk res/drawable/file.png
$ANDROID/tools/aapt add file.apk res/drawable/file.png

Then we have to remove the old APK signature and replace it with a new one.
We will generate a fake self signed key to sign the APK:

$ openssl genrsa -out key.pem 1024
$ openssl req -new -key key.pem -out request.pem
$ openssl x509 -req -days 9999 -in request.pem -signkey key.pem -out certificate.pem
$ openssl pkcs8 -topk8 -outform DER -in key.pem -inform PEM -out key.pk8 -nocrypt

Remove the old signature from the APK:

for f in `$ANDROID/tools/aapt list file.apk |grep "META-INF"` ; do
$ANDROID/tools/aapt remove file.apk $f

And now we sign the APK, I use signapk.jar to do this:
$ java -jar signapk.jar certificate.pem key.pk8 file.apk file-signed.apk

That’s it, our APK is now ready to install… just remember to setup your android phone to allow installing applications from unknown sources :)

Posted in android, linux | Tagged , , , | 4 Comments

Algoritmo para generar claves WPA de las redes WLAN_XXXX y JAZZTEL_XXXX

El 15 de Diciembre de 2010 la página de SeguridadWireless publico un servicio que calculaba las claves WPA de los SSID WLAN_XXXX y JAZZTEL_XXX screenshot aqui. No publicaron el código, y al cabo de pocas horas la página dejó de funcionar. Hoy me entero via Twitter de que finalmente a.s.r ha publicado en forocoches el algoritmo para sacar las claves de estas redes de Telefonica y Jazztel. Neikokz ha puesto un generador en esta página, basado en el script original de a.s.r.

Aquí teneis una versión modificada del script de a.s.r, que escanea las redes disponibles en busca de los SSIDs afectados, e imprime las claves de las que tengamos disponibles a nuestro alrededor:

$ ./ wlan0
[] CalcWLAN-ng (original source by a.s.r, improved by pof)
Scanning wifi networks on interface wlan0
SSID: WLAN_D679  KEY: a36206418a9691112e29

Update: Añadido soporte para las redes WLAN_XXXX con BSSID 00:1F:A4 de los routers ZyXEL P660HW-B1A, basado en el algoritmo de neikokz.


Don’t be evil! 😉

Posted in linux, wireless | Tagged , , , , , , , | 21 Comments

Generic load balancer for multiple WAN links

So you have two or more ADSL lines and want to use them all?
… or maybe you’re stealing your neighbor’s wifi and you have more than one network available?
… or you have cloned your SIM card and want to use multiple 3G connections simultaneously?

You can easily setup your Linux box to route multiple connections using iproute2, no matter how many WAN links you have! I have made a script to automate the process, it also comes with an optional failover watchdog which will monitor all the WAN links and automatically disable those which fail, re-enabling them when the connection is back.

Configuration is simple -one column for each WAN link-, you just need to have a separate interface for each link. If you want to use only one physical interface I recommend using a different VLAN on each WAN link. Edit the script to configure it as follows:

# Specify each WAN link in a separate column, example:
# In this example we have 3 wan links (vlanXXX interfaces) attached to a single
# physical interface because we use a vlan-enabled switch between the balancer
# machine and the ADSL routers we want to balance. The weight parameter should
# be kept to a low integer, in this case the ADSL line connected to vlan101 and
# vlan102 is 4Mbps and the ADSL line connected to vlan100 is 8Mbps (twice fast)
# so the WEIGHT value in vlan100 is 2 because it is two times faster.
# WANIFACE=\"    vlan101         vlan100         vlan102\"
# WEIGHTS=\"     1               2               1\"
# quick formula to calculate the weight: (LINKSPEED/MINSPEED)*NUM_LINKS
# If you don't want to use vlans, you should then use a separate physical
# interface for each link. IP aliasing on the same interface is not supported.

The script will set up the default route to be a multipath route, this will balance routes over the multiple WAN links and cache them depending on the destination address (so often used sites will always be sent over the same link). The weight parameters can be adjusted for each link in case you don’t have the same speed on each.


Enjoy! 😉

Posted in linux | Tagged , , , , , , , , | 13 Comments

pof headquarters reloaded

This is just a quick post to inform of the recent changes I’ve done with the server during the Christmas holidays:

  • Switched from OVH RPS to Linode VPS.
  • Moved the physical location of the server from France, to United Kingdom (without any noticeable downtime, yay!).
  • Updated the WordPress theme, although the look & feel is quite similar to the previous one, this has been written as a child theme of the popular “twentyten” theme, so it now supports widgets and all the fancy wordpress features.
  • Changed the base distribution from Gentoo to Ubuntu Server
  • Changed the mail server from Qmail to Postfix.
  • Setup a new microblog, which is basically an automatic backup of my twitter account, so I can do searches and avoid loosing old tweets when I reach the API limit.
  • Registered a 3 letter domain and created, a public URL shortening service, which runs on this server too :) feel free to use it!
Posted in linux, personal, pofHQ | Tagged , , , , , , , , , , , , | 6 Comments

Best solution to fully backup KVM virtual machines

Today I found the “” script from Daniel Berteaud which is IMHO the best solution to fully backup a libvirt managed virtual machine. The perl script is very flexible and allows for various configurations depending on your VM setup and backup needs, it requires ubuntu packages “libsys-virt-perl” and “libxml-simple-perl” to run.

I use it to backup running virtual machines in this way: first, suspend the virtual machine (for a short while), then create a snapshot of the LVM used as storage by the virtual machine, then resume the virtual machine so it can continue running without almost no downtime (taking a LVM snapshot is very fast!), and after that dump the snapshot to a compressed image file on a remote server (backup storage). To accomplish this just run this command (for example from a cron job): --pre --vm=machine1 --backupdir=/mnt/remotenas/ --privatedir --compress --snapsize=5G --debug

If you use ubuntu, note that the script needs to be edited to adjust the location of lvcreate and lvremove (in /sbin instead of /usr/sbin).

Posted in linux | Tagged , , , , | 30 Comments

Linux on ONKYO BX407A4

On a recent trip to Japan I bought this UMPC, of course I was not happy with the default Japanese windows OS, and changed it to Ubuntu. Here are some quick notes I took during the process for me to remember if I need to reinstall in the future and hoping it can be useful to anyone else having this UMPC. As far as I know, this is the same hardware as UMID Mbook BZ / M2 or Sagem SPIGA, so instructions should be valid for these devices as well.

onkyo bx407a4

Continue reading

Posted in gadgets, linux | Tagged , , , | 5 Comments

Dingoo A320

Yesterday I got a Dingoo A320 as a birthday present 😀 (thanks iolanda!!!). Here are my notes of things i need to remember in the future…

To upgrade the firmware (I flashed official 1.20) just copy the firmware file (named a320.hxf) to the root folder of the dingoo internal memory, disconnect it from your computer and switch it off, then press and hold “down” and switch on your dingoo again. After the firwmare has been updated the a320.hfx file can be deleted from the dingoo.

To check the LCD type (to choose the correct kernel and dual-boot bootloader for Dingux) just go to system setup – about, and press up-right-down, up-right-down. My LCD module is ILI9331.

Posted in gadgets, linux | Tagged | 1 Comment

Mounting a VirtualBox dynamic size VDI Image

To work with a variable sized VDI file, you have to dump it to a dd file first (COPYDD); this will create a fixed size dump that then can be opened on a loop device. The variable sized file does not allow mounting as the fs driver needs to see the expected disk size.

So we proceed as follows:

~/.VirtualBox/VDI$ vditool COPYDD win.vdi dump
vditool    Copyright (c) 2008 Sun Microsystems, Inc.

Then we look where the partition starts, in my case this was a WinXP fat32 formatted filesystem:

~/.VirtualBox/VDI$ hexdump -C -v dump |head -n 30000 |grep -i dos
00007e00  eb 58 90 4d 53 44 4f 53  35 2e 30 00 02 10 24 00  |.X.MSDOS5.0...$.|
00008a00  eb 58 90 4d 53 44 4f 53  35 2e 30 00 02 10 24 00  |.X.MSDOS5.0...$.|

Then we mount it:

~/.VirtualBox/VDI$ sudo mount -o loop,offset=0x7e00,umask=000 -o ro dump /mnt/
Posted in linux | Tagged , | 3 Comments

random notes on setting up KVM + libvirt + virtio

This is a collection of notes I took while setting up a virtual machine host which has several guest virtual machines running on Ubuntu 8.10.

1) Create a logical volume to install the guest

$ sudo lvcreate -v -n phq_mail -L 80G vg0 /dev/md1
    Setting logging type to disk
    Finding volume group \"vg0\"
    Creating directory \"/etc/lvm/archive\"
    Archiving volume group \"vg0\" metadata (seqno 6).
    Creating logical volume phq_mail
    Creating volume group backup \"/etc/lvm/backup/vg0\" (seqno 7).
    Found volume group \"vg0\"
    Creating vg0-phq_mail
    Loading vg0-phq_mail table
    Resuming vg0-phq_mail (254:5)
    Clearing start of logical volume \"phq_mail\"
    Creating volume group backup \"/etc/lvm/backup/vg0\" (seqno 7).
  Logical volume \"phq_mail\" created

Remember you can view all your logical volumes using lvdisplay

2) Creating a network segment to separate the servers from the rest of the network (the clients will use routing through the host to access the server).

$ vim ~/pofhq-servers.xml
  <bridge name=\"virbr%d\" />
  <ip address=\"\" netmask=\"\">
      <range start=\"\" end=\"\" />
$ virsh net-define pofhq-servers.xml
Network pofhq-servers defined from pofhq-servers.xml
$ virsh net-create pofhq-servers.xml
Network pofhq-servers created from pofhq-servers.xml
$ virsh net-autostart pofhq-servers
Network pofhq-servers marked as autostarted
$ rm ~/pofhq-servers.xml

This will create the file pofhq-servers.xml into /etc/libvirt/qemu/networks and link it to autostart folder.

Optionally, if you don’t want to use the ‘default’ network segment, you can delete it:

$ virsh net-undefine default
$ virsh net-destroy default

This will automatically delete de default-network.xml file (and autostart symlink if present) on /etc/libvirt/qemu/networks.

3) Installing the guest operating system using virtio for best virtual machine network and disk performance

We will start by letting virt-install create the default VM template for us:

$ sudo virt-install -n phq_mail -r 1024 -f /dev/vg0/phq_mail -c ubuntu-server.iso --accelerate --vnc --noautoconsole -v
Starting install...
Creating domain...                                                 0 B 00:00
Domain installation still in progress.  You can reconnect to
the console to complete the installation process.

Right after this, we will stop the VM and edit it’s configuration manually:

$ virsh shutdown phq_mail
$ virsh dumpxml phq_mail > phq_mail.xml
$ virsh undefine phq_mail
$ virsh destroy phq_mail
$ vim phq_mail.xml

Make the following modifications:

  • Boot from CD:
    <boot dev='cdrom'/>
    <disk type='file' device='cdrom'>
      <source file='/home/pau/ubuntu-8.10-server-i386.iso'/>
      <target dev='hdc' bus='ide'/>
  • Use virtio for the hard disk:
    <disk type='block' device='disk'>
      <source dev='/dev/vg0/phq_mail'/>
      <target dev='vda' bus='virtio'/>
  • Use virtio for the network:
    <interface type='network'>
      <mac address='00:16:36:7a:7b:7c'/>
      <source network='pofhq-servers'/>
      <model type='virtio'/>

Save the file, and create again the virtual machine with the new config:

$ virsh define phq_mail.xml
Connecting to uri: qemu:///system
Domain phq_mail defined from phq_mail.xml
$ virsh create phq_mail.xml
Connecting to uri: qemu:///system
Domain phq_mail created from phq_mail.xml
$ virsh autostart phq_mail
Connecting to uri: qemu:///system
Domain phq_mail marked as autostarted

Install normally, and then change the boot option to ‘hd’ to boot from normal hard disk again when installation has been finished (if needed, use shutdown/undefine/destroy and define/create/autostart again after finishing the installation).

4) If the guest VM you have installed is Ubuntu, remember to install acpid, for the VM to shutdown cleanly.

5) Useful documentation:

Posted in linux | Tagged , , , | Leave a comment