Python – Parse through an XML file generated by Nmap.

The script below uses two different modules. The ElementTree and the NmapParser. The former module is used to manipulate XML files. The latter module is used specifically to manipulate XML files generated by the Nmap port scanner. In both cases both modules will need to read the actual XML file. This script is better suited for an environment that has Linux and Windows servers.

The Nmap command used to generate the capture is

1
nmap -T4 -A -v 10.0.0.29 -oX file.xml

Sample of output

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
HostName:                desktop.sfentona.lol
Operating System Guess:  Linux 3.7 - 3.9 - Accuracy Detection 98
-----------------------------------------------------------------------------
Port: 22    State: open  Protocol: tcp Product: None            Version: None       ExtrInfo: protocol 2.0
Port: 80    State: open  Protocol: tcp Product: nginx           Version: 1.4.6      ExtrInfo: Ubuntu    
Port: 139   State: open  Protocol: tcp Product: Samba smbd      Version: 3.X        ExtrInfo: workgroup: DESKTOP
Port: 445   State: open  Protocol: tcp Product: Samba smbd      Version: 3.X        ExtrInfo: workgroup: DESKTOP



HostName:                    ad1.sfentona.lol
Operating System Guess:  Microsoft Windows 7 or Windows Server 2012 - Accuracy Detection 100
-----------------------------------------------------------------------------
Port: 53    State: open  Protocol: tcp Product: Microsoft DNS                       Version: None                 ExtrInfo: None      
Port: 88    State: open  Protocol: tcp Product: Windows 2003 Kerberos               Version: None                 ExtrInfo: server time: 2014-12-28 03:22:28Z
Port: 135   State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 139   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 389   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 445   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 464   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 593   State: open  Protocol: tcp Product: Microsoft Windows RPC over HTTP     Version: 1.0                  ExtrInfo: None      
Port: 636   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3268  State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3269  State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3389  State: open  Protocol: tcp Product: Microsoft Terminal Service          Version: None                 ExtrInfo: None      
Port: 49152 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49153 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49154 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49155 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49156 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49158 State: open  Protocol: tcp Product: Microsoft Windows RPC over HTTP     Version: 1.0                  ExtrInfo: None      
Port: 49159 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49167 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49175 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None

 
Continue reading

Share Button

Python – Scan network for a specific service and send a string via socket to obtain more information about the running service.

The script below uses the netaddr, nmap module and socket module. The netaddr module calculate the IP range you want to scan, the nmap module does the scan it self. The socket module will send a string of text in order to obtain more information regarding the service running.

The service being scanned is the SSH service. In this case it is hardcoded to scan that specific service.

import nmap
from netaddr import IPNetwork
import socket
from contextlib import contextmanager


nm = nmap.PortScanner()


for loop_1 in IPNetwork('10.0.0.2/26'):
#for loop_1 in IPNetwork('10.0.0.29'):
    (nm.scan(loop_1.format(), '22'))
    try:

       state = (nm[loop_1.format()]['tcp'][22]['state'])

       server_address = (loop_1.format(),22)
       sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       sckt.connect((server_address))
       sckt.send('I AM TEXT')
       reply = sckt.recv(100)
       sckt.shutdown
       sckt.close()

       if state == "open" and reply:

            data = [loop_1, state, reply]
            print ""
            print "Server-------SSH------Version"
            print data[0], "  ", data[1],"   ", data[2]
       else:
            print "Nooo"

    except (KeyError) as ip_not_reached:
        pass

    except (socket.error) as socket_connection_error:
        print ""
        print loop_1,socket_connection_error
Share Button

CISCO C3700 commands

All available commands for the C3700 CISCO router
Continue reading

Share Button

Python – Use threading to get all EOD quote prices from all NASDAQ symbols.

The script below goes to yahoo finance and takes the current price of all stocks from NASDAQ. There is really no real life application of this script. It was created to learn threading. The script obtains the prices for thousands of stocks with in a couple of seconds at most. The output at some sections is off due to the speed the results are being printed. In order for this to work you will have to download all the symbols for the NASDAQ exchange and save that info in a text file located in the same location the script is running from.

#!/usr/bin/python
import urllib
import re
import threading
from datetime import datetime

print "------------------------------------------"
print "Today's date: ", str(datetime.now())[0:10]
print "------------------------------------------"
print ""
print "Symbol    Price      Time"

