0

I have this code to send a packet in PHP, and I wanted to know how to do it in perl, I have tried sending via hex data, but it is not working. Here is the PHP code:

$sIPAddr = "37.221.175.211";                                                         
$iPort = 7777;                                                               
$sPacket = "";                                                                 
$aIPAddr = explode('.', $sIPAddr);                                              

$sPacket .= "SAMP";                                                           

$sPacket .= chr($aIPAddr[0]);                                                   
$sPacket .= chr($aIPAddr[1]);                                                   
$sPacket .= chr($aIPAddr[2]);                                                   
$sPacket .= chr($aIPAddr[3]);                                                   

$sPacket .= chr($iPort & 0xFF);                                               
$sPacket .= chr($iPort >> 8 & 0xFF);                                          

$sPacket .= 'c';                                                              


$rSocket = fsockopen('udp://'.$sIPAddr, $iPort, $iError, $sError, 2);           
fwrite($rSocket, $sPacket);

fclose($rSocket); 

How would I go about doing this in Perl? I want to use a raw socket as well to send it.

This is what I tried, but the server is not replying to it, which makes me think that the data is corrupted somewhere:

$packet = Net::RawIP->new({
                        ip => {
                              saddr => $saddr,
                              daddr => $dest,
                              },

                        udp => {
                              source => $rsport,
                              dest => $port,
                              data => "\x53\x41\x4d\x50\x25\xdd\xaf\xd3\x61\x1e\x63", # this is the data from the PHP file in HEX
                              },
                        });
  $packet->send;
6
  • Please show us what you've tried so far. You said you tried sending hex data. Did you do that in Perl? Please edit your question and let us see the code and explain how it is not working. Also check out How to Ask. Commented Jan 10, 2014 at 21:11
  • @simbabque Sorry about that, done! Commented Jan 10, 2014 at 21:15
  • Comments in Perl are #, not //. Please do not mock the code in here, but post the real code. Remove stuff you do not like us to see, but try to make it compile please. Commented Jan 10, 2014 at 21:23
  • @simbabque Well that is simply the method I am using to send the packet, the rest of the code is not relevant. Commented Jan 10, 2014 at 21:24
  • Have you sniffed what this is sending? Since you are using low level functions in PHP, maybe you should try using IO::Socket or IO::Socket::IP Commented Jan 10, 2014 at 21:31

1 Answer 1

1

Don't know about Net::RawIP, but here's the Perl variant that sends the exact same packet as your PHP code, using IO::Socket::INET module. For docs for it, see https://metacpan.org/pod/IO::Socket::INET

use strict;
use warnings;
use IO::Socket;

my $sIPAddr = '37.221.175.211';
my $iPort = 7777;
my $sPacket = 'SAMP' . join( '', map chr,
    split(/\./, $sIPAddr),
    $iPort & 0xFF,
    $iPort >> 8 & 0xFF,
) . 'c';

my $sock = IO::Socket::INET->new(
    Proto    => 'udp',
    PeerPort => $iPort,
    PeerAddr => $sIPAddr,
) or die "Could not create socket: $!\n";

$sock->send( $sPacket );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.