mnestorov / php-obfuscator

PHP Obfuscator is a command-line tool build with Python to obfuscate PHP source code files.
MIT License
31 stars 14 forks source link

It doesn't work and return errors #3

Closed rikhtehgaran closed 1 week ago

rikhtehgaran commented 2 weeks ago

i make test.php with this code:

<?php

class Database {
    private $pdo;

    public function __construct($dbname) {
        $this->pdo = new PDO("sqlite:$dbname");
        $this->createTable();
    }

    private function createTable() {
        $this->pdo->exec("CREATE TABLE IF NOT EXISTS books (
            id INTEGER PRIMARY KEY,
            title TEXT,
            author TEXT,
            year INTEGER
        )");
    }

    public function insertBook($title, $author, $year) {
        $stmt = $this->pdo->prepare("INSERT INTO books (title, author, year) VALUES (?, ?, ?)");
        $stmt->execute([$title, $author, $year]);
    }

    public function getBooks() {
        $stmt = $this->pdo->query("SELECT * FROM books");
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

function writeToFile($filename, $content) {
    file_put_contents($filename, $content);
}

function readFromFile($filename) {
    return file_get_contents($filename);
}

function formatBook($book) {
    return "Title: " . $book['title'] . ", Author: " . $book['author'] . ", Year: " . $book['year'];
}

$books = [
    ['title' => '1984', 'author' => 'George Orwell', 'year' => 1949],
    ['title' => 'To Kill a Mockingbird', 'author' => 'Harper Lee', 'year' => 1960]
];

$db = new Database('books.db');
foreach ($books as $book) {
    $db->insertBook($book['title'], $book['author'], $book['year']);
}

$allBooks = $db->getBooks();
$content = "";
foreach ($allBooks as $book) {
    $content .= formatBook($book) . "<br>";
}
writeToFile('books.txt', $content);

echo readFromFile('books.txt')."<br>";

    if (!extension_loaded('bolt')) {
        die('<b>Error:</b> The bolt extension is not loaded!');
    }
    define('4deeec90209f3805f67be0ec', '15290e997112a8a063dddb61');

now run this : image

mrg@mrg-HP:/mnt/My/Home/www/Test/obfuscator/php-obfuscator$ ./start.sh 
Welcome to the PHP Obfuscator!
Follow the prompts to obfuscate your PHP files.

Choose the mode for obfuscating your PHP files:
1: Single file
2: Multiple files
3: Entire project directory
Enter the mode number (1/2/3): 1
Enter the output directory path: /home/mrg/www/Test/encode/0/out
Enter file or directory paths to exclude (separated by a space) (you can skip this step): 
Create backups of original PHP files? (y/n): y
Choose the obfuscation options:
Obfuscate variables? (y/n): y
Obfuscate functions? (y/n): y
Obfuscate classes? (y/n): y
Enter the PHP file path: /home/mrg/www/Test/encode/0/test.php
Error while obfuscating /home/mrg/www/Test/encode/0/test.php: expected str, bytes or os.PathLike object, not list
mrg@mrg-HP:/mnt/My/Home/www/Test/obfuscator/php-obfuscator$ ./start.sh 
Welcome to the PHP Obfuscator!
Follow the prompts to obfuscate your PHP files.

Choose the mode for obfuscating your PHP files:
1: Single file
2: Multiple files
3: Entire project directory
Enter the mode number (1/2/3): 3
Enter the output directory path: /home/mrg/www/Test/encode/0/out
Enter file or directory paths to exclude (separated by a space) (you can skip this step): 
Create backups of original PHP files? (y/n): y
Choose the obfuscation options:
Obfuscate variables? (y/n): y
Obfuscate functions? (y/n): y
Obfuscate classes? (y/n): y
Enter the project directory path: /home/mrg/www/Test/encode/0
Obfuscating:   0%|                                                                                                                                           | 0/1 [00:00<?, ?file/s]
Error while obfuscating /home/mrg/www/Test/encode/0/test.php: expected str, bytes or os.PathLike object, not list                                            | 0/1 [00:00<?, ?file/s]
Obfuscating: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 875.27file/s]
rikhtehgaran commented 2 weeks ago

Also, i repeated the path with the quotation it returned invalid path error: image

rikhtehgaran commented 2 weeks ago

i have done some changes: in config file I put this:

# Configure colors for printing messages
YELLOW = '\033[93m'
BLUE = '\033[94m'
GREEN = '\033[92m'
RED = '\033[91m'
RESET = '\033[0m'

# Configure YakPro packages path
YAKPRO = "/home/mrg/www/Test/obfuscator/new/php-obfuscator/yakpro-po/yakpro-po.php"
# Log filename
log_filename = 'app.log'

Also, I changed this method:

def obfuscate_php(input_file, obfuscation_options, create_backup, output_directory):
    if create_backup:
        backup_file = f"{os.path.splitext(input_file)[0]}_backup.php"
        shutil.copy2(input_file, backup_file)
        logging.info(f"Created backup: {backup_file}")

