-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
179 lines (125 loc) · 6.2 KB
/
scanner.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
# ------------------------- [ Sub Project File | Coding: utf-8 ] -------------------------- #
# Project: EzNet #
# File: client.py #
# Python Version: 3.10.2 - Tested: 3.10.2 - All others are untested. #
# The libraries should get installed among the integrated libraries: Libraries #
# ----------------------------------------- [ ! ] ----------------------------------------- #
# This code doesn't have any errors. if you got an error, check syntax and python version. #
# ----------------------------------------- [ ! ] ----------------------------------------- #
# Author: nihadenes - <[email protected]> #
# Links: <https://github.com/nihadenes> #
# Date: Date #
# License: License #
# --------------------------------------- [ Enjoy ] --------------------------------------- #
# Casual importances.
import threading
import socket
import time
import uuid
import threading
from threading import Thread
import nmap
# Our side libraries for encryption, communication, packaging and unpacking.
from communication import *
from encryption import *
# Global our confşg messages so we can use them in our functions.
# ADDR: The address of the server.
# FORMAT: The format of the message. ("ascii", "utf-8", "utf-16", "utf-32"...etc)
# HEADER: The header of the message as we send our main message's length to make TCP more reliable.
global ADDR, FORMAT, HEADER
ADDR, FORMAT, HEADER = (socket.gethostbyname(socket.gethostname()), 9595), "utf-8", 64
# "send" function is for sending our messages to the server.
def send(client=None, packagetype=None, message=None, ipadress=None, key=None):
# Function to send data.
def send_data(client=None, message=None):
# Adding try to give an error code to the algorithm.
try:
# Sending the length of the message to make TCP more reliable.
send_length = str(len(message.encode(FORMAT))).encode(FORMAT)
# Completing header message to {HEADER} amount of bits.
send_length += b' ' * (HEADER - len(str(len(message.encode(FORMAT))).encode(FORMAT)))
# Send length of the message and the main message.
client.send(send_length)
client.send(message.encode(FORMAT))
except:
# If there is an error, raise an exception with a specified error code.
raise Exception(301)
# Function to receive data.
def recv_data(client=None):
# Adding try to give an error code to the algorithm.
try:
# Getting the legnth of the message. (Wanted to make the function of getting the header message to be more efficient)
msg_length = ""
# Get the message until it fully arrives.
while len(msg_length) != HEADER:
# Get the message part by part.
part = client.recv(HEADER - len(msg_length))
# Add the message part to the message length.
msg_length += part.decode(FORMAT)
# Convert the message length to an integer. (It automatically removes the spaces)
msg_length = int(msg_length)
# Get the main message.
receive = ""
# Get the message until it fully arrives.
while len(receive) != msg_length:
# Get the message part by part.
part = client.recv(msg_length - len(receive))
# Add the message part to the message.
receive += part.decode(FORMAT)
# Return the message.
return receive
except:
# If there is an error, raise an exception with a specified error code.
raise Exception(301)
# Function for sending our package.
def send_packet(client=client, packagetype=packagetype, message=message, ipadress=ipadress, key=key):
# Packaging the data.
message = getenc(packagetype=packagetype, message=message, ipadress=ipadress, key=key)
# Send our data.
send_data(client=client, message=message)
# Receive reply data from the server.
receive = recv_data(client=client)
# Unpackage the data.
receive = getdec(receive, key=key)
# Return the data.
return receive
# Return our package.
return send_packet()
# "client" function is for creating our client.
def client():
# Create a socket client and use TCP protocol.
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to our server.
client.connect(ADDR)
# Get the session hash key for peer-to-peer encrypted communication.
hash_key = (send(client=client, packagetype="client.getkey"))["message"]
# Just send random things for testing.
for i in range(10000):
send(client=client, packagetype="client.message", message=randomlet(512), key=hash_key)
def testsocket(ip, list):
ADDR = (ip, 9595)
print(ip)
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
list.append([ip, "true"])
except:
list.append([ip, "false"])
# Run the client forever, even if there's errors.
while True:
nm = nmap.PortScanner()
nm.scan(hosts=f'{socket.gethostbyname(socket.gethostname())}/24', arguments='-sP')
list = nm.all_hosts()
print(list)
results = []
threads = [Thread(target=testsocket, args=(i, results)) for i in list]
[i.start() for i in threads]
[i.join() for i in threads]
print(results)
for i in results:
ADDR = (i[0], 9595)
try:
client()
except Exception as e:
print(f"Client failed, [{e}], [{e.__class__}].")
time.sleep(5)