Python - fun with list comprehensions

Jumping between dimensions or making lists from lists.
The power of the inline loops.

Suppose we have three-dimensional array:
d3 = [ [ [101,100], [99,98] ] , [ [97,96], [95,94] ] ]

Let's transform it into two-dimensional array
d2 = [ d3[a][b] for a in range(2) for b in range(len(d3)) ]
Result:
[[101, 100], [99, 98], [97, 96], [95, 94]]

and into one-dimensional array:
d1 = [ d2[b][a] for b in range(len(d2)) for a in range(2) ]
Result:
[101, 100, 99, 98, 97, 96, 95, 94]

We can also jump from 3d to 1d:
d1 = [ d3[a][b][c] for a in range(len(d3)) for b in range(2) for c in range(2) ]
Result:
[101, 100, 99, 98, 97, 96, 95, 94]

Now, let's do reverse transformation:
From 1d to 2d:
d2 = [ [ d1[a+b] for b in range(2) ] for a in range(0,len(d1),2) ]
Result:
[[101, 100], [99, 98], [97, 96], [95, 94]]

and from 2d to 3d:
d3 = [ [ d2[a+b] for b in range(2) ] for a in range(0,3,2) ]
Result:
[[[101, 100], [99, 98]], [[97, 96], [95, 94]]]

And of course we can do far-jump from 1d to 3d:
d3 = [ [ [ d1[b+a+c] for c in range(2) ] for b in range(0,3,2) ] for a in range(0,5,4)]
Result:
[[[101, 100], [99, 98]], [[97, 96], [95, 94]]]

More about list comprehensions:
list-comprehensions
wiki-list-comprehensions

Powershell - Identifying knowledge base article by id number.

This script may help you identify the Microsoft's installation file by getting the link and title of the knowledge base article that regard to that file. As the parameter you can use id number, file name or string with a number.



Example:

Python - Tetris in 28 or 50 lines of code.

Fully functional classic Tetris game written in Python 2.6.5 for Linux, text mode (curses module), only standard libraries.
The code is highly optimized for size, not for performance or speed, it looks ogly and you shouldn't write your programs like that, it's only the proof of concept.

Normal version: 50 lines
Squeezed code: 28 lines

Download: tetris2850l.tar.bz2




If you have an idea how to make it even more shorter, please let me know by posting comments here.

OpenWrt - iptables proxy configuration.

Simple script that redirects http traffic to a proxy server by using iptables.
Redirection only affects lan clients (not the router).
Tested on Kamikaze 8.09.2 configured in sta mode (as wireless client).
If it doesn't work on your OpenWrt version try to replace PREROUTING with prerouting_rule,POSROUTING with postrouting_rule and FORWARD with forwarding_rule.
Also change the LAN variable to your LAN interface - br-lan in my case.


#!/bin/sh

LAN=br-lan
LANIP=$(ifconfig $LAN | awk '/dr:/{gsub(/.*:/,"",$2);print $2}')
INTERNAL_NETWORK=$(echo $LANIP | sed 's/[0-9]*$//g')'0/24'
PROXYIP=$2
PROXYPORT=$3

ConfigProxyOn() {
  echo "HTTP proxy enabled - $PROXYIP:$PROXYPORT"
  iptables -t nat -A PREROUTING -i $LAN -s ! $PROXYIP -p tcp --dport 80 -j DNAT --to $PROXYIP:$PROXYPORT
  iptables -t nat -A POSTROUTING -o $LAN -s $INTERNAL_NETWORK -d $PROXYIP -j SNAT --to $LANIP
  iptables -A FORWARD -s $INTERNAL_NETWORK -d $PROXYIP -i $LAN -o $LAN -p tcp --dport $PROXYPORT -j ACCEPT
}

ConfigProxyOff() {
  iptables -t nat -D PREROUTING -i $LAN -s ! $PROXYIP -p tcp --dport 80 -j DNAT --to $PROXYIP:$PROXYPORT
  iptables -t nat -D POSTROUTING -o $LAN -s $INTERNAL_NETWORK -d $PROXYIP -j SNAT --to $LANIP
  iptables -D FORWARD -s $INTERNAL_NETWORK -d $PROXYIP -i $LAN -o $LAN -p tcp --dport $PROXYPORT -j ACCEPT
}

GetStatus() {
  iptables -t nat -L PREROUTING
  iptables -t nat -L POSTROUTING
  iptables -L FORWARD
}
case "$1" in
       on)
         ConfigProxyOn
       ;;
       off)
         ConfigProxyOff
       ;;
       status)
         GetStatus
       ;;
       *)
echo "Usage: "
echo " Turn on http proxy - proxy.sh on "
echo " Turn off http proxy - proxy.sh off "
echo " Current status - proxy.sh status"
echo
exit 1
esac

OpenWrt - upgrading Kamikaze to Backfire

My new video. Simple tutorial how to prepare trx image and upgrade the OpenWrt Kamikaze 8.09.2 to Backfire 10.03 on Linksys wrt54gl 1.1

Strange YOUTUBE error.

500 Internal Server Error

A team of highly trained monkeys has been dispatched to deal with this situation.

Also, please include the following information in your error report:

....

Microsoft teaches you how to exploit and secure your code.

Virtual Labs is a one of Microsoft's online services that gives you opportunity to work on some MS products by doing exercises on a specialy prepared virtual machines. Labs are divided into two sections, Technet - for those who's interested on operating systems, databases and similiar products and MSDN labs which are aimed to developers. Here's the full list

Microsoft Virtual Labs

One of the interesting section is the Security at the MSDN site. Currently there is about fifteen labs where you will see examples of buffer overflow, Cross-site scripting and sql injection vulnerabilities, and learn how to detect and fix them using tools avaliable in Visual Studio.


These labs are very short and simple, most of them can be done in few minutes but are worth to see especially if you work on Visual Studio.

Security Labs

Installing HDAPS on UBUNTU Lynx (10.04)

I did it on a clean UBUNTU Lynx installation by using tutorial founded on the thinkwiki.org.

This is the main how-to
http://www.thinkwiki.org/wiki/Installing_Ubuntu_9.10_(Karmic_Koala)_on_a_ThinkPad_T61#Install_HDAPS_-_IBM_Active_Protection_System_Linux_Driver

I think you should also install the following modules before starting the installation:

sudo apt-get install build-essential module-assistant

If you want to check if everything works correctly use hdaps-gl from hdaps-utils package. This is the simple opengl application that displays laptop position in real-time.

The package - hdaps-utils_0.1-1.1_i386.deb - works fine for my setup.

You can also use the Avant Window Navigator with the ThinkHDAPS applet.

To change the sensitivity edit the file (as the root user)

/etc/default/hdaps

and change the SENSITIVITY line. The higher value means less sensitivity.

To restart the hdapsd execute following command

sudo /etc/init.d/hdapsd restart

Screenshot from Synaptic Package Manager: