5

I need to write a little piece of code to simulate traffic from different source IP addresses and I'm wondering if that can be done by spoofing the addresses with Perl?

I tried Net::RAWIP and that worked, but I need to send some more complex HTTP traffic (namely POST data) and was unable to do so with RAWIP

With LWP I tried using ua->local_address but I get this response:

Can't connect to 10.x.x.x:8080

LWP::Protocol::http::Socket: Cannot assign requested address at /usr/lib/perl5/site_perl/5.10.0/LWP/Protocol/http.pm line 51.

This is the code I'm using:

#!/usr/bin/perl -w

use strict ;
use warnings ;
use LWP::UserAgent ;
use URI::URL ;

my $path = 'http://142.133.114.130:8080' ;
my $url = new URI::URL $path;
my $ua       = LWP::UserAgent->new();

$ua->local_address('10.121.132.112');
$ua->env_proxy ;
my $effing = 'blaj.jpg' ;
my $response = $ua->post( $url,
                        'Content-Type' => "multipart/form-data",
                        'Content' => [ userfile => ["$effing" ]],
                        'Connection' => 'keep-alive' ) ;
print $response->decoded_content();
blackbird
  • 1,129
  • 1
  • 23
  • 48

1 Answers1

2

You can't get a response if you send from an address that's not yours. That means all you can do is send the request. You've indicated you can do the sending, so all you need is the request to send. That's easy.

use strict;
use warnings;

use HTTP::Request::Common qw( POST );

my $req = POST('http://www.example.org/',
   'Content-Type' => "multipart/form-data",
   'Content'      => [ userfile => [ $0 ]],
   'Connection'   => 'keep-alive',
);

print $req->as_string();

Output:

POST http://www.example.org/
Connection: keep-alive
Content-Length: 376
Content-Type: multipart/form-data; boundary=xYzZY

--xYzZY
Content-Disposition: form-data; name="userfile"; filename="x.pl"
Content-Type: text/plain

use strict;
use warnings;

use HTTP::Request::Common qw( POST );

my $req = POST('http://www.example.org/',
   'Content-Type' => "multipart/form-data",
   'Content'      => [ userfile => [ $0 ]],
   'Connection'   => 'keep-alive',
);

print $req->as_string();

--xYzZY--
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Sending the file is not a problem, it's changing the source IP of the request. I'm on a LAN and I have control over the routes and the listening server so that's ok – blackbird Sep 13 '12 at 17:49
  • You said you can already do that with Net::RAWIP. The only thing you couldn't do with Net::RAWIP is format the request, and I showed you how to do that. – ikegami Sep 13 '12 at 18:16
  • Does the print statement actually send the data once the object is setup? RAWIP uses the send() method, but that takes no data arguments. And the data field in the constructor only takes strings – blackbird Sep 13 '12 at 18:42
  • No, it sends it to STDOUT. Obviously, replace that. – ikegami Sep 13 '12 at 18:50