How to change the payload of a packet on retransmission? - scapy

I want to try to implement the RSTEG method using scapy. That is, send a packet, the receiver does not send a confirmation message, this packet is sent again, but with a steganogram in the payload, the receiver again does not send a confirmation message. Then this package is sent again, but without the steganogram. (the same packet is sent three times, but a steganogram is sent a second time.) Is it possible to implement it with Scapy, How can I do that?
With the creation of packets and they are sent there is no problem. I can use timeout and retry to not receive the confirmation message and resend the package. But I do not understand how in this case to change the payload and at the end to get a confirmation message.

You can do something like -
packet=IP() / UDP() / ... / Raw(load=your_data) # the ... are for all your other protocols
response=sr1(packet, timeout=5)
if response is None:
# response is not received, send it 2nd time
packet[Raw].load = your_data_with_steganogram
response=sr1(packet, timeout=1)
if response is None:
# response is not received, send it 3rd time
packet[Raw].load = your_data
response=sr1(packet, timeout=1)

Related

How to use GetRepliesRequest call in Telethon

There is api method messages.getReplies in Telegram API and the equivalent of the same is
functions.messages.GetRepliesRequest in the Telethon.
But this method is not returning the expected replies/comments to the post. Instead, it returns multiple messages including the replies to the requested message_id and other messages also which are not even the replies to the requested message_id.
for conv in client.iter_messages(channel.id):
if conv.reply_to:
# get parent message this message reply to
original_message = conv.get_reply_message()
try:
#iterate all the replies for the parent message
for reply in client.iter_messages(channel.id,
reply_to=original_message.id):
print('\tReply message -> ', reply.to_dict())
except telethon.errors.rpcerrorlist.MsgIdInvalidError:
print('exception ***************')
Here it returns the replies to the input message.id in the argument reply_to including the messages which are not the replies to the input message.id.
(I checked the response from of the method call(inner for loop) and their reply_to_msg_id differs from what i requested to get the result).
I could not understand the behaviour of these replies getting in the result.
Also Telegram API docs are not good in shape with example and explantion.
What and how messages are considered as reply to the message in the telegram?
How telegram decides upon the messages whether it is a reply or a normal message?
if a message is reply, then to which message this is a reply?
Given a broadcast channel with comments enabled (let's say the channel's username is username), and a post with a discussion started (comments) for the channel post with message ID 1001, the following code will print all comments for post 1001 in channel username:
async for m in client.iter_messages('username', reply_to=1001):
print(m.text)
This is equivalent to clicking on the "# comment(s)" button in Telegram Desktop. Unfortunately I was not able to reproduce what you mention here:
But this method is not returning the expected replies/comments to the post. Instead, it returns multiple messages including the replies to the requested message_id and other messages also which are not even the replies to the requested message_id.
Now, for the other questions:
What and how messages are considered as reply to the message in the telegram?
Let's look at the problem from a different angle: send_message with comment_to.
First, messages.getDiscussionMessage must be used on the source broadcast channel with the source message ID. This will return the corresponding "discussion message" in the linked "discussion megagroup channel".
Now, messages.sendMessage can be used to send a message in the linked discussion megagroup channel to reply to the corresponding discussion message.
As you can see, "comments" are simply "replies to" the corresponding message of the discussion group. Hence the name, reply_to, during iter_messages.
How telegram decides upon the messages whether it is a reply or a normal message?
In a given chat, messages can reply to other previous messages in the same chat (in Telethon, message.reply_to). However, for channel comments, they're also replies in a way (just in a different chat), hence the parameter name. I tried to stick with Telegram's naming convention and solve the confusion by documenting the parameter but that might've been the wrong choice.
if a message is reply, then to which message this is a reply?
This can be found through message.reply_to.

PySerial write doesn't send complete message from script

