Automatically decrypting password protected HDFC or ICICI credit card bills

These banks send credit card statements password protected by a combination of the card number, your name and date of birth. If you need to organize these on your PC, needing to remember the password for them can be a chore, especially if you have accounts at both banks. The pdfs they send out also dont have a meaningful name.

The following script automatically detects ICICI and HDFC bills, decrypts the files with the proper passwords, and renames them to a meaningful name. To use it, the only prerequisite is perl and qpdf. perl comes builtin for linux. For Windows, install Activestate or Strawberry perl, and qpdf for Windows.

You can also import a key to registry so that you can right click on the folder and decrypt all files within it.

Code:

#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use Data::Dumper qw(Dumper);
use feature 'say';

my $dirtoproc = '';
if (@ARGV) {
        # print Dumper \@ARGV;
        $dirtoproc = $ARGV[0];
} else {
        $dirtoproc = ".";
}
chdir $dirtoproc;
use Cwd;
my $PwICICI = 'youricicipwd';
my $PwHDFC = 'yourhdfcpwd';

opendir(DIR, ".");
my @files = grep(//,readdir(DIR));
if (@files) {
        foreach my $file (@files) {
                # say "Processing..".$file;
                my ($basename, $parentdir, $extension) = fileparse($file, qr/\.[^.]*$/);
                if($file =~ m/.*_unlocked/) {
                        # If already has name _unlocked, dont process
                        say $file. ' was already processed earlier.';
                } else {
                        if($file =~ m/.*\d+-\d+-\d+.*/) {
                                say "Found HDFC bill: $file";
                                my $cmd = 'qpdf -decrypt --password='.$PwHDFC.' "'.$file.'" "'. 'HDFC_'.$basename.'_unlocked.pdf"';
                                my $output = `$cmd`;
                                print "$file >> ".$basename."_unlocked.pdf\n";
                        } elsif ($file =~ m/.*\sXXXX\sXXXX\s.*/) {
                                say "Found ICICI bill: $file";
                                my $cmd = 'qpdf -decrypt --password='.$PwICICI.' "'.$file.'" "'. 'ICICI_'.$basename.'_unlocked.pdf"';
                                my $output = `$cmd`;
                                print "$file >> ".$basename."_unlocked.pdf\n";
                        } elsif ($basename =~ m/\d+-\d+/) {
                                print "Matched IOB!";
                                if ($file =~ m/IOB.*/) {
                                                say "..was already processed";
                                } else {
                                        use File::Copy;
                                        copy($file,"IOB_".$basename.'.pdf');
                                        print "$file >> IOB_$basename.pdf\n";
                                }
                        }
                }


        }
}

Registry key: import.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Decrypt known PDFs\Command]
@="C:\\perlscripts\\decryptpdf\\runperl.cmd \"%1\""

And the file runperl.cmd:

C:\Strawberry\perl\bin\perl.exe C:\perlscripts\decryptpdf\decrypt.pl %1
pause

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

How to access a specific manpage given as command(n)

In Linux, manpages are a huge source of information on usage of a particular command. Often while reading manpages, you come across a reference to a particular section, like crontab(5). The 5 refers to the specific page, and can be accessed as:

man 5 crontab

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Create a relational database

First, the database and table which exists:

USE dbname;
select * from Authentication;
+----+---------+--------------------+---------------+----------------------+
| id | userid  | password           | role          | email                |
+----+---------+--------------------+---------------+----------------------+
|  7 | user1   | ljkjkjhjhjdhfZn0mz | administrator | [email protected]       |
| 10 | user2   | gdgdgdgdgdg^4YU    | user          | [email protected]       |
| 11 | user3   | dsdsdsdsdsd8       | user          | [email protected]       |
+----+---------+--------------------+---------------+----------------------+

Now create the other table and its fields:

