Botnet

Back to Module Development

Page: 1  
From User Message Body
spartacus
Post #975
Member
Date: 3:38 pm, Apr 19 2009
Posts: 2


Note: problem already solved, forgot to add \n at the end of the line :D

Hello,
I'm trying to create a (simple) botnet script. It's purpose is to let two (or more) php-irc bots connect to each other and communicate. There is a master and a slave. The slave connects to the master.
The connecting part now works, but if i send a line of text from the master to the slave or slave to master, it just reads an empty line.
The log of the master:
Code
Accepted new connection on: 95
onAccept
Sent: Testje
onWrite
onRead
Line: ''

Slave:
Code
onConnect
onRead
Line: ''
Sent: Testje2
onWrite


The master tries to send a line ("Testje"), and then the slave sends one back ("Testje2"). It triggers the onRead function but somehow the line is empty. So there has to be either something wrong with the sending or the receiving functions.
Here is the script for the master bot:
Code
<?php
class botmaster_mod extends module {
    public $title = "Botmaster";
    public $author = "me";
    public $version = "0.1";

    private $listener;
    private $conns = array();
    private $queue = array();

    public function init() {
        $conn = new connection(NULL, "404", 0);

        $conn->setSocketClass($this->socketClass);
        $conn->setIrcClass($this->ircClass);
        $conn->setTimerClass($this->timerClass);

        $conn->setCallbackClass($this);

        $conn->init();

        if ($conn->getError()) {
            echo "Error connecting: ".$conn->getErrorMsg()."\n";
        }

        $this->sockInt = $conn->getSockInt();

        $this->listener = $conn;
    }

    public function destroy() {
        $this->listener->disconnect();
        foreach ($this->conns as $conn) {
            $this->closeConnection($conn);
        }
    }

    public function onTransferTimeout($conn) {
        echo "onTransferTimeout\n";
        if (!$this->closeConnection($conn)) {
            return false;
        }
    }
    public function onConnectTimeout($conn) {
        echo "onConnectTimeout\n";
        if (!$this->closeConnection($conn)) {
            return false;
        }
    }

    public function onRead($conn) {
        echo "onRead\n";
        $connInt = $conn->getSockInt();

        if (!isset($this->conns[$connInt])) {
            $conn->disconnect();
            return false;
        }

        $line = $this->socketClass->getQueueLine($connInt);
        echo "Line: '".$line."'\n";
        return $this->socketClass->hasLine($connInt);
    }

    public function onWrite($conn) {
        echo "onWrite\n";
    }

    public function onAccept($listener, $newConn) {
        echo "onAccept\n";
        $connInt = $newConn->getSockInt();
        $this->conns[$connInt] = $newConn;
        $this->queue[$connInt] = array();
        $this->timerClass->addTimer("conn".$connInt, $this, "sendLines", $connInt, 0.25, false);
        $this->sendLine($connInt, "Testje");
    }
    
    public function sendLine($connInt, $line) {
        $this->queue[$connInt][] = $line;
    }
    
    public function sendLines($connInt) {
        if (!isset($this->conns[$connInt])) {
            $conn->disconnect();
            return false;
        }
        $conn = $this->conns[$connInt];
        foreach ($this->queue[$connInt] as $n => $line) {
            echo "Sent: ".$line."\n";
            $this->socketClass->sendSocket($connInt, $line);
            unset($this->queue[$connInt][$n]);
        }
    }

    public function onDead($conn) {
        echo "onDead\n";
        if (!$this->closeConnection($conn)) {
            return false;
        }
    }

    private function closeConnection($conn) {
        $connInt = $conn->getSockInt();

        if (!isset($this->conns[$connInt])) {
            $conn->disconnect();
            return false;
        }
        $this->conns[$connInt]->disconnect();
        unset($this->conns[$connInt]);
    }
}
?>


And the slave bot:
Code
<?php
class botslave_mod extends module {
    public $title = "Botslave";
    public $author = "me";
    public $version = "0.1";
    public $master = array("host" => "127.0.0.1", "port" => "404");
    
    private $conn;
    private $queue = array();

    public function init() {
        echo "BOTNET SLAVE, connecting to ".$this->master['host'].":".$this->master['port']."\n";
        $conn = new connection($this->master['host'], $this->master['port'], 30);

        $conn->setSocketClass($this->socketClass);
        $conn->setIrcClass($this->ircClass);
        $conn->setTimerClass($this->timerClass);

        $conn->setCallbackClass($this);

        $conn->init();

        if ($conn->getError()) {
            echo "Error connecting: ".$conn->getErrorMsg()."\n";
        }

        $this->sockInt = $conn->getSockInt();
        $conn->connect();
        $this->conn = $conn;
    }

    public function destroy() {
        $this->conn->disconnect();
    }

    public function onTransferTimeout($conn) {
        echo "onTransferTimeout\n";
        $this->conn->disconnect();
    }
    public function onConnectTimeout($conn) {
        echo "onConnectTimeout\n";
        $this->conn->disconnect();
    }

    public function onConnect($conn) {
        echo "onConnect\n";
        $connInt = $conn->getSockInt();
        $this->timerClass->addTimer("conn".$connInt, $this, "sendLines", $connInt, 0.25, false);
    }

    public function onRead($conn) {
        echo "onRead\n";
        $connInt = $conn->getSockInt();
        $line = $this->socketClass->getQueueLine($connInt);
        echo "Line: '".$line."'\n";
        $this->sendLine($connInt, "Testje2");
        return $this->socketClass->hasLine($connInt);
    }

    public function onWrite($conn) {
        echo "onWrite\n";
    }

    public function sendLine($connInt, $line) {
        $this->queue[] = $line;
    }
    
    public function sendLines() {
        //echo "sendLines\n";
        $conn = $this->conn;
        $connInt = $conn->getSockInt();
        foreach ($this->queue as $n => $line) {
            echo "Sent: ".$line."\n";
            $this->socketClass->sendSocket($connInt, $line);
            unset($this->queue[$n]);
        }
    }

    public function onDead($conn) {
        echo "onDead\n";
        $this->conn->disconnect();
    }
}
?>


By the way, if I connect with a simple php script
Code
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
$connection = socket_connect($socket, "127.0.0.1", 404);

socket_write($socket,"Testje\n");
print_r(socket_read($socket, 1000));

socket_close($socket);
?>


It works! So, what am I doing wrong?

Thanks in advance!

Last edited by spartacus at 3:46 pm, Apr 19 2009

Xavia
Post #1099
Member
Date: 10:56 am, Feb 19 2012
Posts: 3


can you tell me how your scirpt works?

i will love to use it, i have like 8 php bots :)

Do you wont some Litecoins?
Join my crew: http://funcoin.org/gettingstarted
   

Back to Module Development