When I try to send a message through the serial port from the Powershell console, I perfectly receive it on the other side (I am using Termite on the receiver to track the messages). However, when I try to execute exactly the same commands from a script, only the first few bytes of my message are received. I am using Python v3.7.2 and pySerial v3.4
I have already tried waiting some time between opening the serial port and using write for the first time. However, it still doesn't work.
When I execute exactly the same commands from the Powershell console, I manage to receive correctly the whole message on the receiver.
import time
import serial
ser = serial.Serial(port = 'COM3', baudrate = 9600, bytesize =
serial.EIGHTBITS, parity = serial.PARITY_NONE, timeout = 5)
msg = 'This is just a message I am trying to send.'
time.sleep(1)
ser.write(msg.encode())
I am just receiving the message "This" in the receiver. What should actually happen is that on the receiver I should see the complete message I send, and not just the first few characters.

twilio: detecting that a dial target is busy?

I'm using the twilio REST interface via flask under python3.
I am using the dial method to route incoming calls to another number (stored in the target variable), as follows:
resp = VoiceResponse()
resp.dial(
target,
action=url_for('callstatus'),
method='GET',
timeout=20
)
return Response(str(resp), 200, mimetype='application/xml')
This works with no problem if the target number starts ringing. However, if the target number is busy, the dial method does not detect this, and it just lets the caller hear the busy signal until the timeout occurs.
I'd like to somehow immediately detect that the target is busy and then route the call to voicemail in that case.
I know how to do the routing to voicemail, but I don't know how to make this happen automatically upon encountering a busy signal.
Is there any way to do this via the twilio REST API?
UPDATE: I tried the following, and it didn't work ...
resp = VoiceResponse()
dial = Dial(
action=url_for('callstatus'),
method='GET',
timeout=20
)
dial.number(
target,
status_callback=url_for('callstatus'),
status_callback_event='initiated ringing answered completed',
status_callback_method='GET'
)
resp.append(dial)
return Response(str(resp), 200, mimetype='application/xml')
When I do it this way, I get a busy signal, and it goes on forever. The time out in the original Dial object gets ignored.
NOTE: I based the above code on this following example within the twilio documentation for Number ...
response = VoiceResponse()
dial = Dial()
dial.number(
'+14158675310',
status_callback_event='initiated ringing answered completed',
status_callback='https://myapp.com/calls/events',
status_callback_method='POST'
)
response.append(dial)
print(response)
ANOTHER NOTE: the status_callback is ignored within the number object. If I comment out action in the dial object, my status callback never gets called, even if I hang up the call. And if I put action back into the dial object and comment out all the status_callback attributes in the number object, the action callback does get called when I hang up the call. This means that action is being recognized, but status_callback is being ignored.
... and I get exactly the same behavior, whether I set the method to POST or GET.
Twilio developer evangelist here.
You could try using <Number> within the <Dial> you have there and including a statusCallback attribute. When the dialled number is busy, Twilio will send a webhook to say that the call is completed and the reason was that it was busy. You could then use the REST API to modify the call and send it to some new TwiML to collect voicemail.
Alternatively, you could put the incoming call into a queue with <Enqueue> and use the REST API to dial out to the phone number. If that call then comes back as busy you can redirect the incoming call out of the queue and into voicemail. If the outbound call is a success then you respond with <Dial> and <Queue> to connect the callers.

How to establish a 2 way communication using sockets in Python?

