ip: 3.15.231.69 DKs blog - Python

DK's Blog

Python

Python examples

How to list biggest files in directory (including sub directorys)

import os
from pathlib import Path
import sys

########################################################################
#Classes
class filek:
    path = ''
    size = 0
    
    def __init__(self, _path, _size):
        self.path = _path
        self.size = _size

########################################################################
# Main 
########################################################################
lista = []
b = len(sys.argv)

if b != 2 and b != 3:
    print ("directory must be specified, min file size is optional")
    print ("findLargestFiles . 1024")
    print ("findLargestFiles /var")
    exit()

#if not defined set min length to 1 byte
if b == 2:
    limit = 1
else:
    limit = int(sys.argv[2])

if limit<1: limit = 1

#create list with all files 
if int(limit) > 0:
    import os 
    for path, dirs, files in os.walk(sys.argv[1]): 
        for f in files: 
            size=os.path.getsize( os.path.join( path, f ))
            if size>limit:
                lista.append(filek(path + f, size))
                #print (path, f, size)

lista.sort(key=lambda x: x.size, reverse=True)
for x in lista:
    print ("%10d %s" % (x.size, x.path))

 


How to search IIS log for SQL injection attempts

import os

LogFile = "f:\\Logs\\W3SVC44\\u_ex201227_x.log" #file to be parsed
pattern = set(['SELECT%','%27%20OR%201%3d','UPDATEXML','EXTRACTVALUE','GTID_SUBSET','%20and%20']) #string for search
saveDirtyLog = "output.txt" #set up file for CERT report
saveIPs = "IPs.txt" #list of IP that are flagged for CERT report

toFile=1 #0-print to console, 1-print to file

fields = {} #column definition
fieldsLoaded = False  #are fields defined
IpList = set([]) #IP list
useProxy = 0 #is reversed proxy used (X-Forwarded-For instead od c-ip)
Lines = [] # line of log text
#tranform all search string to lower
for i in pattern:
    i = i.lower()

#open file for parsing
with open(LogFile,'r',encoding='utf8',errors='ignore') as f:
    Lines = f.readlines() 

count = 0
# Strips the newline character 
for line in Lines: 
    #header
    if (line.startswith("#Fields:")):
        brojac = -1
        for l in line.split(' '):
            if (l != "#Fields:"):
                l = l.lower().strip()
                fields[l] = brojac
                if (l == "x-forwarded-for"):
                    useProxy = 1
            brojac += 1
        fieldsLoaded = True
        continue

    if (line.startswith("#")):
        continue

    if (fieldsLoaded):
        #Log parsing
        s = line.split(' ')
        #print ("cs-uri-query: ")
        #print (s[fields["cs-uri-query"]])
        try:
            q = s[fields["cs-uri-query"]].lower()
        except:
            continue
        #print ("q: ",q,", column: ", fields["cs-uri-query"])
        if (q == '-'):
            continue

        if any(p.lower() in q for p in pattern):
            #print ("q: ", q, ", stupac: ", fields["cs-uri-query"], ", BREAK")
            #ignore white listed IP address
            if (s[fields["c-ip"]].strip() != "1.1.1.1" ):
                if (useProxy == 1):
                    if (s[fields["x-forwarded-for"]].strip() != "1.1.1.1"):
                        IpList.add(s[fields["x-forwarded-for"]].strip())
                else:
                    IpList.add(s[fields["c-ip"]].strip())
            #break
        #if (line.find(pattern)!= -1):
    count += 1

# debug
# print ("\nfieldsLoaded: ", fieldsLoaded)
# print ("\nfields: " , fields)
# print ("\ncount: ", count)
# print ("\nuseProxy: " , useProxy)
# print ("\npattern: ", pattern)


if (toFile == 1):
#write IPs to file
    with open(saveIPs, 'w') as f:
        for line in sorted(IpList):
            f.write(line + "\n")  # set of numbers & a tuple

    if os.path.exists(saveDirtyLog):
        os.remove(saveDirtyLog)

    br = 0
    with open(saveDirtyLog, 'a') as app:
        #CERT report
        for line in Lines: 
            if (line.startswith("#")):
                if (br<6):
                    app.write (line)
                continue
            
            if IpList & set(line.split()):
                app.write(line)

            br += 1
else:
    print ("\nIPs:" , sorted(IpList) )
    print ("\nLog:")
    br=0
    for line in Lines:
        if (not line.startswith("#")):
            s = line.split(' ')
            q = s[fields["cs-uri-query"]].lower()
            if any(p.lower() in q for p in pattern):
            #if (q.find(pattern) != -1):
                print(line.strip())
                br+=1
    print ("\nnumber of lines matching searched strings: ", br)

 


 

Find biggest files in directory (and subdirectory) 

 

import os
from pathlib import Path
import sys