CREATE TABLE `Login` (
  `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
  `TimeLoggedIn` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `id` (`id`),
  KEY `id_2` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

Now duplicate the field id in Login and populate it with existing data from table Authentication.

INSERT INTO `Login` (`id`) SELECT `id` FROM `Authentication`;

Now create relation:

ALTER TABLE `Authentication` ADD FOREIGN KEY (`id`) REFERENCES `Login`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Cloning or duplicating a database in mysql

First dump the database:

mysqldump -u root -p DBNAME > db1

Enter mysql and create the target database:

mysql> CREATE DATABASE 'episodescopy';

Then import it into a new database:

mysql -u root -p episodescopy < db1

Alternate solution:
Create target database:

mysql> CREATE DATABASE 'episodescopy';

Now copy using a utility:

mysqldbcopy --source=root:[email protected] --destination=root:[email protected] episodes episodescopy

It requires installation of mysqldbcopy:

apt install mysql-utilities

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Execute root commands via PHP

Solution using a binary wrapper (with suid bit)

1) Create a script (preferrably .sh) that contains what you want to be ran as root.

# cat > php_shell.sh <<CONTENT
  #!/bin/sh
  /sbin/service sshd restart
CONTENT

2) This file should be owned by root, and since it will later run with root permissions make sure that only root has permission to write to the file.

chown root php_shell.sh
chmod u=rwx,go=xr php_shell.sh

3) To run the script as root no matter what user that executes it, we will need a binary wrapper. Create one that will execute our php_shell.sh.

# cat > wrapper.c <<CONTENT
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>

  int
  main (int argc, char *argv[])
  {
     setuid (0);

     /* WARNING: Only use an absolute path to the script to execute,
      *          a malicious user might fool the binary and execute
      *          arbitary commands if not.
      * */

     system ("/bin/sh /path/to/php_shell.sh");

     return 0;
   }
CONTENT

4) Compile and set proper permissions, including the suid bit (saying that it should run with root privileges):

gcc wrapper.c -o php_root
chown root php_root
chmod u=rwx,go=xr,+s php_root

php_root will now run with root permissions, and execute the commands specified in php_root.sh.
If you don’t need to the option to easily change what commands that will be executed I’d recommend you to write the commands directly in wrapper.c under step 4. Then you don’t need to have a binary executing a external script executing the commands in question.

In wrapper.c, use system (“your shell command here”); to specify what commands you’d like to execute.

From: https://stackoverflow.com/questions/8532304/execute-root-commands-via-php


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

mysql-search and replace value in a particular column

Change value of coloumn meta_value, replacing yes with no:

UPDATE `booksdb_8`.`wop_postmeta` SET `meta_value`=REPLACE(`meta_value`, 'yes', 'no');

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

How to compile the latest development version of smplayer on Ubuntu?

Updated on 10 July 2017
Batch script to compile and install latest development version of smplayer:

#!/bin/bash
#Install dependencies
sudo apt build-dep smplayer
sudo apt-get install libqt4-dev zlib1g-dev fakeroot build-essential devscripts debhelper g++ mplayer2
echo Checking out sources
mkdir ~/src
cd ~/src
echo Removing old debs...
rm *deb
svn co https://subversion.assembla.com/svn/smplayer/smplayer/trunk/ smplayer
svn co https://subversion.assembla.com/svn/smplayer/smplayer-skins/trunk/ smplayer-skins
svn co https://subversion.assembla.com/svn/smplayer/smplayer-themes/trunk/ smplayer-themes
echo Compiling everything...
cd smplayer
./create_deb.sh
cd ..
cd smplayer-skins
./create_deb.sh
cd ..
cd smplayer-themes
./create_deb.sh
cd ..
echo Installing everything as root
sudo dpkg -i smplayer*deb

Updated on 2 May, 2017
Compile the latest smplayer on Ubuntu(New):
We’ll be building smplayer, smplayer-themes, and smplayer-skins.
Install dependencies:

sudo apt build-dep smplayer
sudo apt-get install libqt4-dev zlib1g-dev fakeroot build-essential devscripts debhelper g++ mplayer2

Get source from the svn and build each package:

mkdir ~/src
cd ~/src
svn co https://subversion.assembla.com/svn/smplayer/smplayer/trunk/ smplayer
cd smplayer
./create_deb.sh
svn co https://subversion.assembla.com/svn/smplayer/smplayer-skins/trunk/ smplayer-skins
cd smplayer-skins
./create_deb.sh
cd ..
svn co https://subversion.assembla.com/svn/smplayer/smplayer-themes/trunk/ smplayer-themes
cd smplayer-themes
./create_deb.sh
cd ..
sudo dpkg -i smplayer*deb

Hurray, we’ve successfully built and installed the latest dev version of smplayer.

To update to latest version:
smplayer will notify new update releases. Once updated, we can compile new one:

svn update
./create_deb.sh
sudo dpkg -i ../smplayer_17.5.0_amd64.deb

Note: I found that on playing, smplayer did not have any sound. I had to change my default audio device to pulse.
Options:Preferences:
Audio>Output driver: pulse

Older post:
First, download the latest smplayer tarball from the official site.

Eg:

wget http://downloads.sourceforge.net/project/smplayer/SMPlayer/16.9.0/smplayer-16.9.0.tar.bz2

Extract it:

tar jxf smplayer*bz2
cd smplayer*

Install essential prerequisites for building:

sudo apt-get install libqt4-dev zlib1g-dev fakeroot build-essential devscripts debhelper g++ mplayer2
sudo apt-get -f install

Now create the .deb package:

./create_deb.sh

Install the package:

sudo dpkg -i ../smplayer_16.9.0_amd64.deb

Alternately download the .deb after installing the prerequisites:

http://sourceforge.net/projects/smplayer/files/Unstable/ubuntu/

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Add dropbox daemon sync service at boot on debian

Create a file /etc/init.d/dropbox:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          dropbox
# Required-Start:    $local_fs $remote_fs $network mysql
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts Dropbox Daemon
# Description:       starts Dropbox Daemon
### END INIT INFO

# Change the value of "user" to linux user name who runs seafile
user=root

# Change the value of "seafile_dir" to your path of seafile installation
# usually the home directory of $user
dropbox_dir=/root/.dropbox-dist
script_path=${dropbox_dir}


#
# Write a polite log message with date and time
#


case "$1" in
        start)
                echo Starting Dropbox daemon
                sudo -u ${user} ${script_path}/dropboxd &
        ;;
        restart)
                echo Stopping Dropbox daemon
                sudo -u ${user} killall dropbox
                echo Restarting Dropbox daemon
                sudo -u ${user} ${script_path}/dropboxd &
        ;;
        stop)
                echo Stopping Dropbox daemon
                sudo -u ${user} killall dropbox
        ;;
        *)
                echo "Usage: /etc/init.d/dropbox {start|stop|restart}"
                exit 1
        ;;
esac

Give permissions:

chmod +x /etc/init.d/dropbox

Add to boottime:

cd /etc/rc3.d
ln -s ../init.d/dropbox S95{dropbox}

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Restart seafile

Login shell as the user owning seafile dir.
Eg:

sudo -iu myseafileusr 

Goto install dir

cd /home/myseafileusr/domains/cloud.mbc.com/public_html

Restart services:

./seafile.sh stop
./seahub.sh stop
./seafile.sh start
./seahub.sh start-fastcgi

Logs are at /home/myseafileusr/domains/cloud.mbc.com/public_html/logs:

/home/myseafileusr/domains/cloud.mbc.com/public_html/logs/
├── seafile.init.log
└── seahub.init.log

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

sqlite3 basic reference

C:\sqlite>sqlite3 store.db
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.

Remove a table:

sqlite> DROP TABLE punchlog;

Create a table:

sqlite> CREATE TABLE punchlog(_id INTEGER PRIMARY KEY, date TEXT, punchin TEXT, punchout TEXT, onduty INTEGER, forgotin INTEGER, forgotout INTEGER, latein INTEGER, lateout INTEGER);

List tables:

sqlite> .tables
punchlog

Schema of a table:

sqlite> .schema punchlog
CREATE TABLE punchlog(_id INTEGER PRIMARY KEY, date TEXT, punchin TEXT, punchout TEXT, onduty INTEGER, forgotin INTEGER, forgotout INTEGER, latein INTEGER, lateout INTEGER);

More details on table:

sqlite> PRAGMA TABLE_INFO(punchlog);
0|_id|INTEGER|0||1
1|date|TEXT|0||0
2|punchin|TEXT|0||0
3|punchout|TEXT|0||0
4|onduty|INTEGER|0||0
5|forgotin|INTEGER|0||0
6|forgotout|INTEGER|0||0
7|latein|INTEGER|0||0
8|lateout|INTEGER|0||0
sqlite>

Read data:

SELECT <cols> FROM <table>

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.