Monthly Archives: February 2015

fake DNS server

import socket

class DNSQuery:
def __init__(self, data):
self.data=data
self.dominio=''

tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
if tipo == 0: # Standard query
ini=12
lon=ord(data[ini])
while lon != 0:
self.dominio+=data[ini+1:ini+lon+1]+'.'
ini+=lon+1
lon=ord(data[ini])

def respuesta(self, ip):
packet=''
if self.dominio:
packet+=self.data[:2] + "\x81\x80"
packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00' # Questions and Answers Counts
packet+=self.data[12:] # Original Domain Name Question
packet+='\xc0\x0c' # Pointer to domain name
packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # Response type, ttl and resource data length -> 4 bytes
packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
return packet

if __name__ == '__main__':
ip='192.168.1.1'
print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip

udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))

try:
while 1:
data, addr = udps.recvfrom(1024)
p=DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print 'Respuesta: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
print 'Finalizando'
udps.close()

remove duplicated DNS zones from cPanel cluster servers

#!/bin/bash

# Find and remove duplicated DNS zones from cPanel cluster servers

cp -r /var/named /var/named_$(date +%F)
cp /etc/named.conf /etc/named.conf_$(date +%F)

> /tmp/all_zones
> /tmp/good
> /tmp/bad

find /var/named -maxdepth 1 -name '*.db' -exec basename {} .db \; > /tmp/all_zones

for z in $(cat /tmp/zonos.txt); do
grep -qE "DNS[0-9]*=$z" /var/cpanel/users/* && echo $z >> /tmp/good
done

grep -vwf /tmp/good /tmp/all_zones > /tmp/bad

wc -l /tmp/good /tmp/bad
wc -l /tmp/all_zones

for z in $(cat /tmp/bad); do
rm -f /var/named/$z.db
done

rm -f /var/named/cache/*
mv -f /etc/named.conf /tmp/
/scripts/rebuilddnsconfig

service named restart
sleep 5
service named status

sysinit script on boot

#!/bin/sh

DIR=/usr/local/bin/myservice
DAEMON=$DIR/myservice.py
DAEMON_NAME=myservice

DAEMON_OPTS=""
DAEMON_USER=root

PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions

do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}

case "$1" in

start|stop)
do_${1}
;;

restart|reload|force-reload)
do_stop
do_start
;;

status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;

esac
exit 0

threads in Python simple

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import threading
import time
import requests

"""
Checking http reposne every milisecond.
"""

class HttpChecking(object):

def __init__(self, interval=0.01):

self.interval = interval

thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()

def check_url(self):
try:
self.url = "http://www.linuxhow.tk"
r = requests.head(self.url)
print r.status_code
except requests.ConnectionError:
print "failed to connect"

def run(self):
while True:
print('Checking HTTP status...')
self.check_url()
time.sleep(self.interval)

chk = HttpChecking()
chk.run()

vim fold/unfold all

set foldmethod=indent

use zo and zc to fold and unfold in normal mode
use zR and zM to fold and unfold all folds

zo Open one fold under the cursor. When a count is given, that
many folds deep will be opened. In Visual mode one level of
folds is opened for all lines in the selected area.

zc
zc Close one fold under the cursor. When a count is given, that
many folds deep are closed. In Visual mode one level of folds
is closed for all lines in the selected area.
‘foldenable’ will be set.

Increase /tmp partition size in cPanel and secure it

/etc/init.d/cpanel stop
/etc/init.d/httpd stop
/etc/init.d/lsws stop
/etc/init.d/mysql stop

umount -l /tmp
umount -l /var/tmp
Move /usr/tmpDSK file to another location (just in case you’ll need to mount it somewhere else to preserve data):

mv /usr/tmpDSK /usr/tmpDSK_back
Modify /scripts/securetmp to set tmpdsksize to desired size:

vi /scripts/securetmp
$tmpdsksize = 2048000

/scripts/securetmp

Start cpanel, apache (litespeed), mysql services:
/etc/init.d/cpanel start
/etc/init.d/httpd start
/etc/init.d/lsws start
/etc/init.d/mysql start