Restricted access to remote ports - amazon-ec2

Kind of a weird scenario:
I have an instance running using EC2 on AWS. I can ping different ports (opened or closed) when I am on certain internet connections (ie. work wifi, phone 3G, home wifi) -- they immediately return expected results or refused connections, depending on what ports I am using.
On different internet connections (ie. public wifi, mobile wifi, even via ethernet at work) the request hangs. It doesn't refuse connection, but just hangs until operation timeout.
On Chrome, in chrome://net-internals/#events, it always hangs at the Socket 'TCP_CONNECT_ATTEMPT' step:
890298: SOCKET
my-domain.com:81
Start Time: 2018-05-31 12:11:21.142
t=6421 [st=0] +SOCKET_ALIVE [dt=?]
--> source_dependency = 890297 (TRANSPORT_CONNECT_JOB)
t=6421 [st=0] +TCP_CONNECT [dt=?]
--> address_list = ["22.333.22.22:81"]
t=6421 [st=0] +TCP_CONNECT_ATTEMPT [dt=?]
--> address = "22.333.22.22:81"
Then it times out, updating like so:
890298: SOCKET
my-domain.com:81
Start Time: 2018-05-31 12:11:21.142
t= 6421 [st= 0] +SOCKET_ALIVE [dt=75230]
--> source_dependency = 890297 (TRANSPORT_CONNECT_JOB)
t= 6421 [st= 0] +TCP_CONNECT [dt=75230]
--> address_list = ["22.333.22.22:81"]
t= 6421 [st= 0] +TCP_CONNECT_ATTEMPT [dt=75230]
--> address = "22.333.22.22:81"
t=81651 [st=75230] -TCP_CONNECT_ATTEMPT
--> os_error = 60
t=81651 [st=75230] -TCP_CONNECT
--> net_error = -118 (ERR_CONNECTION_TIMED_OUT)
t=81651 [st=75230] -SOCKET_ALIVE
Why would it work on some internet connections, and then block others?

Related

Fetch emails through IMAP with proxy of form user:password:host:port

