Inline csv to list loader (not recommended) - Python

This is a nice trick that once again shows the power of Python. But I do not recommend to use this solution in a regular programming or at least check for any errors while opening a file.

Example 1 -
csv file (test1.csv):

line 1 field 1,line 1 field 2,line 1 field 3,line 1 field 4
line 2 field 1,line 2 field 2,line 2 field 3,line 2 field 4
line 3 field 1,line 3 field 2,line 3 field 3,line 3 field 4

loading as a list

import re

csv = [ re.split(",",l) for l in re.split("\n",open("test1.csv").read()) ]
print csv

result

[
['line 1 field 1', 'line 1 field 2', 'line 1 field 3', 'line 1 field 4'],
['line 2 field 1', 'line 2 field 2', 'line 2 field 3', 'line 2 field 4'],
['line 3 field 1', 'line 3 field 2', 'line 3 field 3', 'line 3 field 4']
]



Example 2 -
csv file (test2.csv):

"line 1,field 1","line 1,field 2","line 1,field 3","line 1,field 4"
"line 2,field 1","line 2,field 2","line 2,field 3","line 2,field 4"
"line 3,field 1","line 3,field 2","line 3,field 3","line 3,field 4"

loading as a list:

import re

csv = [ re.findall('"[^"]+"',l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result

[
['"line 1,field 1"', '"line 1,field 2"', '"line 1,field 3"', '"line 1,field 4"'],
['"line 2,field 1"', '"line 2,field 2"', '"line 2,field 3"', '"line 2,field 4"'],
['"line 3,field 1"', '"line 3,field 2"', '"line 3,field 3"', '"line 3,field 4"']
]


Example 3
and instruction that will handle all two files:

import re

csv = [ re.findall('"[^"]+"',l) if re.match("\"",l) else re.split(",",l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result are the same like in example 1 or 2.

Any questions and suggestions are welcome.

Video - Arkanoid in 11 or 34 lines of code.

Promotion video. See previous post for more details.
Music: Talking Droids.
Watch in HD!

Python - Arkanoid in 11 or 34 lines of code.

And another classic game written in Python, this time Arkanoid (Breakout).Of course code is highly optimized for size and only standard modules are used.

Standard version: 34 lines
Short version (same code): 11 lines

Download: arkanoid3411l.tar.bz2

digitalinsane.com is back

New look, new/old like style - digitalinsane.com. Now, it's some kind of my project portfolio rather than regular blog. Also, old links to my javascript games has been changed, previous are still available but it is only redirections to the new ones.

New links:
Pacman
Solitaire
Space-Invaders
Puzzle now it's working!
Yetris

Enjoy!

VIDEO - SNAKE game in 26/15 lines of PYTHON code

Promotion video. See previous post for more details.
Watch in HD!

Python - Snake game in 15 or 26 lines of code.

This time fully functional Snake game written in Python 2.6.5 (linux / text-mode). The code is highly optimized for size / only standard modules are used.

Standard version: 26 lines
Short version (same code): 15 lines

Download: snake2615l.tar.bz2



VIDEO - Tetris in 50/28 lines of PYTHON code

Promotion video for my Python Tetris game (super short code).Enjoy
Watch in HD!

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:

The Thinkvantage Power Manager COM object.

If you have IBM/Lenovo Thinkpad Laptop with Thinkvantage Power Manager installed you can use the PWRMGR object to check the battery status, charging progress, remaining time and also change the screen brightness.
This is undocumented method, probably you can't make any damage on your hardware, however use it at your own risk.
These examples were tested on Thinkpad T60 with windows 7.

First, let's get the list of avaliable objects:

gwmi win32_classiccomclasssetting | ? { $_.InprocServer32 -like '*PWRMGR*' } | select VersionIndependentProgId

We will work with PWRMGR.Slider and PWRMGR.Battery.
Next, let's create a new instances and check avaliable methods and properties:

$slider = new-object -com PWRMGR.Slider
$battery = new-object -com PWRMGR.Battery

$slider | gm
$battery | gm



and now the cool part, unplug the power cable from your laptop and execute following commands :

$battery.Update()
$battery.ChargeStatus
$battery.RemainingPercentage
$battery.RemainingTime


as you can see we have an informaiton about the remaining percentage and time (which are the same as on the taskbar), Plug back the power cable and run these commands:

$battery.Update()
$battery.ChargeStatus
$battery.RemainingPercentage
$battery.ChargeTime


this time we've got the charging time, also the ChargeStatus was changed, you can use this function to detect if your laptop is on battery or not.
It's important to execute Update() method to get the current values before you check any property.

And the last cool trick is changing the screen brightness, this time we'll use PerformBrightnessSimulation from PWRMGR.Slider object.

$slider.PerformBrightnessSimulation(0)
$slider.PerformBrightnessSimulation(10)
.. (step by 10)
$slider.PerformBrightnessSimulation(90)
$slider.PerformBrightnessSimulation(100)


Of course I didn't exhaust the subject, these was only few things that I found exploring that object.

Note:
If you prefer Python as your scripting language you can use pywin32 to work with the COM objects.

import win32com.client
battery = win32com.client.Dispatch("PWRMGR.Battery")
battery.Update()
print(battery.RemainingPercentage)

Powershell, Enabling/Disabling Network Card, two methods.

Yet another powershell script from my collection. This time it's a connection of two different methods of changing the state of the network card. I read on few forums that people are looking for a script that could disable or enable the network card, however some of them are using windows xp but the others Vista and higher. The problem is that on Vista and 7 we can use WMI to make this job quickly and clean but on Windows XP it's not that simple, we need to use the com object to do that.
The following script checks the operating system version and decides which method must be used.
If you are using windows xp, you will need to change highlighted variables depends on your os-language.

At the end of this post I've put few lines of code that will help you determine the values for variables.

Check values for your operating system (these depends on the system language):

$shell = New-object -com Shell.Application
(($shell.NameSpace(3).Items() | ? { $_.Path -like '*7007ACC7-3202-11D1-AAD2-00805FC1270E*'}).GetFolder.Items() | ? { $_.Name -eq 'change to your interface Name' }).Verbs() | Select Name

External MessageBox

I wrote External MessageBox about year ago for use in a batch scripts to control execution flow and communicate with a user.
The utility is very simple, as the name suggests it shows the dialog box with specified caption, message, icon and one or several buttons. If you look at the source code you'll see that it just a call of the system MessageBox function (more about it here).
One of the cool feature is that you can pin the MessageBox to another running application, and block it until the user click one of the buttons.
Here is an example:
  1. Run windows calculator (calc.exe),
  2. Open cmd shell and change directory to where you unzip the emsgbox.exe,
  3. run following command:

emsgbox.exe /n calc.exe /c Calculator Info /m 2+2=5 /t 48

The calulcator will be blocked until you click OK button.
The button number which user has clicked is placed in %errorlevel% environment variable (or $lasterrorcode - powershell), however this method works only if you write a batch script or in the cmd shell when you use emsgbox.exe as follow:

start /wait emsgbox.exe /n calc.exe /c Calculator Info /m 2+2=5 /t 48
echo %errorlevel%


(as I said if you use it in batch script you don't have to use "start /wait" statement)
Link to the project/download page:

Powershell, problem with the comma character.

I'm using powershell a lot, and I want to use it like a normal shell environment, but I can't.

Well, I give you example, try this in powershell:

nmap -sS -p80,22 -PN -n 192.168.1.1

The comma character is the reason. It is a some kind of special character in powershell so you can't use it like that. In that case lets try the same command with ps escape character (backward apostrophe (`))

nmap -sS -p80`,22 -PN -n 192.168.1.1

still not working, but the error is different.

So how to fix this?

Solution number one (add space between -p and 80):

nmap -sS -p 80`,22 -PN -n 192.168.1.1

or solution number two (specify a protocol):

nmap -sS -pT:80`,22 -PN -n 192.168.1.1

What is my external IP?

Sometimes, when I'm connected to the local network I have to check what is my external IP address. If I'm on an unknown network and have no access to the router or the server configuration I need external IP to check who is the ISP or what services are avaliable behind the NAT.
There are two ways to get the external IP. First, is to check the IP configuration on the NAT server or router, but this works only if:
  • the router is directly connected to the internet, not to the another router on the other lan network,
  • the static IP address is configured,
  • you have an access to the configuration

Second, and most easiest way is to open your browser, go to your favorite search engine, search for 'what is my ip' and click the first result from the top. If you are lucky, that address is your external IP. If you for some reasons have no luck, check the links at the end of this post.
The other thing could be the command line instruction that will return the IP address, so we can use it for other purposes, as a part of some other scripts. One of possible way is to write a script that fetch the website and get the IP address from its content. Most of the WHAT-IS-MY-IP pages are not script friendly since they are regular web pages full of html tags and it is hard to dermine where the IP address is placed and even if you find it you can never be sure that it is correct (you may find not just one). Anyway, if you want to search through the content of the page,
I recommend you to use Regular Expression:

(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})

Specially for this post, I searched for those websites that shows only IP addresses, no html tags and other content.
Here is the list:
http://www.whatismyip.org/
http://www.whatismyip.com/automation/n09230945.asp
http://icanhazip.com/
http://whatismyip.akamai.com/
http://b10m.swal.org/ip
http://b10m.swal.org/cgi-bin/whatsmyip.cgi?just-ip

And the script that has been ported to a different platforms:
Powershell:

(New-Object System.Net.WebClient).DownloadString('http://whatismyip.org')

Python 3:
import urllib.request
page = urllib.request.urlopen('http://whatismyip.org')
ip = page.read().decode("utf8");
page.close()
print (ip)


Python 2:
import urllib
ip = urllib.urlopen('http://whatismyip.org').read()
print ip


Linux (bash) using wget:
#!/bin/sh
wget -qO - http://www.whatismyip.com/automation/n09230945.asp
echo

Scanning for available WLAN networks with PowerShell

Everyone interested knows how great tool netsh is if it comes about network configuration on Windows, especially when you are command line geek. Personally, I am often using it for setting ip addresses or managing Windows firewall. Starting from Windows Vista Microsoft has changed few and add new contexts giving you more control over network configuration. One of my favorite is "wlan". If you use Windows as your Wireless connection manager and, as I mentioned you are command line person the netsh wlan context is just for you. Not every wlan settings are changeable, but those that are available are enough to control wireless configuration through command line.
Few days ago I wrote a script that transform the result from 'netsh wlan show networks' command into something that looks familiar to airodump-ng scan, I think table form is more readable.

Here is the script (oh, I forgot to mention, this is PowerShell script):

Usage example:

Nping - port redirection detection.

Yesterday I got an email from insecure.org about the beta version (5.30) of nmap. For
those who use nmap I recommend to read the changelog, they fix and change few things and add new interesting scripts. Also, from now on Nping will be the part od nmap distribution (like ncat).

Nping is a "ping" like tool but on steroids (something simillar to hping2), it has a lot of powerfull features that can expand host discovery techiques in many different ways. Like nmap ping option with packet trace, using Nping you can check the response TTL values, and more or less determine if there is a port redirection on a remote router (Read my previous posts about finding port redirecition with nmap)

Here is a simple example:
nping --tcp -flags syn -p3389,22,1723 -c 1 -H [x.x.x.x]
(where x.x.x.x is an IP address)

--tcp -flags syn tcp protocol, only syn flag will be set
-p ports to check
-c 1 this is for reducing count of packets (for more information check Nping man page)
-H show only response

One of the difference that I found between the nmap and nping is that nping is a little bit slower (nmap ~0,4sec / nping ~3sec), currently I don't know why this happening and how can I (as a user) speed it up. Maybe it's a parameter issue.

How to clear security eventlog with the SYSTEM account.

After you clear the security log, one log entry is created (Event ID 1120) with the information who did this (account name, domain name) and when.
If you, for some reasons, want to hide that information you can clear the security log (or other event logs) with the SYSTEM account. This account is on every Windows operating system, so it is hard to guess who really cleared that log.
Let's do it.
First of all, you need two applications psexec.exe and psloglist.exe, you can get them from systinternals.com. Next, open your command prompt, and change directory to where you have downloaded these files. Type the following command:

psexec -accepteula -s -c psloglist.exe -accepteula -c system -n 1

psexec switches:
-s Run remote process in the System account.
-c Copy the specified program to the remote system for execution. (psloglist.exe in our case)

psloglist switches:
-c Clear the event log after displaying,
-n 1 Show only most recent entry (you can omit this if you want to see all events)

-accepteula automatic license agreement (if you run sysinternals tools for the first time you have to accept the license, this switch prevents from waitng for user response)
The really cool thing about this trick is to use it on remote machine:

psexec \\[host] -u [user] -p [password] -accepteula -s -c psloglist.exe -accepteula -c security -n 1

Of course you must have suitable privileges to clear the security log (in most cases you have to be an administrator).

Nmap - port redirection detection.

Not perfect! port redirection detection method, example:

nmap -sS -PN -n --max-parallelism 1 --packet-trace -p1723,3389 x.x.x.x
(where x.x.x.x is an IP address)

If SYN/ACK TTLs for both ports are different then sometimes it means that there is port redirection on a router. Also looking at TTL values you can identify the remote os (ex. TTL close to 128 it's probably MS Windows).

BTW. shorter version of --max-parallelism is -M. Maybe i'm wrong but -M is undocumented shortcut, I found it when I used --max-parallelism with 0 value, this caused error "Argument to -M must be at least 1!". :)

Welcome!

Hello and welcome to my new playground. It's been a while since I've made something ambitious, like for example my previous blog (digitalinsane.com). For about two year I've been working in small company as an ERP application developer. It's a fine job however I feel that I'm stuck. If someone will ask me "Do you like your job?", the answer will be "I have to.". Like every human being on the planet I need money to survive, and doing boring business things it's currently only way to survive, so I have to like my job. Anyway, I did few cool things in JavaScript couple of years ago and I thought I'll do this until I get completely bored of it, but my job has changed me. Now I'm focused on operating systems, networking, SQL servers, business intelligence applications, etc. As you can see these things are not related in what I've been doing in the past, so I had to change my interests.Also, I needed to find something that makes all these things less boring and free myself from feeling stuck. So, I've started interesting in computer security, pentesting, the bright and dark side, I've desired to be a ninja or Jedi (please stop laughing), and because of that I decided to create new blog instead of continuing digitalinsane.com, which was mostly about JavaScript frameworks.

I hope you enjoy this one.