Run a second script, passing STDIN

Viewed 60

Clickbank (and others) post an IPN message to the URL address on the domain / server. Presumably, the data goes to <STD IN>, and the script processes. I'm on Windows / Strawberry Perl, and can emulate this by posting form data to the script. The problem is, I want to "interupt" this flow ...

What I =WANT= to do is for my 'replacement' script to capture the <STD IN> from CB, and then pass the unaltered to the 'original' program. In effect, my replacement script is invisible, and as far as the original script is concerned, it is still getting an input from ClickBank, (save for any IP checking / referrer HTTP checks of the original script) As this original script is a "Third Party" app such as a membership site, it cannot be amended. Note: This isn't phishing or similar. It has the consent of the original product vendor.

  1. I tried to "require" the original script from the replacement, but now "original" cannot see anything in its <STDIN>

  2. I tried to fork. The second thread opens, but again, "original" cannot see the <STDIN> input

  3. I tried "$message=<STD IN >" before the fork, and after the fork with no difference.

  4. I did wonder about our @ARGV ... but not sure that would get the data into the "original" <STD IN>

  5. (One solution would be to resend the whole message to the "original" URL, but that seems wasteful since the script is in the same folder)

** What I am trying to achieve: Get the encoded string from Clickbank sent to my server, but the action being "invisible" to the original script.

CBData ---> original.pl (As the process runs at present)

CBData --> my replacement sending me copy of data --> original script.

** I wrote my installer to place the replacement script in the same folder of original: /cgi-bin/original.pl /cgi-bin/replacement.pl

The code - commented out at present - also sends a request to my server to see if it's online. If it's down, it sends the $message to a secondary server, and this will be processed once the original site is online, (basically, the second site 'pings' the original server until it says "Hallo?", and then transmits any data it has accumulated

#! "C:/Strawberry/perl/bin/perl.exe"
use lib ("C:/Users/Chris Brown/perl5/lib/perl5/");

use strict;
use warnings;
use LWP::UserAgent ();
print "content-type: text/html\n\n";

my $ua = LWP::UserAgent->new(timeout => 10);
$ua->env_proxy;

my $response = $ua->get('http://XXXXX.YYY');

if ($response->is_success) {
#require "./testsecondprog.pl"; # User's original perl script
my $message=<STDIN>;
my $cpid = fork();

if ($cpid == 0) {
#    printf "%s\n", "I'm the child, pid is $$";
require "./testsecondprog.pl";
} else {
print $message;
printf "%s\n", "I'm the parent, pid is $$, cpid is $cpid";
}

#require "./testsecondprog.pl";
#my $url="http://www.XXXXX.YYY/ipn/readPerl.php";
#my $res = $ua->post( $url, "content"=>$message);
#print "content-type: text/html\n\n";
print "and back to the first program";
print $message;
}

(Yes, I know the lower prompt should be in the parent bracket of fork; I'm only testing at present)

The "original" program is:

#! "C:/Strawberry/perl/bin/perl.exe"
use lib ("C:/Users/Chris Brown/perl5/lib/perl5/");

# This is testing to see if the required program can still access <STDIN>
# and whether the output will still be sent to the browser, and not returned to the require script

my $message=<STDIN>;

print "content-type: text/html\n\n";
print "This is the second script";
print $message;
print "<br><br>*************

Jumping to this script directly displays the $message / <STDIN>, so it is working. But only displays text prompts when 'called' via the other program.

It may be a "quirk" in Windows that stops it working.

1 Answers

This is the code I FINALLY managed to crack: It pipes the <STDIN> to the other script, at the same time building the $message variable. The piped program (seems to) run as normal as it sent output to browser. The LWP::UserAgent then sends the $message to the other site. If that is not running, (which I'm hoping is the same as a wrong URL that I typed to simulate a offline condition) it does not return a "200" code. Thus, the second part runs to send it to another server. (Only 10hrs to crack it !!)

#! "C:/Strawberry/perl/bin/perl.exe"
use lib ("C:/Users/Chris Brown/perl5/lib/perl5/");
use strict;
use warnings;
use LWP::UserAgent ();

my $message;
open my $fh, "|-","readPerl.pl"  or die "Couldn't open a pipe into: $!";
foreach my $line ( <STDIN> ) {
$message.=$line;
print $fh $line;
}

my $ua = LWP::UserAgent->new(timeout => 10);
$ua->env_proxy;
my $response = $ua->post('http://www.atomz.com/cgi-bin/readPerl.pl',{"code"=>"$message"});

if ($response->{'_rc'} ne '200') {
print "send to backup server";
}
Related