Skip to content

Commit 75f1e3d

Browse files
committed
fix: linting errors
1 parent 194106c commit 75f1e3d

File tree

2 files changed

+128
-19
lines changed

2 files changed

+128
-19
lines changed

netcat/netcat.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,26 @@ def __init__(self, args, buffer=None) -> None:
3535
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
3636

3737
def run(self):
38+
"""
39+
Entry point of the program, checks if the program is in listen mode
40+
"""
3841
if self.args.listen:
3942
self.listen()
4043
else:
4144
self.send()
4245

4346
def send(self):
47+
"""
48+
Establishes a connection to a target server and port,
49+
sends an initial buffer if provided, and
50+
facilitates bidirectional communication by receiving
51+
data and allowing user input to send messages
52+
in an interactive loop.
53+
54+
Raises:
55+
KeyboardInterrupt: Captures user termination to ensure cleanup and graceful exit.
56+
57+
"""
4458
self.socket.connect((self.args.target, self.args.port))
4559
if self.buffer:
4660
self.socket.send(self.buffer)
@@ -66,6 +80,10 @@ def send(self):
6680
sys.exit()
6781

6882
def listen(self):
83+
"""
84+
Listens for incoming connection if Program used
85+
in listen mode
86+
"""
6987
print("listening")
7088
self.socket.bind((self.args.target, self.args.port))
7189
self.socket.listen(5)
@@ -76,6 +94,10 @@ def listen(self):
7694
client_thread.start()
7795

7896
def handle(self, client_socket: socket.socket):
97+
"""
98+
Handler function for incoming types of requests
99+
which can be either command shell, upload, or execute
100+
"""
79101
if self.args.execute:
80102
output = execute(self.args.execute)
81103
client_socket.send(str(output).encode())
@@ -135,11 +157,11 @@ def handle(self, client_socket: socket.socket):
135157
parser.add_argument("-p", "--port", type=int, default=5555, help="specified port")
136158
parser.add_argument("-t", "--target", default="192.168.1.203", help="specified IP")
137159
parser.add_argument("-u", "--upload", help="upload file")
138-
args = parser.parse_args()
139-
if args.listen:
140-
buffer = ""
160+
input_args = parser.parse_args()
161+
if input_args.listen:
162+
BUFFER = ""
141163
else:
142-
buffer = sys.stdin.read()
164+
BUFFER = sys.stdin.read()
143165

144-
nc = NetCat(args, buffer.encode("utf-8"))
166+
nc = NetCat(input_args, BUFFER.encode("utf-8"))
145167
nc.run()

proxies/proxy.py

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,38 @@
99
)
1010

1111

12-
def hexdump(src: bytes | str, length=16, show=True) -> list | None:
12+
def hexdump(src: bytes | str, length=16, show=True) -> list:
13+
"""
14+
Generate a hex dump of a given input string or bytes object.
15+
16+
This function creates a hexadecimal representation of the input with
17+
a specified length of bytes per line. It also provides a printable
18+
representation of the content alongside the hex dump. Users can opt
19+
to display the output directly or return it as a list of strings.
20+
21+
Parameters:
22+
src: bytes | str
23+
The input data to be converted to a hex dump. Can be either
24+
a 'bytes' object or a string.
25+
length: int, optional
26+
The number of bytes to display per line in the hex dump.
27+
Defaults to 16.
28+
show: bool, optional
29+
Determines whether to print the hex dump directly. If set to
30+
False, the hex dump is returned as a list of strings. Defaults
31+
to True.
32+
33+
Returns:
34+
list
35+
Returns a list of strings representing the hex dump.
36+
37+
Raises:
38+
TypeError
39+
If `src` is not of type bytes or str.
40+
"""
1341
if isinstance(src, bytes):
1442
src = src.decode()
15-
results = list()
43+
results = []
1644
for i in range(0, len(src), length):
1745
word = str(src[i : i + length])
1846
printable = word.translate(HEX_FILTER)
@@ -22,11 +50,21 @@ def hexdump(src: bytes | str, length=16, show=True) -> list | None:
2250
if show:
2351
for line in results:
2452
print(line)
25-
else:
26-
return results
53+
return results
2754

2855

2956
def receive_from(connection: socket) -> bytes:
57+
"""
58+
Receives data from a socket connection until no more data is sent or an
59+
exception is raised. The method assembles all received chunks into a
60+
single bytes object.
61+
62+
Args:
63+
connection (socket): The socket connection to receive data from.
64+
65+
Returns:
66+
bytes: The complete data received from the socket connection.
67+
"""
3068
buffer = b""
3169
connection.settimeout(10)
3270
try:
@@ -44,11 +82,37 @@ def receive_from(connection: socket) -> bytes:
4482

4583

