Forum Discussion

dragonflymr's avatar
dragonflymr
Icon for Cirrostratus rankCirrostratus
Mar 10, 2015

iRule to randomly change source IP

Hi,

 

Still to new to iRules so I have problem figuring out rule that will simulate clients coming from different geolocations when traffic is generated from limited pool of private ip addresses. So something like that: 1. Packet is coming with private IP 2. Random generator (I will find code for that but if somebody has ready one allowing to generate country located ip I will appreciate sharing) is generating some IP and replacing original one with generated before sending request to server - actually it would be great if it could be changed on client side but I doubt it is possible? I would like to generate some date for Analytic and other statistic modules on LTM 3. Before sending response packet destination IP is changed back to original source IP before sending back to the client

 

Piotr

 

  • And maybe you find these useful as well:

    when CLIENT_ACCEPTED {     
        snat 10.10.10.[expr ( [getfield [IP::client_addr] "." 4] % 32 ) + 1]
       snat 10.10.[getfield [IP::client_addr] "." 3].[getfield [IP::client_addr] "." 4]
    }
    

    The following one simulates varying clients by inserting a randomized X-Forwarded-For and uses another internal virtual server for processing:

    when HTTP_REQUEST {
        snat automap
        HTTP::header insert X-Forwarded-For [expr int(rand()*128) + 32].[expr int(rand()*254)].[expr int(rand()*254)].[expr int(rand()*254)]    
        virtual vs_internal
    }
    
  • Hi Piotr,

    here is a sample iRule to do exactly this:
    when CLIENT_ACCEPTED {
        snat [expr { int(rand()*193) + 1}].[expr { int(rand()*254) + 1}].[expr { int(rand()*254) +1}].[expr { int(rand()*253) +1}]
    }
    

    Thanks, Stephan

  • Dojs's avatar
    Dojs
    Icon for Cirrostratus rankCirrostratus

    I'm trying to do this random_ip_irule (device) but won't works. With 2 VS and iRules. Could you tell me if had some changes to 13.1.0.2?

     

  • ypg's avatar
    ypg
    Icon for Nimbostratus rankNimbostratus

    Hi Pitor,
    I understand that you had a question back in 2015 about simulating clients from different geolocations using a limited pool of private IP addresses. Although your question was from a few years ago, I wanted to provide an updated answer for anyone facing a similar challenge in 2023.

    If you're looking to simulate clients from different geolocations using a limited pool of private IP addresses, you can consider using a random IP address generator. I recommend checking out https://randomtools.io/random-ipaddress-generator/ for a ready-to-use tool that can generate country-located IP addresses.

    To achieve the desired flow:

    1. When a packet arrives with a private IP, you can use the random IP generator from the provided link to generate a new IP address representing a different geolocation.
    2. Replace the original private IP with the generated IP before forwarding the request to the server.
    3. If possible, consider modifying the response packet's destination IP back to the original source IP before sending it back to the client.

    Please keep in mind that modifying IP addresses in transit may have specific limitations or dependencies on the network infrastructure and client capabilities.