########################################################################
# Classes
class filek:
    path = ''
    size = 0
    
    def __init__(self, _path, _size):
        self.path = _path
        self.size = _size

########################################################################
# Main
########################################################################
lista = []
b = len(sys.argv)

if b != 2 and b != 3:
    print ("Directory is mandatory, min file size is optional)")
    print ("findLargestFiles . 1024")
    print ("findLargestFiles .")
    exit()

if b == 2:
    limit = 1
else:
    limit = int(sys.argv[2])

if limit<1: limit = 1

if int(limit) > 0:
    for path, dirs, files in os.walk(sys.argv[1]): 
        for f in files: 
            size=os.path.getsize(os.path.join( path, f ))
            if size>limit:
                lista.append(filek(path + f, size))
                #print (path, f, size)

lista.sort(key=lambda x: x.size, reverse=True)
for x in lista:
    print ("%10d %s" % (x.size, x.path))

 


Check IP for domain list (if there is DNS A record set for domain)

 

import os
import sys
import dns.resolver
from netaddr import *
from termcolor import colored


lista = [
'damir.globaldizajn.hr',
'google.com',
'gmail.com'
]


IP = ""

for x in lista:
    try:
        a = dns.resolver.resolve(x, 'A')
        for r in a:
            IP = str(r)
        
        print (IP, colored(x,"green"))
    except:
        print ("000.000.000.000", x, "non existing domain")
        continue
    



 


Test domain list if they exist and return CODE 200

 

import os
import sys
import dns.resolver
from netaddr import *
from termcolor import colored
import requests

lista = [
'https://damir.globaldizajn.hr',
'httsp://google.com',
'https://gmail.com'
]

boja = "white"

for x in lista:
    try:
        r = requests.get(x)
        if str(r.history) == "[]":
            boja = "white"
        else:
            boja = "yellow"

        if r.status_code == 200:
            print (str(x) + " - " + colored(str(r.history),boja) + " * " + colored (str(r.status_code), "green" ))
        else:
            #print (str(x) + " - " + colored(str(r.history),boja) + " * " + colored (str(r.status_code),"red"))

    except:
        print (x, " ERROR " + str(sys.exc_info()[0]))
        continue

 


How read recursively directory a search only files with some extension like .txt

 

import glob

for filename in glob.iglob("./" + '**/*.txt', recursive=True):
	print(filename)

 


Filter all IPs from some text file and sort it

 

import re

#list of all IPs
lista = []

#open file in which are IPs all around
file1 = open('file.txt','r',encoding='utf8',errors='ignore')
Lines = file1.readlines()
for line in Lines:
	#find all IPs in one line of text in file
	x = re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", line, flags=re.IGNORECASE)
	for i in x:
		lista.append(i)

#remove duplicates
l = list(set(lista))
#sort it 
l.sort()
#display all
for i in l:
	print (i)
	

 


 

How to monitor UPS for you PC with python, this script can be executed every minute and if UPS looses grid power send email to alert someone

 

import psutil
import smtplib
from email.mime.text import MIMEText
     
def sendMail(onPower, percent):
    sender = 'UPS@example.com'
    receiver = 'receiver@example.com'
    msg = MIMEText("UPS status onPower: " + onPower + ", battery percent: " + percent + "\n" )
    msg['From'] = sender
    msg['To'] = receiver
    msg['Subject'] = 'UPS alert PC on battery power'
    s = smtplib.SMTP('localhost')
    s.sendmail(sender, [receiver], msg.as_string())
    s.quit()

battery = psutil.sensors_battery()
if battery.power_plugged == True:
    print("UPS is connected to grid")
else:
    print("UPS not connected to grid, send alert email")
    sendMail( str(battery.power_plugged), str(battery.percent))

 

 


How to execute shell command and get output

 

import subprocess

result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
print (str(result.stdout))
cmd = "ls -al | grep aaa"

ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = str(ps.communicate()[0]).replace("\\n","\n")

 

 


How to use match and regular expression

 

import re

z = re.findall("FROM:(<[^>]+>)", next_line, flags=re.IGNORECASE)
if z:
    From = z[0]

 


How to read long text file

 

with open('filename.txt','r',encoding='utf8',errors='ignore') as fRead:
    for line in fRead:
        print(line)


 


How to simulate contains string property

 

if str.find("substr") != -1:
    print("true")

or

if "substr" in str:
    print("true")

 


How to access MS SQL Server with python

 

import pyodbc

conn_str = 'Driver={SQL Server};SERVER=sqlServer;DATABASE=DBName;UID=login;PWD=password'
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()

sql = "SELECT * FROM myTable"
cursor.execute(sql)
records = cursor.fetchall()
for row in records:
  ID = row.ID
  UserName = row.UserName
  print("ID: " + str(ID) + ", user: " + UserName)

 

 

@2016