4684
def request_handler(buffer: bytes) -> bytes:
85+
"""
86+
Handles incoming binary packets, processes the data, and modifies the buffer
87+
before sending it back.
88+
89+
This function takes a binary data buffer as input, modifies or processes the
90+
information contained within, and returns the updated binary buffer as output.
91+
92+
Args:
93+
buffer: Binary data to be modified and processed.
94+
95+
Returns:
96+
Modified binary buffer after processing.
97+
"""
4798
# perform packet modifications
4899
return buffer
49100

50101

51102
def response_handler(buffer: bytes) -> bytes:
103+
"""
104+
Processes and modifies the response packet data.
105+
106+
The function takes raw packet data as input, applies any
107+
modifications required, and returns the modified packet data
108+
as output.
109+
110+
Args:
111+
buffer (bytes): The raw packet data to be processed.
112+
113+
Returns:
114+
bytes: The modified packet data.
115+
"""
52116
# perform packet modifications
53117
return buffer
54118

@@ -59,19 +123,41 @@ def proxy_handler(
59123
remote_port: int,
60124
receive_first: bool,
61125
) -> None:
126+
"""
127+
Handles the proxy connection by relaying data between a local client and a remote server.
128+
129+
This function establishes a connection with the remote host and optionally receives data first,
130+
then continuously transfers data between the local client and the remote server while applying
131+
custom request and response handlers. The function is designed for bidirectional data transfer
132+
until one or both connections are closed.
133+
134+
Parameters:
135+
client_socket: socket
136+
The socket representing the local client connection.
137+
remote_host: str
138+
The hostname or IP address of the remote server to connect to.
139+
remote_port: int
140+
The port number of the remote server.
141+
receive_first: bool
142+
A flag indicating whether to receive data from the remote server before starting
143+
the bidirectional data transfer.
144+
145+
Returns:
146+
None
147+
"""
62148
remote_socket = socket(AF_INET, SOCK_STREAM)
63149
remote_socket.connect((remote_host, remote_port))
64150

65151
if receive_first:
66152
remote_buffer = receive_from(remote_socket)
67153
if len(remote_buffer):
68-
print("[<==] Received %d bytes from remote." % len(remote_buffer))
154+
print(f"[<==] Received {len(remote_buffer)} bytes from remote.")
69155
hexdump(remote_buffer)
70156

71157
while True:
72158
local_buffer = receive_from(client_socket)
73159
if len(local_buffer):
74-
print("[<==] Received %d bytes from local." % len(local_buffer))
160+
print(f"[<==] Received {len(local_buffer)} bytes from local.")
75161
hexdump(local_buffer)
76162

77163
local_buffer = request_handler(local_buffer)
@@ -80,7 +166,7 @@ def proxy_handler(
80166

81167
remote_buffer = receive_from(remote_socket)
82168
if len(remote_buffer):
83-
print("[<==] Received %d bytes from remote." % len(remote_buffer))
169+
print(f"[<==] Received {len(remote_buffer)} bytes from remote.")
84170
hexdump(remote_buffer)
85171

86172
remote_buffer = response_handler(remote_buffer)
@@ -101,21 +187,24 @@ def server_loop(
101187
remote_port: int,
102188
receive_first: bool,
103189
):
190+
"""
191+
Main server loop, binds to the local host and port and starts the server
192+
"""
104193
server = socket(AF_INET, SOCK_STREAM)
105194
try:
106195
server.bind((local_host, local_port))
107196
except Exception as e:
108-
print("[!!] Failed to listen on %s:%d" % (local_host, local_port))
197+
print(f"[!!] Failed to listen on {local_host}:{local_port}")
109198
print("[!!] Check for other listening sockets or correct permissions.")
110199
print(e)
111200
exit(0)
112201

113-
print("[*] Listening on %s:%d" % (local_host, local_port))
202+
print(f"[*] Listening on {local_host}:{local_port}")
114203
server.listen(5)
115204
while True:
116205
client_socket, addr = server.accept()
117206
# print out the local connection information
118-
print("> Received incoming connection from %s:%d" % (addr[0], addr[1]))
207+
print(f"> Received incoming connection from {addr[0]}:{addr[1]}")
119208

120209
proxy_thread = Thread(
121210
target=proxy_handler,
@@ -125,6 +214,7 @@ def server_loop(
125214

126215

127216
def main():
217+
"""Main function of the proxy, parses the command line arguments and starts the server"""
128218
if len(argv[1:]) != 5:
129219
print("Usage: proxies/proxy.py [localhost] [localport]", end=" ")
130220
print("[remotehost] [remoteport] [receive_first]")
@@ -139,10 +229,7 @@ def main():
139229

140230
receive_first = argv[5]
141231

142-
if "True" in receive_first:
143-
receive_first = True
144-
else:
145-
receive_first = False
232+
receive_first = bool(receive_first)
146233

147234
server_loop(local_host, local_port, remote_host, remote_port, receive_first)
148235

0 commit comments

Comments
 (0)