Tag Archive - linux

capturing environment variables in python

capturing environment variables is very useful when one has to do some hacks..
this is a simple bash script which shows all the environment variables when caught from STDIN

1
2
3
4
5
#!/bin/bash

CAPTURE_FILE=/var/log/capture_data
env >> ${CAPTURE_FILE}
exit 1

Equivalent Python script:

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

import os
import sys

def capture():

    log = os.environ
    data = open("/tmp/capture.log", "a")
    for key in log.keys():
        data.write((key))
        data.write(" : ")
        for n in log[key]:
            data.write('%s' % ((n)))
        data.write("\n")
    data.close()
    sys.exit(1)

def main():

    capture()

if __name__ == "__main__":
    main()
 

python md5 or sha1 hash of files

Creating a copy of MD5 hash of all critical files on your system is a good idea as such you can track any changes in the files. Linux has a built in tool “md5sum” which does the job, so to create a md5 hash use the md5sum command

1
2
[root@server ~]# md5sum install.log
e04dda261d4f5fed7f2f40d4cd6b7705  install.log

the output can be stored as a database

1
[root@server ~]# md5sum install.log > database

and recheck the database using

1
2
[root@server ~]# md5sum -c database
install.log: OK

this is plain and cool, now we explore python’s way of creating an elegant md5 or sha1 hash utility, and this can be done using the built-in hashlib module. You need to have python > 2.4

Continue Reading…

 

encrypt / decrypt files in linux

These are the different ways you can encrypt file in linux, we will use openssl and gnupg’s file encryption and decryption process.

Symmetric encryption

as the name says symmetric encryption uses the same pair of key to encrypt and decrypt a file which is OK if you are not bothered too much about the security of the location of pass-phrase.

from the command prompt type

1
openssl des3 -salt -in file -pass file:pass -out file.encrypt

were file:pass is the location of the passphrase file or you can remove this option and enter the passphrase manually.

to decrypt the encrypted file, use

1
openssl des3 -d -salt -in file.encrypt -out file

if you have the gnupg program installed, you can encrypt the file using:
Continue Reading…

 

How does ls command work in linux?

You can use these two utilities in linux, strace to trace system calls and ftrace to trace the function,system calls which are called within the kernel.

to run a strace on ls,

1
strace -o output.log ls /etc

so,

1
execve("/bin/ls", ["ls"], [/* 21 vars */]) = 0

the system call execve() is evoked and it executes the command ls and based on the type of executable it will look for the program ld.so.* to load the shared libraries which are needed by the program.

http://www.kernel.org/doc/man-pages/online/pages/man2/execve.2.html

Continue Reading…

 

kindle for linux

the new version of kindle reader does not work on linux, so i found the beta version of kindle which works cool, here is the link..

http://d1xhj100piaj9u.cloudfront.net/25338/KindleForPC-installer.exe

http://www.krisindigitalage.com/KindleForPC-installer.exe

 

python: system monitoring script

This is a simple python system monitoring script using subprocess.call and os.popen function, since they use the internal unix commands to poll the server, if you want a portable script which would work across different OS then psutils is more suitable http://code.google.com/p/psutil/

But the advantage of this script is that it can calculate memory and cpu per process for apache,bind etc…

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python

from subprocess import Popen, PIPE, STDOUT
import subprocess
import os
import popen2

def memory():
    print "gathering memory info..."
    subprocess.call("free", shell=True)

def cpu():
    print "gathering system uptime info... "
    subprocess.call("uptime", shell=True)

def pci():
    print "gathering pci devices.."
    subprocess.call("lspci", shell=True)

def apache():
    print "calculating apache mem use.."
    sum = 0.0
    command = os.popen("ps aux | grep apache | awk '{print $4}'")
    for i in command.readlines():
    l = i.rstrip("\n")
    d = float(l)
    sum = d + sum
    print sum

def exim():
    print "calculating exim mem use..."
    esum = 0.0
    ecommand = os.popen("ps aux | grep -v grep | grep exim | awk '{print $4}'")
    for e in ecommand.readlines():
    f = e.rstrip("\n")
    g = float(f)
    esum = g + esum
    print esum

def spam():
    print "calculating spamd mem use.."
    ssum = 0.0
    spamd = os.popen("ps aux | grep spamd | awk '{print $4}'")
    for s in spamd.readlines():
    t = s.rstrip("\n")
    u = float(t)
    ssum = u + ssum
    print ssum

def log():
    print "logged in users..."
    subprocess.call("last| grep 'still'", shell=True)

def main():
    memory()
    cpu()
#   pci()
    apache()
    spam()
    exim()
    log()

main()
 

secure centralized log server

log archive script

#!/bin/sh

Today=`date +%Y%m%d`
fy=`date –date=’5 days ago’ +%Y`
fm=`date –date=’5 days ago’ +%m`
fd=`date –date=’5 days ago’ +%d`

echo “$Today”;
echo “$fd”;

server=`ls /var/log/HOSTS`;

for i in $server
do

mkdir -p /archive/$i/$fy/$fm/$fd
tar -zcvf /archive/$i/$fy/$fm/$fd/$fd.tar.gz /var/log/HOSTS/$i/$fy/$fm/$fd/

done

#!/bin/sh

Today=`date +%Y%m%d`
fy=`date –date=’5 days ago’ +%Y`
fm=`date –date=’5 days ago’ +%m`
fd=`date –date=’5 days ago’ +%d`

echo “$Today”;
echo “$fd”;

server=`ls /var/log/HOSTS`;

for i in $server
do

mkdir -p /archive/$i/$fy/$fm/$fd
tar -zcvf /archive/$i/$fy/$fm/$fd/$fd.tar.gz /var/log/HOSTS/$i/$fy/$fm/$fd/

done

 

slow down ping rates

You may want to keep the ability to reply to pings, but protect yourself from a form of attack known as a ‘ping flood’. So how can you manage such a feat? The easiest way is to slow down the rate at which the server replies to pings. Continue Reading…

 

stop replying to pings

While ping is a very useful command for discovering network topology, the disadvantage is that it does just that, and makes it easier for hackers on the network to target live servers. But you can tell Linux to ignore all pings – the server simply won’t respond. Continue Reading…

 

transferring files without ftp or scp

Need to transfer a directory to another server but do not have FTP or SCP access? Well this little trick will help out using the netcat utility. On the destination server run:

Continue Reading…

 
Page 1 of 212»
Theme Tweaker by Unreal