def stock_prices_reader(smbl):
    url = "http://finance.yahoo.com/q?s=" + smbl + "&ql=0"
    #print url
    html_link = urllib.urlopen(url)
    html_read = html_link.read()
    regex = '(.+?)'
    pattern = re.compile(regex)
    price = re.findall(pattern, html_read)
    print (str(smbl).center(5)), (''.join(price).center(14)), str(datetime.now())[11:25]
    print (" ----".ljust(1)), ("-----".center(14)),("--------".rjust(5))

symbolsfile = open("symbols.txt")
symbolist = symbolsfile.read()
newsymbolist = symbolist.split(("\n"))

threadlist = []
for loop1 in newsymbolist:
    thrd = threading.Thread(target=stock_prices_reader, args=(loop1,))
    thrd.start()
    threadlist.append(thrd)

for loop2 in threadlist:
    loop2.join()

Share Button

Apache2 – Parameters, StartServers, MinSpareServers, etc

  • TimeOut Directive: Amount of time the server will wait for certain events before failing a request

The TimeOut directive defines the length of time Apache will wait for I/O in various circumstances:

  • When reading data from the client, the length of time to wait for a TCP packet to arrive if the read buffer is empty.
  • When writing data to the client, the length of time to wait for an acknowledgement of a packet if the send buffer is full.
  • In mod_cgi, the length of time to wait for output from a CGI script.
  • In mod_ext_filter, the length of time to wait for output from a filtering process.
  • In mod_proxy, the default timeout value if ProxyTimeout is not configured.
  • On virtualized servers like VPS servers, lowering this value to 100 can help improve performance.
  • KeepAlive: Enables HTTP persistent connections. High volume and/or load balanced servers should have this setting disabled (Off) to increase connection throughput.
  • MaxKeepAliveRequests: This setting works only if KeepAlive is enabled as well. This setting limits the number of requests allowed per persistent connection when KeepAlive is on. If it is set to 0, unlimited requests will be allowed.
  • KeepAliveTimeout: The number of seconds Apache will wait for another request before closing the connection. Setting this to a high value may cause performance problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.
  • StartServers: is the number of Apache (httpd) processes that will be created upon startup
  • MinSpareServers: Minimum number of Servers kept on IDLE
  • MaxSpareServers: Max number of Server kept on IDLE.

 

Share Button

Apache2 – Access based on IP – http password access

How to allow/deny access based on IP address. Also how to use the htpasswd file

1) Access based on IP – See lines 9 to 11


    Options Indexes FollowSymLinks
    AllowOverride AuthConfig
    AuthType Basic
    AuthName "Authentication Required"
    Require valid-user
    AuthUserFile "/var/www/html/.htpasswd"
    #Require all granted
    Order deny,allow
    Deny from all
    Allow from 10.0.0.29


2) – http password access – See lines 3 to 7
The .htpasswd file will need to be told which users it should accept. You do this issuing the following command:

1
htpasswd -c .htpasswd gmastrokostas

    Options Indexes FollowSymLinks
    AllowOverride AuthConfig
    AuthType Basic
    AuthName "Authentication Required"
    Require valid-user
    AuthUserFile "/var/www/html/.htpasswd"
    #Require all granted
    Order deny,allow
    Deny from all
    Allow from 10.0.0.29


Share Button

Python – Find RAM usage for highest HTTP process and calculate MAXClients setting.

The script examines the /proc directory and imports the numerical directories in a list and distinguishes which process is for HTTP. It will then calculate the amount of RAM it uses. This script actually can pickup other process as well, if told so and report the amount of RAM it consumes. The script will then calculate how much RAM it uses

#!/usr/bin/python
import os
import psutil
import subprocess
import time

'''
if os.exists("/etc/redhat-release"):
    process = "httpd"
else:
    process = "apache2"
'''

pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
http_proccess = []
for elements in pids:
    p = psutil.Process(elements)
    p.name()
    p.memory_info_ex()[0]
    if p.name() == 'apache2':
        #print p.name(), p.memory_info_ex()[0]
        http_proccess.append(p.memory_info()[0])
        process = p.name()
        apache_status = True
    elif p.name() == 'httpd':
        #print p.name(), p.memory_info_ex()[0]
        http_proccess.append(p.memory_info()[0])
        process = p.name()
        apache_status = True
    else:
        pass

print "--------------------------------------------------"
http_largest_hog = float((max(http_proccess) / 1000000.0))
print "Largest ", process, " process is ",  round(http_largest_hog, 10), "MBs"

free_memory_proc = float(psutil.virtual_memory().free) / 1000000.0
print "Free memory with ", process, " running",round(free_memory_proc,10), "MBs"
if apache_status == True:
    subprocess.call('service apache2 stop', shell=True )
    time.sleep(10)
    print "Processing...."
