Category Archives: Python

IT Security Brothers (http://itsb.pro)

Hi guys, I would like to present you our new project IT Security Brothers http://itsb.pro
We provide pentest, consultations and IT outsourcing services.
Feel free to hire us for IT jobs.

Dmesg with human-readable timestamp

Default dmesg timestamp format:
[ 680.870878] r8169 0000:02:00.0: em1: link up
[ 680.870889] IPv6: ADDRCONF (NETDEV_CHANGE): em1: link becomes ready
[ 695.002667] tun: Universal TUN/TAP device driver, 1.6

On some systems dmesg has option -T
for example fedora 17:
[Вт. дек. 11 11:52:14 2012] r8169 0000:02:00.0: em1: link up
[Вт. дек. 11 11:52:14 2012] IPv6: ADDRCONF (NETDEV_CHANGE): em1: link becomes ready
[Вт. дек. 11 11:52:29 2012] tun: Universal TUN/TAP device driver, 1.6

for other situations there are scripts on perl and python:
solutions on the perl:
Continue reading Dmesg with human-readable timestamp

Python: convert to bits and to bytes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def tobits(s):
    result = []
    for c in s:
        bits = bin(ord(c))[2:]
        bits = '00000000'[len(bits):] + bits
        result.extend([int(b) for b in bits])
    return result


def frombits(bits):
    chars = []
    for b in range(len(bits) / 8):
        byte = bits[b*8:(b+1)*8]
        chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
    return ''.join(chars)

Python: get information from AD (ldap protocol)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python

import ldap, sys

#example.msk
srv_example = 'ldap://172.16.0.12'
user_example = 'brownie'
pass_example = 'Eir2xooP'
dn_example = 'dc=example,dc=msk'

filter = '(&(objectClass=user)(samaccountname='+sys.argv[1]+'))'

conn = ldap.initialize(srv_example)
conn.protocol_version=3
conn.set_option(ldap.OPT_REFERRALS,0)
try:
        conn.bind_s(user_example, pass_example)
except ldap.INVALID_CREDENTIALS:
        print "username or password is incorrect."
        sys.exit()

res_id = conn.search_s(dn_example, ldap.SCOPE_SUBTREE, filter,['mail'])
for entry in res_id:
        print entry[1]
conn.unbind()

softswitch: Essentra BAX

Generating strings for import in Essetra BAX .

edit passgen.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
import random

class PassGen():
        def generator(self):
                passwd=""
                for i in xrange(0,11):
                        if (i%2==0):
                                passwd+=random.choice('abcdefghjkmnprstuvwxyzABCDEFGHJKMNPRSTUVWXYZ')
                        else:
                                passwd+=str(random.randint(2, 9))
                return passwd

        def getPassword(self):
                for i in xrange(0,1):
                        return self.generator()

edit bax.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
import random
from passgen import PassGen

class AddToBax():
        pswd = PassGen()
        prefix = 73522;
        def AddSingleNumberGen(self):
                print "Enter the person surname:"
                surname = raw_input()
                print "Enter the person name:"
                name = raw_input()
                print "Enter the address: "
                address = raw_input()
                print "Enter telephon number(6 digits format: 64xxxx):"
                telnumber = raw_input()
                password = self.pswd.getPassword()
                fullnumber = str(self.prefix)+str(telnumber)
                print "U,%s,COSUser,User,%s,test@kss45.ru,810,7,8,3522,%s,,,,%s,,%s,%s,E,D,E,D,,,%s,%s,User\n" % (fullnumber, password, telnumber, address, name, surname, fullnumber, password)

        def AddGroupNumberGen(self):
                print "Enter the company name:"
                company = raw_input()
                print "Enter the address:"
                address = raw_input()
                print "Enter the first number (6 digits format: 64xxxx): "
                first = raw_input()
                print "Enter the last number (6 digits format: 64xxxx): "
                last = raw_input()
                totalnumbers = int(last) - int(first) + 1
                while (first != int(last) + 1):
                        fullnumber = str(self.prefix)+str(first)
                        password = self.pswd.getPassword()
                        print "I,%s,COSUser,User,%s,test@kss45.ru,810,7,8,3522,%s,,,,%s,,,%s,E,D,E,D,,,%s,%s,User" % (fullnumber, password, first, address, company, fullnumber, password)
                        first = int(first) + 1

newuser = AddToBax()
print "Select Single(1) or Group(2) number generation"
selection = raw_input()
if selection == "2":
        newuser.AddGroupNumberGen()
else:
        newuser.AddSingleNumberGen()

That's all, press number 2 to generate strings for mass import of telephon number.
Press another key to generate string for one phone number.

bash-completion

bash-complition amazing package using which you can through TAB to see / select the option that provides the software.

In gentoo, he posed as follows:
emerge-q gentoo-bashcomp

But the script automates the inclusion of the features in all applications that support it:

1
2
3
4
5
6
7
8
#!/usr/bin/python
import os,re
os.system("eselect bashcomp list > /tmp/eselect")
line = open('/tmp/eselect','r').readlines()[-1]
regexp = re.search('[0-9].{2}',line)
for i in xrange(1,int(regexp.group(0))):
    os.system("eselect bashcomp enable --global "+str(i))
print "that's all"

PS: bash-completion is a complement not only for the bash-shell, but also for zsh and other