Jump to content
Gabriel87

[python] Port Blocker

Recommended Posts

#!/usr/bin/env python
#Name: portblock.py
#Author: s3my0n
#Email: RuSH4ck3R[at]gmail[dot]com

from socket import *
import sys

def Main(ADDR, MAXRESP):

try:
server = socket(AF_INET, SOCK_STREAM)
server.bind(ADDR)
server.listen(5) # 5 is maximum connection at a time
except BaseException, e:
print '\n [-]%s' % e
sys.exit()

notdone = True
count = 0 #will increase by one on response
while notdone:
try:
print '\n[+]Wating for connection'
client, caddr = server.accept()
print '\n Connection from: ', caddr
client.send('\n\aAccess Denied on port: %d\n' % (port)) #beep xD
client.close()
count += 1
if count == MAXRESP:
break #if you close the server here, you will get an error
except KeyboardInterrupt:
print '\n [-]Aborted' #will only exit when data has been recieved
sys.exit()
except BaseException, e:
print '\n [-]%s' % e
sys.exit(1)
server.close()
sys.exit()

if __name__=='__main__':
if len(sys.argv) not in [3, 4]:
print '\n\t#############'
print '\t# By s3my0n #'
print '\t#############'
print '\n\tUsage: portblock.py [host IP] [port] [maximum responses]'
print '\t portblock.py [port] [maximum responses]'
print '\n\tDefault host IP address is localhost'
sys.exit()

if len(sys.argv) == 4:
host = sys.argv[1] #user host IP address
(p, m) = (2, 3) #just a workaround (hack ;-])

else:
host = '127.0.0.1' #default host IP address
(p, m) = (1, 2)

try:
port = int(sys.argv[p]) #port number
maxresp = int(sys.argv[m]) #maximum responses
except ValueError:
print '\n [-]Number needed for port and max responses'
sys.exit()

if port not in range(1, 65536):
print '\n [-]Invalid port number'
sys.exit()

addr = (host, port) #address for binding

Main(addr, maxresp) # initializing the server

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...