Python Forum
How do i listen to loopback address on my local computer, without a port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i listen to loopback address on my local computer, without a port
#1
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?
Reply
#2
(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.
Reply
#3
(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.
DigiGod likes this post
Reply
#4
DigiGod thank you for the help. This was alot of help.
DigiGod likes this post
Reply
#5
[/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.
Reply
#6
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.
Gribouillis write May-27-2023, 08:17 AM:
Off site promotion link removed, please read What to NOT include in a post
billykid999 likes this post
Reply
#7
(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...)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listen TCP and send data to websockets neoboby 3 2,898 Feb-13-2023, 09:47 PM
Last Post: Vadanane
  Your p“Timeout !!!!” when I enter a known good IP address when running port scanner James2000k 8 2,751 Aug-06-2022, 10:42 AM
Last Post: Larz60+
  error opening port from local to internet looney99 3 2,774 Oct-16-2020, 02:01 PM
Last Post: BeanieW
  UDP Listen without Bind() GaryFunk 3 8,405 Mar-29-2018, 02:28 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020