SSH Infect

SSH Infect

infect_demo

A script to manage ssh public key install on servers

#!/bin/bash
#set -x

DESTHOST=$1
DB_PATH="$HOME/tmp"
DB_HOST_PREFIX='ssh_key'
packup_dir="${DB_PATH}/$(basename $0)_packup"
bin_dir="$HOME/bin"

which ssh-add >/dev/null 2>&1
SSH_ADD_PRESENT=$?

function list_infected()
{
    cd ${DB_PATH}/
    hosts=`ls ${DB_HOST_PREFIX}* 2>/dev/null`

    for host in $hosts; do
            echo "- ${host##${DB_HOST_PREFIX}_}"
    done
    cd - >/dev/null 2>&1
}

function clear_infected()
{
    cd ${DB_PATH}
    hosts=`ls ${DB_HOST_PREFIX}_$1 2>/dev/null`
    if [[ "$hosts" == "" ]]; then
        echo "No matching record found for host: $1"
        exit 1
    fi
    echo "#=================================================="
    echo "Following hosts will be cleared"
    echo "#=================================================="
    echo ${hosts##${DB_HOST_PREFIX}_}|tr " " "\n"
    echo "#=================================================="
    read -p "Confirm [y/n]: " ans
    if [[ "$ans" == "y" ]]; then
        key=$(awk '{print $NF}' ~/.ssh/id_rsa.pub)
        $(ssh $1 "sed -i \"/$key/d\" ~/.ssh/authorized_keys") && echo "Key removed"
        rm -f $hosts

        ssh_key_test $1
        if [[ "$?" != "0" ]]; then
            echo "Host $1 cleaned"
        fi
    fi
    cd - >/dev/null 2>&1
}

function packup()
{

    rm -rf $packup_dir*
    packup_dir=${packup_dir}_$(date +%F)
    mkdir -p ${packup_dir}/{$(basename ${DB_PATH}),$(basename ${bin_dir})}
    cp -rp ${DB_PATH}/${DB_HOST_PREFIX}* ${packup_dir}/$(basename ${DB_PATH})
    cp -rp $0 ${packup_dir}/$(basename ${bin_dir})
    cp -rp ~/.ssh ${packup_dir}
    tar -czf ${packup_dir}.tgz $packup_dir 2>/dev/null
    echo "Package: ${packup_dir}.tgz"
}

function startup()
{
    if [[ ! -d $DB_PATH ]]; then
        echo "DB path is not present"
        mkdir -p $DB_PATH
    fi

}

function ssh_key_gen()
{
    if [[ ! -f ~/.ssh/id_rsa ]]; then
        [[ $SSH_ADD_PRESENT -eq 0 ]] && ssh-add -D
        ssh-keygen -t rsa -N '' -q -f ~/.ssh/id_rsa
        [[ $SSH_ADD_PRESENT -eq 0 ]] && ssh-add
    fi
}

function ssh_key_test()
{
    ssh -oPasswordAuthentication=no $1 exit 0;
    return $?

}

function ssh_key_copy()
{
    SSH_KEY_COPY_CMD="umask 0022; mkdir -p ~/.ssh; chmod 700 ~/.ssh; cat >> ~/.ssh/authorized_keys"
    ssh_key_test $DESTHOST
    if [[ "$?" != "0" ]]; then
        $(cat ~/.ssh/id_rsa.pub |ssh $DESTHOST $SSH_KEY_COPY_CMD) \
                && touch $DB_PATH/${DB_HOST_PREFIX}_${DESTHOST}
    else
        touch $DB_PATH/${DB_HOST_PREFIX}_${DESTHOST}
    fi
}

#===============START===================#
startup

if [[ "$1" == "-l" ]]; then
    echo "#=================================================="
    echo "Infected hosts"
    echo "#=================================================="
    list_infected
elif [[ "$1" == "-c" ]]; then
    clear_infected $2
elif [[ "$1" == "-p" ]]; then
    echo "#=================================================="
    echo "Packing `basename $0` database"
    echo "#=================================================="
    packup
else
    ssh_key_gen
    ssh_key_copy
fi

The Fall of Icarus and The Flight of Phoenix

mug

Na karda gunahon ki bhi hasrat ki milay daad Ya rab agar in karda gunahon ki saza hai

-Mirza Ghalib

Klanti aamaar kshama karo prabhu, Pathe jodi pichhiye pari kabhu

Tagore

Ke se mor,  kei baa jaane
kichu taar dekhi aabha,  Kichu pai anumane
kichu taar bujhi naa baa.

-Tagore

O tor ashalata parbe chire, Hoito re phal phalbe na
Asbe pathe adhar neme, tai bolei ki roibi theme
O tui bare bare jalbi bati, Hoito bati jalbe na

-Tagore

A Weekend of Sept 2015 at Duliajan

DSC_0136

cropped-dsc_01921.jpg

First Impressions

DSC_0360-1

media-20151013-1

Deity

IMG_20151012_072354

Prayer and Practice 

dsc_0515

Vijaya Dashami 2016

my_booksBooks

Bitmap resource pool


Bitmap resource pool

class BitMapPool:

    def __init__(self, start, end, bitmap=[]):
        self.start = start
        self.end = end
        if bitmap == []:
            bitmap = ['0' for i in range(0, end)]
        self.bitmap = bitmap

    def allocate(self):

        for i in range(self.start, self.end):
            if self.bitmap[i] == '0':
                self.bitmap[i] = '1'
                return i
        return None

    def free(self, i):
        if self.bitmap[i] == '1' and i in range(self.start, self.end):
            self.bitmap[i] = '0'

    def free_pool_size(self):
        count = 0
        for i in range(self.start, self.end):
            if self.bitmap[i] == '0':
                count += 1
        return count

def main():

    pool = BitMapPool(200, 400)
    for i in range(1, 100):
        res = pool.allocate()
        print "Res: %s \nBitmap: %s" % (res, ''.join(pool.bitmap))

    print "Free pool size: %s" % pool.free_pool_size()

    for i in range(250, 300):
        pool.free(i)
        print "Freeing %s" % i

    print "Free pool size: %s" % pool.free_pool_size()

    for i in range(1, 40):
        res = pool.allocate()
        print "Res: %s \nBitmap: %s" % (res, ''.join(pool.bitmap))

    print "Free pool size: %s" % pool.free_pool_size()
    _bitmap = pool.bitmap
    pool = BitMapPool(200, 400, _bitmap)
    for i in range(1, 30):
        res = pool.allocate()
        print "Res: %s \nBitmap: %s" % (res, ''.join(pool.bitmap))

    print "Free pool size: %s" % pool.free_pool_size()

if __name__ == '__main__':
    main()

Run Length Encoding

from itertools import groupby
import json

def encode(input_string):
    return [[len(list(g)), k] for k, g in groupby(input_string)]

def decode(lst):
    return ''.join(c * n for n, c in lst)

_str = ('000000000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000000000000'
        '000000000000000000000000000000000000000000000000000000000000001111111'
        '111111111111111111111111111111111111111111111111111111111111111111111'
        '111111111111111111111111111111111111111110000000000000000000000000000'
        '0000000000000000000000000000000000000000000000000000000')

#worst case
#_str = '10'*200

_enc = encode(_str)
_dec = decode(_enc)

print "Encoded:", json.dumps(_enc), 'Length:', len(json.dumps(_enc))
print "Decoded:", _dec, 'Length:', len(_dec)

#worst case
_str = '10' * 200

_enc = encode(_str)
_dec = decode(_enc)

print "Worst case Encoded:", _enc, 'Length:', len(json.dumps(_enc))
print "Worst case Decoded:", _dec, 'Length:', len(_dec)

OUTPUT

Encoded: [[200, "0"], [117, "1"], [83, "0"]] Length: 35
Decoded: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000 Length: 400
Worst case Encoded: [[1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0'], [1, '1'], [1, '0']] Length: 4000
Worst case Decoded: 1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010 Length: 400

Single A4 paper Phone stand in 5 mins

Here is a quick and simple mobile stand made from a single A4 paper(you will need a stapler too) in 5 mins

Step1: Start with  an A4 size paper

IMG_20140731_122106_new

Step2: Fold half-way along length

IMG_20140731_122122_new

 

Step3: Repeat again to reduce to quarter width by folding along the length

 

IMG_20140731_122155_newIMG_20140731_122217_new

step4: Fold to make a triangle at one end

IMG_20140731_122234_new

Step5: Fold to make a smaller triangle at other end

IMG_20140731_122326_new

Step6: This is how the folded paper should look like at the end.

 

IMG_20140731_121026_newIMG_20140731_121054_new

Step7: Staple as shown in the picture

IMG_20140731_121129_newIMG_20140731_121040_new

Step8: The stand is ready

Some picture of the stand holding the phone.

DSC_0314.JPG_new DSC_0317.JPG_new DSC_0318.JPG_new

 

 

Classic Encryption: Vigenère

def gen_list(start):
        li = [chr(i) for i in range(65, 91)]
        return li[start:len(li)] + li[0:start]

def gen_arr():
        arr = []
        for i in range(0, 26):
                arr.append(gen_list(i))
        return arr

def print_arr():
        arr = gen_arr()
        for i in arr:
                print i

def gen_key_repeat(key, length):
        key_len = len(key)
        return key * (length/key_len) + key[:(length%key_len)]

def encrypt_char(in_char, key_char):
        arr = gen_arr()
        x = arr[0].index(in_char)
        for i in range(0, len(arr)):
                if arr[i][0] == key_char:
                        y = i
                        break
        return arr[y][x]

def decrypt_char(en_char, index, key_char):
        arr = gen_arr()
        for i in range(0, len(arr)):
                if arr[i][0] == key_char:
                        y = i
                        break
        x = arr[y].index(en_char)
        return arr[0][x]

def decrypt(input, key):
        key_repeat =  gen_key_repeat(key, len(input))
        arr = gen_arr()
        decrypted = []
        for i in range(0, len(input)):
                decrypted.append(decrypt_char(input[i], i, key_repeat[i]))
        return decrypted

def encrypt(input, key):
        key_repeat =  gen_key_repeat(key, len(input))
        arr = gen_arr()
        encrypted = []
        for i in range(0, len(input)):
                encrypted.append(encrypt_char(input[i], key_repeat[i]))
        return encrypted

key="KEYWORD"
input = raw_input("Input: ")
input = input.replace(" ", "").upper()

print_arr()

encrypted = "".join(encrypt(input, key))
print "Encrypted: ", encrypted

decrypted = "".join(decrypt(encrypted, key))
print "Decrypted: ", decrypted

vig

Classic Encryption: Four Square

Simple encryption based on classic four square


import random

chars = range(65,91)
chars.pop(16)

def make_array(input):
        arr = []
        li = []
        for i in range(0, len(input)):
                if i % 5 == 0 and i:
                        arr.append(li)
                        li = []
                li.append(chr(input[i]))
        arr.append(li)
        return arr

random.shuffle(chars)
b1 = make_array(chars)
random.shuffle(chars)
b2 = make_array(chars)
random.shuffle(chars)
b3 = make_array(chars)
random.shuffle(chars)
b4 = make_array(chars)

def tokenize(input):
        for i in range(0, len(input), 2):
                yield "%s%s" %(input[i], input[i+1])

def find(ch, block):
        for i in range(0, len(block)):
                for j in range(0, len(block[i])):
                        if ch == block[i][j]:
                                return i, j
def encrypt(input):
        first = find(input[0], b1)
        second = find(input[1], b4)
        return "%s%s" %(b2[first[0]][second[1]], b3[second[0]][first[1]])
        return first, second

def decrypt(input):
        first = find(input[0], b2)
        second = find(input[1], b3)
        return "%s%s" %(b1[first[0]][second[1]], b4[second[0]][first[1]])
        return first, second

def print_block():
        for i in range(0, len(b1)):
                print " ".join(b1[i]), "   ", " ".join(b2[i])

        print "\n"
        for i in range(0, len(b1)):
                print " ".join(b3[i]), "   ", " ".join(b4[i])

input = raw_input("Input: ")
input = input.upper()

if len(input) % 2 != 0:
        input = input[:-1]

print_block()

encrypted = "".join([encrypt(i) for i in tokenize(input)])
print "Encrypted: ", encrypted

decrypted = "".join([decrypt(i) for i in tokenize(encrypted)])
print "Decrypted: ", decrypted

fs3 fs2 fs1