So, I have a server script that receives an image from a client script and is supposed to send an acknowledgement saying "OK". But the acknowledgement never goes through.
Server script -
import socket,sys
s = socket.socket()
print("Socket successfully created")
port =80
s.bind(('', port))
print("socket binded to %s" %(port))
s.listen(5)
print("socket is listening")
while True:
c, addr = s.accept()
print('Got connection from', addr)
file_name=s.recv(1024)
file_name=fil_ename.decode("utf-8")
with open(file_name,"wb")as f:
while True:
data=c.recv(1024)
if not data:
break
f.write(data)
c.send(bytes('Thank you ! File received.',"utf-8"))
c.close()
Client script -
import socket
s = socket.socket()
# Define the port on which you want to connect
port = 80
s.connect(('IP address of my server', port))
s.send(bytes("hand.jpeg","utf-8"))
f=open("back.jpeg","rb")
data=f.read(512)
while data:
s.send(data)
data=f.read(512)
f.close()
print(s.recv(10))
The server does not send any acknowledgement and seems to get stuck in the for loop. But if i remove the line c.send(bytes('Thank you ! File received.',"utf-8")) from the server script, the code works well. Also, if I remove the receive part from server side and just send the acknowledgent part , i.e c.send(bytes('Thank you ! File received.',"utf-8")) , the client receives the message. But If a combination of receive(the image file) and the acknowledgement is made in the server side as shown in the code, the server side fails to respond.
Important thing to note is that on KeyBoardInterrupt-ing the above program, it shows that the server side script is hanged/stuck in the data=c.recv(1024) line. But the same problem vanishes if the acknowledgement line is removed.
Note:- The client side script is running on my local machine and the server side script is running on a Google Cloud VM Instance.
Please help.
Thank you.
Hmm... I don't think I completely believe your description of the behavior. But I do know what's wrong. It's entirely reasonable that your server is sitting in the receive loop, because the client hasn't signaled EOF to the connection. Under what circumstances do you believe that this will actually break?
if not data:
break
The answer is that the client needs to either close the socket, or use shutdown(SHUT_WR) to indicate that it will not be sending any more data. So to do what you want, on the client side:
...
f.close()
s.shutdown(socket.SHUT_WR)
...
Now the next time the server calls recv, it will get an empty string returned and the break above will be taken.
This leaves the connection open in one direction but not the other. So the client will not be able to send any more data. However, the server will still be able to send to the client until it closes the socket (or uses shutdown itself).
There is another more subtle problem. You are assuming that your first server-side recv will receive only and exactly the bytes containing your file name. 99.9% of the time that will work. But it's possible that the data from your next client-side send will also be available when the server calls recv for the first time. That will likely give you a bogus file name (though not necessarily an illegal one) and will certainly mean that your file is not transferred faithfully.
You should never assume that the data provided by a single send by one peer will be received by a corresponding single recv on the other side. The data received could be more or less and it's up to the application to frame the data to ensure that it receives exactly the intended amount.
Here
while True:
data=c.recv(1024)
if not data:
break
f.write(data)
it loops back to waiting for a message after it has received one because you don't break the while loop after receiving data. the if not data: doesn't do anything since recv() stops and waits until it gets a message, and thus data is never nothing. You should either break the loop after receiving a message by adding a break after f.write(data), or send the OK in the loop.

Is socket.write thread" safe?

I am using node.js to communicate over a socket in json. One of the operations sends a big file over the socket in base64 format. As a result the message is split into packets and triggers various data events on the client. I handle this using a buffer on the client side
var response={
file: fs.readFileSync("FileName","base64")
}
socket.write(JSON.stringify(response));
On the client side, using a buffer
cleartextStream.on("data",function(data){
console.log("Received data")
try {
data=JSON.parse(data);
_buffer="";
events.trigger(data.event,data);
}
catch(e){
console.log("Incomplete message received. Building Buffer");
_buffer+=data;
if(/\}$/.test(data) ){//Tests for json boundary
data=JSON.parse(_buffer);
events.trigger(data.event,data);
}
}
This works fine for me for now. The only problem I anticipate is if while the file is being sent, some other event triggers a write on the socket.
t=0 Start sending file
t=5 Still sending file
t=6 Another event uses socket.write to start sending message
t=7 still sending file
t=8 Another event sending message
This will result in garbled messages. So does socket.write block while sending a single message, or will it allow other methods to use socket.write even before the transmission is completed?

Resources