    try:
        # Create the directory if it doesn't exist
        os.makedirs(output_directory, exist_ok=True)

        output_file = os.path.join(output_directory, f"obfuscated_{os.path.basename(input_file)}")
        logging.info(f"Obfuscating {input_file}")

        command = ["php", YAKPRO] + obfuscation_options + ["-o", output_file, input_file]
        call(command)

        print(f"{GREEN}Obfuscated file saved as {output_file}{RESET}")
        logging.info(f"Obfuscated {input_file} successfully")

    except Exception as e:
        logging.error(f"Error while obfuscating {input_file}: {e}")
        print(f"{RED}Error while obfuscating {input_file}: {e}{RESET}")

And I do this:

$ git clone --branch 4.x https://github.com/nikic/PHP-Parser.git
$ cd /home/mrg/www/Test/obfuscator/new/php-obfuscator/yakpro-po/PHP-Parser
$ git checkout 4.x

Now I get this output: image

$ python3 main.py
Welcome to the PHP Obfuscator!
Follow the prompts to obfuscate your PHP files.

Choose the mode for obfuscating your PHP files:
1: Single file
2: Multiple files
3: Entire project directory
Enter the mode number (1/2/3): 1
Enter the output directory path: /home/mrg/www/Test/encode/0
Enter file or directory paths to exclude (separated by a space) (you can skip this step): 
Create backups of original PHP files? (y/n): y
Choose the obfuscation options:
Obfuscate variables? (y/n): y
Obfuscate functions? (y/n): y
Obfuscate classes? (y/n): y
Enter the PHP file path: /home/mrg/www/Test/encode/test.php
Info:   Using [/mnt/My/Home/www/Test/obfuscator/new/php-obfuscator/yakpro-po/yakpro-po.cnf] Config File...
Info:   yakpro-po version = 2.0.14
Error:  Too much parameters are specified, I do not know how to deal with that!!!
Obfuscated file saved as /home/mrg/www/Test/encode/0/obfuscated_test.php

But no file was created. It only take a backup

mnestorov commented 2 weeks ago

@rikhtehgaran thank you for your feedback. Later today I will check and release a fix.

mnestorov commented 1 week ago

@rikhtehgaran the errors you are encountering seem to stem from two primary issues:

Invalid command type: we are constructing the YAKPRO command as a list, which is correct. However, when we pass this list to subprocess.call(), the list should contain only strings. If any element of the list is not a string, Python will raise an error similar to expected str, bytes or os.PathLike object, not list.

Improper handling of file paths: The error message regarding invalid file paths may be a result of incorrectly handling quoted file paths or improper input validation. If file paths contain spaces or special characters, they need to be handled appropriately.

I've updated the code, thinking the bugs are fixed. Please, check this commit: ee6d727