else:
    pass

free_memory_Noproc = float(psutil.virtual_memory().free) / 1000000.0
print "Free memory with out ", process, " running",round(free_memory_Noproc,10), "MBs"

ram_gained =  free_memory_proc - free_memory_Noproc
print "RAM Gained after ", process, "terminated: ",ram_gained, " MBs"

apache_maxClients = ram_gained / http_largest_hog
print "Adjust Apache MAXCLIENTS parameter at ", apache_maxClients
Share Button

Python – Subnet Calculator

This script asks the IP class and then for the number of hosts. It will then calculate how many network blocks can be created within that subnet, the borrowed bits and it produces the netmask for that subnet.

from array import array
from netaddr import *

def subnet_calculator():
    class_A = ('10.0.0.1')
    class_B = ('172.16.0.1')
    class_C = ('192.168.0.1')


    print "CLASS A = 10.0.0.1"
    print "CLASS B = 172.16.0.1"
    print "CLASS C = 192.168.0.1"
    print "---------------------"
    user_input_1 = raw_input("Select a Class: ")
    hostsPerSubnet = int(raw_input("How many Hosts per subnet: "))
    user_input_2 = raw_input(("Enter starting IP address: "))
    Intarr=array('f',[])

#Creating a power 2 table (Decimals = 511 - 1023 - 2047 - 4095)
    for loop_2 in range(36):
        totalHosts = 2**loop_2
        Intarr.append(totalHosts)
     #Comparing the number of hosts entered with the array elements above.
     #Once the first instance of comparison where totalHosts < Decimals, it will use that to calculate hosts.
     #Example: Num of hosts entered: 8000. So, 8000 is less than 8,191. 8,191 Will be used to calculate the hosts.
        if hostsPerSubnet < totalHosts:
            maskBits = loop_2
            numHostsADJ = totalHosts -2
            netBlocks = totalHosts/256
            borrowedbits = netBlocks - maskBits
            #Calculating the Netmask
            netmas_pre = ("%s / %s") % (user_input_2, borrowedbits)
            netmas_post = IPNetwork(netmas_pre)
            netmask_final = netmas_post.netmask


            print "Total Hosts Minus Broadcast and Network: %s" % numHostsADJ
            print "Total Hosts Included Broadcast and Network: %s" % totalHosts
            print "Mask Bits: %s" % maskBits
            print "Network Blocks: %s " % netBlocks
            print "Borrowed Network bits: %s " % borrowedbits
            print "Starting IP Address is: %s  " % user_input_2
            print "Netmask is: %s " % netmask_final
            exit()


subnet_calculator()

Sample of output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CLASS A = 10.0.0.1
CLASS B = 172.16.0.1
CLASS C = 192.168.0.1
---------------------
Select a Class: A
How many Hosts per subnet: 8000
Enter starting IP address: 10.0.0.1
Total Hosts Minus Broadcast and Network: 8190
Total Hosts Included Broadcast and Network: 8192
Mask Bits: 13
Network Blocks: 32
Borrowed Network bits: 19
Starting IP Address is: 10.0.0.1  
Netmask is: 255.255.224.0
Share Button

Python – A simple Multithreaded Chat script.

There are two scripts that compose this chat script. The server and the client script. The server script is the one that uses the socket API and as a result follows the traditional steps of communication as dictated by the socket API. The client script connects to the chat server and communication between the two starts.

Note: The threads in this script are not closed. Also, the script is inefficient with the thread creation. As a result communication is limited to the amount of threads being created, which is hardcoded. This was my very first script using sockets and multithreading. It is a learning process. Continue reading

Share Button

Python – A simple example using threading module

Example 1: Threading example which loops 10 times by also creating threads for the function. This script however, does not ensure that threads are being closed.

import threading
import time

def somefunction():
    for loop in range (10):
        print "thread sleeps for 10 seconds"
        time.sleep(10)
        print "thread woke up"

for counter in range(10):
     threader = threading.Thread(target=somefunction)
     threader.start()

Example 2: The script below is the same as the one above. However it employs threading accounting by storing each thread to a list. This allows to close threads when the process exits.

import threading
import time

def somefunction():
    for loop in range (10):
        print "thread sleeps for 20 seconds"
        time.sleep(20)
        print "thread woke up"

threads = []
num_threads = 10
for i in range(num_threads):
     # here you need to pass in "somefunction" without parens
     threads.append(threading.Thread(target=somefunction))
     threads[-1].start()

# then you need to join them (join waits for threads to finish)
for thread in threads:
    thread.join()
Share Button