Author Topic: [PHP] Packet Buffer  (Read 4736 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[PHP] Packet Buffer
« on: December 14, 2005, 05:50:54 pm »
Changelog
13 December 2005: Work began.
13 December 2005: Added DWORD creation.
14 December 2005: Added removal functions.
14 December 2005: Made numerous two-second bugfixes due to stupid things I forgot.

To-do
Remove WORD / DWORD.
Test.

Example - Creating SID_AUTH_INFO
Code: [Select]
insert_int32(0);
insert_void("68XIRATS");
insert_int32(0xCD);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_int32(0);
insert_string("CAN");
insert_string("Canada");
$data = return_bncs(0x50);

The Code~
Code: [Select]
<?
  /*
   * PHP Packet Buffer
   *
   * Module:  pbuffer.php
   * Author:  Joe LaFrance
   * Purpose: Constructing BNCS packets.
   *
   * Copyright (C) 2005 Joe LaFrance
  */
 
  $buffer = "";
  // This is where we'll construct the packet
 
  function clear() {
    $buffer = "";
  }
 
 
  /*
   * Create packets
  */
  /* Attaches the BNCS header to a string of data */
  function return_bncs($packetID) {
    return chr(0xFF) . chr($packetID) . make_int16(strlen($buffer)) . $data;
    clear();
  }
  /* Insert an NTString, also just called a string */
  function insert_string($data) {
    $buffer = $buffer . $data . chr(0);
  }
  /* Appends a void, also known as NonNTString */
  function insert_void($data) {
    $buffer = $buffer . $data;
  }
  /* Appends a little endian 8-bit integer (BYTE) */
  function insert_int8($data) {
    $buffer = $buffer . chr($data);
  }
  /* Appends a little endian 16-bit integer (WORD) */
  function insert_int16($data) {
    $buffer = $buffer . make_int16($data);
  }
 
  /* Append a little endian 32-bit integer (DWORD) */
  function insert_int32($data) {
    $buffer = $buffer . make_int32($data);
  }
 
 
 
  /*
   * Packet "debuffer" functions
  */
  /* Set the buffer data */
  function setbuffer($data) {
    clear();
    $buffer = $data;
  }
  /* Remove the header */
  function remove_header() {
  return substr($buffer, 0, 4);
  $buffer = substr($buffer, 4);
  }
  /* Remove a null terminated string */
  function remove_string() {
  $ntpos = strpos ($buffer, chr(0));
  $ret = substr($buffer, 0, $ntpos);
  $buffer = substr(strstr($buffer, chr(0)), 1);
  return $ret;
  }
  /* Remove a void of length $len */
  function remove_void($len) {
  $ret = substr($buffer, 0, $len);
  $buffer = substr($buffer, $len+1);
  return $ret;
  }
  /* Remove a 8-bit little endian integer (BYTE) */
  function remove_int8() {
  $ret = (int)substr($buffer, 0, 1);
  $buffer = substr($buffer, 1);
  }
  /* Remove a 16-bit little endian integer (WORD) */
  function remove_int16() {
    // stub
  }
  /* Remove a 32-bit little endian integer (WORD) */
  function remove_int32() {
    // stub
  }
 



  /*
   * Create integral data structures
  */
  /* Create a little-endian 16-bit integer (WORD) */
  function make_int16($data) {
    $A = $data % (256 ^ 1);
    $B = $data + $a;
    return chr($A) . chr($B);
  }
  /* Create a little-endian 32-bit integer (DWORD) */
  function make_int32($data) {
    $A = $data % (256 ^ 3);
    $B = $data % (256 ^ 2);
    $C = $data % (256 ^ 1);
    $D = $data - ($A + $B + $C);
    return chr($A) . chr($B) . chr($C) . chr($D);
  }
  /* Parse a little-endian 16-bit integer (WORD) */
  function get_int16() {
  // stub
  }
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32() {
  // stub
  }

?>
« Last Edit: December 14, 2005, 05:56:08 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Packet Buffer
« Reply #1 on: December 14, 2005, 07:24:41 pm »
PHP isn't VisualBasic, there's an assignment operator for appending strings.

Code: [Select]
$v = "This is my string.  ";

$v = $v . "I'm wasting keystrokes and clarity by doing this.";

$v = "I could just do this, ";

$v .= "which would make things a lot easier.";

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Packet Buffer
« Reply #2 on: December 14, 2005, 07:34:05 pm »
I personally like using x = x op y instead of x op= y.  It just looks so much cleaner to me :)

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Packet Buffer
« Reply #3 on: December 14, 2005, 07:50:42 pm »
I personally like using x = x op y instead of x op= y.  It just looks so much cleaner to me :)

It looks horrible in PHP, in my opinion.  It really depends on the situation, but in this case, I'd definitely do x op= y.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Packet Buffer
« Reply #4 on: December 14, 2005, 08:23:48 pm »
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.

I was obviously doing int16 and int32 horribly, so, port from JavaOp2 (hope it works..):

Code: [Select]
  /* Create a little-endian 16-bit integer (WORD) */
  function make_int16($w) {
  $ret = "";
    $ret = $ret . (($w & 0x00FF) >> 0);
    $ret = $ret . (($w & 0xFF00) >> 8);
    return $ret;
  }
  /* Create a little-endian 32-bit integer (DWORD) */
  function make_int32($d) {
  $ret = "";
    $ret = $ret . (($d & 0x000000FF) >> 0);
    $ret = $ret . (($d & 0x0000FF00) >> 8);
    $ret = $ret . (($d & 0x00FF0000) >> 16);
    $ret = $ret . (($d & 0xFF000000) >> 24);
    return $ret;
  }
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Packet Buffer
« Reply #5 on: December 14, 2005, 08:41:21 pm »
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.

+= is for integers.  .= is for strings.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Packet Buffer
« Reply #6 on: December 14, 2005, 10:17:36 pm »
I was wondering if its .= or += and I didn't want to take my chances. But thanks, though.

+= is for integers. .= is for strings.

O RLY? =p


Hm, I need to figure out how to do MakeDWORD and MakeWORD correctly.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Packet Buffer
« Reply #7 on: December 14, 2005, 10:19:37 pm »
O RLY? =p


Hm, I need to figure out how to do MakeDWORD and MakeWORD correctly.

You're the one that questioned the obvious in the first place; I only answered it.