-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathportscan.py
111 lines (88 loc) · 3.74 KB
/
portscan.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import requests
import ipcalc
import sys
import os
import threading
import time
def scan(eachTreadsIP,eachTreadsEndIP,threadNumber):
global finishFlag
initialPort=int(sys.argv[3])
#mulitple ports
if len(sys.argv)==5 :
endPort=int(sys.argv[4])
for x in range(eachTreadsIP,eachTreadsEndIP):
fileNameUnproccessed = "IP-Address-"+str(ipAddresses[x])+"-port-scan-between-"+str(initialPort)+"-and-"+str(endPort)+"-thread-number-"+str(threadNumber+1)
fileNameProccessed = fileNameUnproccessed.replace("/", "-")
fileNames.append(fileNameProccessed)
file = open(fileNameProccessed,"w")
for y in range(initialPort,endPort):
pload = {'port':y,'ip':ipAddresses[x],'t':'2000'}
r = requests.post('http://ports.my-addr.com/check_port.php',data = pload)
if r.text=='1':
file.write("port : "+str(y)+" is open on : " + str(ipAddresses[x])+"\n")
print("port : "+str(y)+" is open on : " + str(ipAddresses[x]))
else:
file.write("port : "+str(y)+" is closed on : " + str(ipAddresses[x])+"\n")
print("port : "+str(y)+" is closed on : " + str(ipAddresses[x]))
finishFlag=finishFlag+1
print(finishFlag)
#single port
else :
for x in range(eachTreadsIP,eachTreadsEndIP):
fileNameUnproccessed = "IP-Address-"+str(ipAddresses[x])+"-port-scan-for-"+str(initialPort)+"-thread-number-"+str(threadNumber+1)
fileNameProccessed = fileNameUnproccessed.replace("/", "-")
fileNames.append(fileNameProccessed)
file = open(fileNameProccessed,"w")
pload = {'port':initialPort,'ip':ipAddresses[x],'t':'2000'}
r = requests.post('http://ports.my-addr.com/check_port.php',data = pload)
if r.text=='1':
file.write("port : "+str(initialPort)+" is open on : " + str(ipAddresses[x])+"\n")
print("port : "+str(initialPort)+" is open on : " + str(ipAddresses[x]))
else:
file.write("port : "+str(initialPort)+" is closed on : " + str(ipAddresses[x])+"\n")
print("port : "+str(initialPort)+" is closed on : " + str(ipAddresses[x]))
finishFlag=finishFlag+1
fileNames= []
finishFlag = 0
whileChar = 1
ipAddresses = []
if len(sys.argv) > 3 :
if len(sys.argv)==5 :
fileNameFinalUnproccessed = str(sys.argv[1])+"-port-scan-between-"+str(int(sys.argv[3]))+"-and-"+str(int(sys.argv[4]))+"-Final"
fileNameFinalProccessed = fileNameFinalUnproccessed.replace("/", "-")
finalFile = open(fileNameFinalProccessed,"w")
else:
fileNameFinalUnproccessed = str(sys.argv[1])+"-port-scan-for-"+str(int(sys.argv[3]))+"-Final"
fileNameFinalProccessed = fileNameFinalUnproccessed.replace("/", "-")
finalFile = open(fileNameFinalProccessed,"w")
for x in ipcalc.Network(sys.argv[1]):
ipAddresses.append(x)
if len(ipAddresses)<int(sys.argv[2]):
print("The number of threads cannot exceed the number of IPs. Which is:"+str(len(ipAddresses)))
sys.exit()
eachThreadsIP=int(len(ipAddresses)/(int(sys.argv[2])))
lastThreadsExtraIP=(len(ipAddresses)-(int(sys.argv[2]))*(int(len(ipAddresses)/(int(sys.argv[2])))))
for i in range(int(sys.argv[2])+1):
if (len(ipAddresses))>=((eachThreadsIP*(i+1)+lastThreadsExtraIP+1)):
worker = threading.Thread(target=scan,args=((eachThreadsIP*i),(eachThreadsIP*(i+1)),i))
worker.start()
else :
worker = threading.Thread(target=scan,args=((eachThreadsIP*i),((eachThreadsIP*(i+1))+lastThreadsExtraIP),i))
worker.start()
break
while whileChar==1:
if finishFlag == (int(sys.argv[2])):
for k in fileNames:
file = open(k,'r')
lines = file.readlines()
count=0
for line in lines:
count += 1
finalFile.write(line.strip()+"\n")
os.remove(k)
whileChar=0
break
else:
print("Usage for single port: python3 portscan.py <IP Subnet> <Thread Number> <Port>")
print("Usage for port range: python3 portscan.py <IP Subnet> <Thread Number> <Starting Port> <End Port>")
sth