I have code to login to my email account to fetch recent emails:
def fetchRecentEmail(emailAddr, emailPassword, timeout=120):
host = fetch_imap_server(emailAddr) # e.g. 'outlook.office365.com'
with IMAP4_SSL(host) as session:
status, _ = session.login(emailAddr, emailPassword)
if status == 'OK':
# fetch most recent message
status, messageData = session.select("Inbox")
:
I'm trying to tweak it to go through a proxy.
ref: How can I fetch emails via POP or IMAP through a proxy?
ref: https://gist.github.com/sstevan/efccf3d5d3e73039c21aa848353ff52f
In each of the above resources, the proxy is of clean form IP:PORT.
However my proxy is of the form USER:PASS:HOST:PORT.
The proxy works:
USER = 'Pp7fwti5n-res-any-sid-' + random8Digits()
PASS = 'abEDxts7v'
HOST = 'gw.proxy.rainproxy.io'
PORT = 5959
proxy = f'{USER}:{PASS}#{HOST}:{PORT}'
proxies = {
'http': 'http://' + proxy,
'https': 'http://' + proxy
}
response = requests.get(
'https://ip.nf/me.json',
proxies=proxies, timeout=15
)
The following code looks like it should work, but errors:
HOST = 'outlook.office365.com'
IMAP_PORT = 963
PROXY_TYPE = 'http' # rainproxies are HTTP
mailbox = SocksIMAP4SSL(
host=HOST,
port=IMAP_PORT,
proxy_type=PROXY_TYPE,
proxy_addr=URL,
proxy_port=PORT,
username=USER,
password=PASS
)
emailAddress, emailPassword = EMAIL.split(',')
mailbox.login(emailAddress, emailPassword)
typ, data = mailbox.list()
print(typ)
print(data)
I needed to add a timeout arg/param in 2 places to get the code to run:
def _create_socket(self, timeout=None):
sock = SocksIMAP4._create_socket(self, timeout)
server_hostname = self.host if ssl.HAS_SNI else None
return self.ssl_context.wrap_socket(
sock, server_hostname=server_hostname
)
def open(self, host='', port=IMAP4_PORT, timeout=None):
SocksIMAP4.open(self, host, port, timeout)
Rather confusing that nobody else seems to have flagged that in the gist.
But it still won't work.
If I use any number other than 443 for IMAP_PORT I get this error:
GeneralProxyError: Socket error: 403: Forbidden
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)
And if I use 443, while I now get no error, mailbox = SocksIMAP4SSL( never completes.
So I am still far from a working solution.
I am hoping to run this code simultaneously on 2 CPU cores, so I don't understand the implications of using port 443. Is that going to mean that no other process on my system can use that port? And if this code is using this port simultaneously in two processes, does this mean that there will be a conflict?
Maybe you can try monkeypatching socket.socket with PySocket.
import socket
import socks
socks.set_default_proxy(socks.SOCKS5, HOST, PORT, True, USER, PASS)
socket.socket = socks.socksocket
Then check if your IMAP traffic is going through a given proxy.

Connect Lopy with Windows over BLE

I am trying to send data from the Lopy to my Windows PC over Bluetooth Low Energy (BLE).
On the PC, I see the Lopy and I can connect him. But a few seconds later the Lopy disconnects.
This is my code:
bluetooth = Bluetooth()
bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456')
def conn_cb (bt_o):
events = bt_o.events() # this method returns the flags and clears the internal registry
if events & Bluetooth.CLIENT_CONNECTED:
print("Client connected")
pycom.rgbled(0x00ff00)
elif events & Bluetooth.CLIENT_DISCONNECTED:
print("Client disconnected")
pycom.rgbled(0xFF0000)
bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
bluetooth.advertise(True)
srv1 = bluetooth.service(uuid=b'1223190123456', isprimary=True)
chr1 = srv1.characteristic(uuid=b'ab3451230123456', value=5)
while True:
print("Send Data")
chr1.value("here is the data need to be send it should be a very very long string")
time.sleep(3.050)
def char1_cb_handler(chr, data):

Doesn't get Access_accept packet from freeradius server

1.From client:
root#amsys-LIFEBOOK-AH502:/home/amsys# radtest -t chap usr password 127.0.0.1 0 testing123
This is how,the way i sended a packet access-request packet from the client (here,loop back only).
2.From server.
the server responds to client as shown as below:
Ready to process requests.
Ignoring request to auth address * port 1812 as server default from unknown client 127.0.0.1 port 34962 proto udp
3.server to client
Sending Access-Request of id 67 from 0.0.0.0 port 47852 to 127.0.0.1 port 1812
User-Name = 'usr'
User-Password = 'password'
NAS-IP-Address = 127.0.1.1
NAS-Port = 0
Message-Authenticator = 0x00
radclient: no response from server for ID 67 socket 3
if anybody would aware about this thing,please give your prompt response and pleased me.thanking you.!

Sending packets between Amazon EC2 instances using their MAC-addresses

Using this small ruby script + packetfu, I try to retransmit a broadcast packet to several hosts:
#!/usr/bin/ruby
require 'rubygems'
require 'packetfu'
dev = ARGV[0]
mac=`ip link show #{dev} | awk '/ether/ {print $2}'`
ARGV.shift
dests = ARGV
cap = PacketFu::Capture.new(
:timeout => 4000,
:iface => dev, :start => true,
:filter => "ether src #{mac} and ether[0] & 1 = 1")
loop do
cap.stream.each do |pkt|
frame = PacketFu::IPPacket.parse(pkt)
i = 0
dests.each do |dest|
frame.eth_daddr = dest
frame.ip_header.ip_id = frame.ip_header.ip_id + i
frame.ip_header.ip_sum = frame.ip_header.ip_calc_sum()
frame.to_w(dev)
i = i + 1
end
end
end
Using tcpdump, I see that they are retransmitted (sent several
times).
$ echo "foo" | netcat -ub 192.168.15.255 54321
On host1 (source host), tcpdump shows me at the moment:
12:19:30.602374 0a:f4:8d:75:e5:af > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 46: 192.168.15.17.36339 > 192.168.15.255.54321: UDP, length 4
12:19:30.605856 0a:f4:8d:75:e5:af > 0a:a2:5f:f1:de:e4, ethertype IPv4 (0x0800), length 46: 192.168.15.17.36339 > 192.168.15.255.54321: UDP, length 4
12:19:30.608896 0a:f4:8d:75:e5:af > 0a:25:ef:2c:24:e3, ethertype IPv4 (0x0800), length 46: 192.168.15.17.36339 > 192.168.15.255.54321: UDP, length 4
On host2 (destination host, mac address 0a:a2:5f:f1:de:e4), tcpdump shows nothing.
Security policy allows all traffic in the VPC.
Have you any ideas?
I'm sure that I miss some small thing.
(I thank Kenta Yasukawa for the answer):
Have you turned off the source/destination check for your instances? By
default, EC2 instances drop an incoming packet whose destination address does
not match the receiver IP address. You can disable it by right-clicking on an
instance in Management console and select "Change Source / Destination check",
and click disable. 
To disable the ckeck using EC API Tools:
$ ec2-modify-network-interface-attribute --source-dest-check False eni-8c8c9bca

How to find out the port number of a TCP connection

Let's say I have the following piece of code.
server = TCPServer.new(3200)
client = server.accept()
How do I find out what port number that client sent its message to me is? I have tried both client.peeraddr and client.addr and both of them do not give me the proper port number.
Port that clients are connecting to is 3200. And port on client side where connection is created from is random for every connection, given by OS from unused ports.
client.peeraddr gives you an array that corresponds to a struct addrinfo. For AF_INET, it looks something like this:
["AF_INET", 48942, "127.0.0.1", "127.0.0.1"]
You can create an Addrinfo object from it and get the port like so:
require 'socket'
server = TCPServer.new(3200)
client = server.accept()
addr = Addrinfo.new(client.peeraddr)
port = addr.ip_port

Resources