0

i have this script in python:

LOGIN_TEMPLATE = b'\xa0\x00\x00\x60%b\x00\x00\x00%b%b%b%b\x04\x01\x00\x00\x00\x00\xa1\xaa%b&&%b\x00Random:%b\r\n\r\n'
GET_SERIAL = b'\xa4\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
             b'\x00\x00\x00\x00\x00\x00\x00'
GET_CHANNELS = b'\xa8\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
               b'\x00\x00\x00\x00\x00\x00\x00\x00'
GET_SNAPSHOT = b'\x11\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
               b'\x00\x00\x00\n\x00\x00\x00%b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
               b'\x00\x00%b\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

TIMEOUT = 10

  self.serial = ''
        self.channels_count = -1
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(TIMEOUT)
        self.socket.connect((ip, port))
        self.socket.send(LOGIN_TEMPLATE % (struct.pack('b', 24 + len(login) + len(password)), login.encode('ascii'),
                                           (8 - len(login)) * b'\x00', password.encode('ascii'),
                                           (8 - len(password)) * b'\x00', login.encode('ascii'),
                                           password.encode('ascii'), str(int(time.time())).encode('ascii')))
        data = self.socket.recv(128)

there is a way to convert in php ?

i create an example of what i try.

$login = 'admin';
$password ='admin';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    define('LOGIN_TEMPLATE', '\xa0\x00\x00\x60%b\x00\x00\x00%b%b%b%b\x04\x01\x00\x00\x00\x00\xa1\xaa%b&&%b\x00Random:%b\r\n\r\n');

    if (!is_resource($socket)) {
        echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
    }

    if (!socket_bind($socket, '11.11.11.11', 1024)) {
        echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
    }

    $rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);

    if ($rval === false) {
        echo 'Unable to get socket option: '. socket_strerror(socket_last_error()) . PHP_EOL;
    } else if ($rval !== 0) {
        echo 'SO_REUSEADDR is set on socket !' . PHP_EOL;
    }


    $len = strlen(LOGIN_TEMPLATE); // how to insert login e password ??


    $sendMsg = socket_send($socket, LOGIN_TEMPLATE, $len, MSG_DONTROUTE);// how to insert login e password ??

    $buf = '';
    if (false !== ($bytes = socket_recv($socket, $buf, 128, MSG_WAITALL))) {
        echo "Read $bytes bytes from socket_recv(). Closing socket...";
    } else {
        echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
    }

I tried to understand how to generate the LOGIN_TEMPLATE.   but I don't understand how to interpret python code on how to generate it. can anyone help me understand how to create it?

the original python script is here: https://gitlab.com/camshift/dahua-scanner/blob/master/utils/dahua.py

1 Answer 1

1

Instead of

define('LOGIN_TEMPLATE', '\xa0\x00\x00\x60%b\x00\x00\x00%b%b%b%b\x04\x01\x00\x00\x00\x00\xa1\xaa%b&&%b\x00Random:%b\r\n\r\n');

write

define('LOGIN_TEMPLATE', "\xa0\x00\x00\x60%c\x00\x00\x00%-'\x008s%-'\x008s\x04\x01\x00\x00\x00\x00\xa1\xaa%s&&%s\x00Random:%d\r\n\r\n");
  • double quoted to get the escape sequences expanded
  • Python's conversion specifier %b replaced as appropriate

This is just a template which needs to be expanded to a concrete login data packet, let's name it $LOGIN, with login name and password in $login and $password, respectively:

$LOGIN = sprintf(LOGIN_TEMPLATE, 24 + strlen($login) + strlen($password)
                               , $login, $password, $login, $password, time());
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! it works and only one question : GET_CHANNELS = b'\xa8\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00' GET_SNAPSHOT = b'\x11\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00\n\x00\x00\x00%b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \ how to convert in php ? i don't know what is the "\" on end line
See "What is the purpose of a backslash at the end of a line?". It allows for nicer formatting by splitting long statements. In addition, the byte-strings on the line with `\` and the following are concatenated to one, as if both were enclosed in just one pair of quotes.
In PHP, you could also put the string parts into one string literal, or use the concatenation operator . between them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.