A
A
Alons2020-05-13 01:34:20
Python
Alons, 2020-05-13 01:34:20

Unable to download file via socket, why?

trying to write a reverse shell, still learning...
link to server
link to client
The problem is with the download function in the client (presumably). When it is called on the server, the file is downloaded, but the file remains inaccessible until the socket is closed. Those. if you declare s.close() at the end of the function, then the file will be available, and the client will subsequently fly away with an error in the s.send(output.encode()) line.
If the socket is not closed, then upon exiting the download function, it falls into an infinite loop without returning anything to the server, but if the script is killed, the downloaded file will become available...
It is required that after downloading the file be available without explicitly closing the socket or shutdown (to one of the sides) ... and of course the client continued to work in the normal mode, continuing to execute further commands. But I just can’t understand what is my mistake and how can I implement my plan?!?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-05-13
@Alons

The main problem in tqdm. You can't use an iterator at the same time

progress = tqdm.tqdm(range(filesize) ... for _ in progress:
and manually update the progress, it will be terribly buggy (try it, write the simplest loop:
r=tqdm.tqdm(range(100))
for i in r:
  sleep(1)
  r.update(10)

you will see what will happen.
tqdm is buggy and doesn't stop the receive cycle on the server in time. When the file is completely downloaded, the line bytes_read = client_socket.recv(BUFFER_SIZE)is blocked waiting for new data and everything hangs.
In general, for blocking sockets, an empty result of recv is a sign of an error / closing of the connection, if there is no data, recv() blocks.
You either need to deal with tqdm, or count the length of the received data and break the loop like this:
if downloaded >= filesize:
  break

Well, in theory, this is a dumb approach:
received = client_socket.recv(BUFFER_SIZE).decode()
filename, filesize = received.split(SEPARATOR)

No one guarantees in general that what you sent in separate packets (separate send) will also arrive in separate packets (that is, you will need to receive data in separate recv). Data can be buffered and arrive in one packet. And you read garbage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question