Posts: 26
Threads: 7
Joined: May 2023
I want to listent to my loopback address on my windows PC, without attaching it to a port. I want to listent to any messages, or ICMP pings sourced from address 10.0.0.1, as the 10.0.0.0 network is an internal network which shoots off a ping to the loopback in cases of network congestion. I'm using routers to send a ping, and they can't ping ports. (they can traceroute ports, which i may try to implement later). The python listener will then trigger based on a ping from 10.0.0.1. Any suggestions on how to do this? I read back last summer on socket programming and have done a few tutorials, but these usually refer to just that: socket programming, establishing connections, and binding to ports. I want a simple ip address listener, that listens to ip address loopback for a ping sourced from 10.0.0.1 ~ any tips?
Posts: 9
Threads: 0
Joined: May 2023
(May-23-2023, 08:30 PM)billykid999 Wrote: I want to listent to my loopback address on my windows PC, without attaching it to a port. I want to listent to any messages, or ICMP pings sourced from address 10.0.0.1, as the 10.0.0.0 network is an internal network which shoots off a ping to the loopback in cases of network congestion. I'm using routers to send a ping, and they can't ping ports. (they can traceroute ports, which i may try to implement later). The python listener will then trigger based on a ping from 10.0.0.1. Any suggestions on how to do this? I read back last summer on socket programming and have done a few tutorials, but these usually refer to just that: socket programming, establishing connections, and binding to ports. I want a simple ip address listener, that listens to ip address loopback for a ping sourced from 10.0.0.1 ~ any tips?
To listen for ICMP ping packets originating from a specific IP address (such as 10.0.0.1) on the loopback interface in Python, you can use the socket module along with the AF_PACKET address family.
Here's an example code snippet that demonstrates how you can achieve this:
import socket
import struct
def listen_for_ping(source_ip):
# Create a raw socket using AF_PACKET address family
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
# Set the interface to loopback
sock.bind(("lo", 0))
while True:
# Receive a packet
packet, _ = sock.recvfrom(65535)
# Extract the IP header from the packet
ip_header = packet[14:34]
# Unpack the source and destination IP addresses from the header
source_address = socket.inet_ntoa(ip_header[12:16])
destination_address = socket.inet_ntoa(ip_header[16:20])
# Check if the packet is an ICMP ping from the specified source IP
if (
source_address == source_ip
and destination_address == "127.0.0.1" # Loopback address
and packet[23] == 1 # ICMP protocol (1 is for ICMP)
and packet[34] == 8 # ICMP type 8 is Echo Request (ping)
):
print("Received ping from", source_ip)
# Start listening for pings from 10.0.0.1 on the loopback interface
listen_for_ping("10.0.0.1") In this code, I created a raw socket using the socket.socket function with the AF_PACKET address family. I then bind the socket to the loopback interface ("lo") using the bind method.
Inside the infinite loop, I receive packets using recvfrom. I then extract the IP header from the packet and check if the packet is an ICMP ping from the specified source IP address (10.0.0.1) and destined for the loopback address (127.0.0.1).
If the conditions are met, I print a message indicating that a ping was received from the specified source IP.
Please note that running this code may require administrative/root privileges as it involves listening on a raw socket. Additionally, ICMP packets might be filtered by firewalls or security software, so ensure that your setup allows ICMP traffic on the loopback interface.
Hope this helps.
Posts: 26
Threads: 7
Joined: May 2023
(May-26-2023, 12:13 AM)DigiGod Wrote: (May-23-2023, 08:30 PM)billykid999 Wrote: I want to listent to my loopback address on my windows PC, without attaching it to a port. I want to listent to any messages, or ICMP pings sourced from address 10.0.0.1, as the 10.0.0.0 network is an internal network which shoots off a ping to the loopback in cases of network congestion. I'm using routers to send a ping, and they can't ping ports. (they can traceroute ports, which i may try to implement later). The python listener will then trigger based on a ping from 10.0.0.1. Any suggestions on how to do this? I read back last summer on socket programming and have done a few tutorials, but these usually refer to just that: socket programming, establishing connections, and binding to ports. I want a simple ip address listener, that listens to ip address loopback for a ping sourced from 10.0.0.1 ~ any tips?
To listen for ICMP ping packets originating from a specific IP address (such as 10.0.0.1) on the loopback interface in Python, you can use the socket module along with the AF_PACKET address family.
Here's an example code snippet that demonstrates how you can achieve this:
import socket
import struct
def listen_for_ping(source_ip):
# Create a raw socket using AF_PACKET address family
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
# Set the interface to loopback
sock.bind(("lo", 0))
while True:
# Receive a packet
packet, _ = sock.recvfrom(65535)
# Extract the IP header from the packet
ip_header = packet[14:34]
# Unpack the source and destination IP addresses from the header
source_address = socket.inet_ntoa(ip_header[12:16])
destination_address = socket.inet_ntoa(ip_header[16:20])
# Check if the packet is an ICMP ping from the specified source IP
if (
source_address == source_ip
and destination_address == "127.0.0.1" # Loopback address
and packet[23] == 1 # ICMP protocol (1 is for ICMP)
and packet[34] == 8 # ICMP type 8 is Echo Request (ping)
):
print("Received ping from", source_ip)
# Start listening for pings from 10.0.0.1 on the loopback interface
listen_for_ping("10.0.0.1") In this code, I created a raw socket using the socket.socket function with the AF_PACKET address family. I then bind the socket to the loopback interface ("lo") using the bind method.
Inside the infinite loop, I receive packets using recvfrom. I then extract the IP header from the packet and check if the packet is an ICMP ping from the specified source IP address (10.0.0.1) and destined for the loopback address (127.0.0.1).
If the conditions are met, I print a message indicating that a ping was received from the specified source IP.
Please note that running this code may require administrative/root privileges as it involves listening on a raw socket. Additionally, ICMP packets might be filtered by firewalls or security software, so ensure that your setup allows ICMP traffic on the loopback interface.
Hope this helps.
Thanks. This solved my problem. I read Python Black Hat chapter 3, after reading what you wrote your code is a lot more concise, I'm going to use parts of it down the road.
Posts: 26
Threads: 7
Joined: May 2023
DigiGod thank you for the help. This was alot of help.
Posts: 9
Threads: 0
Joined: May 2023
[/quote]
Thanks. This solved my problem. I read Python Black Hat chapter 3, after reading what you wrote your code is a lot more concise, I'm going to use parts of it down the road.
[/quote]
Good book, just waaaaaaaay outdated...lol. If you run into any more problems, don't hesitate to reach out.
Posts: 1
Threads: 0
Joined: May 2023
May-27-2023, 07:32 AM
(This post was last modified: May-27-2023, 08:17 AM by Gribouillis.)
Listening to the loopback address (127.0.0.1) on your local computer without specifying a port doesn't serve any practical purpose since network communication typically involves ports. However, if you want to test connectivity to the loopback address, you can use tools like ping or telnet to check if the loopback address is responding.
For example, you can open a terminal/command prompt and run the following command to ping the loopback address:
ping 127.0.0.1
This will send ICMP echo requests to the loopback address and wait for responses. If you receive responses, it means the loopback address is functioning correctly.
billykid999 likes this post
Posts: 26
Threads: 7
Joined: May 2023
(May-27-2023, 07:32 AM)kaimsuudgui Wrote: Listening to the loopback address (127.0.0.1) on your local computer without specifying a port doesn't serve any practical purpose since network communication typically involves ports. However, if you want to test connectivity to the loopback address, you can use tools like ping or telnet to check if the loopback address is responding.
For example, you can open a terminal/command prompt and run the following command to ping the loopback address:
ping 127.0.0.1
This will send ICMP echo requests to the loopback address and wait for responses. If you receive responses, it means the loopback address is functioning correctly.
Thanks. I'm triggering a Python script from an ip address ping after I parse the packet header. That's why I needed this. problem now is, given the code above my Windows 10 is printing both source and destination address as source. Not sure if anyone could help me with this? I tried a similar ICMP script from black hat python chapter 3, still printing the wrong source address(printing the dest as source and dest as dest...)
|