<commit_before><?php
if (!defined('BASEPATH')) {
     exit('No direct script access allowed');
}
#
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/*
 * @package Open-AudIT
 * @author <NAME> <<EMAIL>>
 *
 * @version   2.1.1
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */


if (! function_exists('windows_credentials')) {
    /**
     * The SSH credentials test. 
     *
     * @access    public
     *
     * @category  Function
     *
     * @author    <NAME> <<EMAIL>>
     *
     * @param     ip          The target device's ip address
     *
     * @param     credentials An array of credentials objects
     *
     * @param     display     Should we echo the output to te screen?
     *
     * @return    false || credentials object with an additional flag for 'sudo' and root
     */
    function windows_credentials($ip = '', $credentials = array(), $log = null)
    {
        if (empty($log)) {
            $log = new stdClass();
        }
        $log->file = 'wmi_helper';
        $log->function = 'windows_credentials';
        $log->message = 'Windows credentials starting';
        discovery_log($log);


        if (empty($credentials)) {
            $log->severity = 3;
            $log->message = 'No credentials array passed to windows_credentials.';
            discovery_log($log);
            return false;
        }
        if (empty($ip) or !filter_var($ip, FILTER_VALIDATE_IP)) {
            $log->severity = 3;
            $log->message = 'No IP passed or bad IP to windows_credentials ('. $ip . ').';
            discovery_log($log);
            return false;
        }
        foreach ($credentials as $credential) {
            if ($credential->type == 'windows') {

            $log->file = 'wmi_helper';
            $log->function = 'windows_credentials';
            $log->message = 'Testing credential set ' . $credential->name;
            discovery_log($log);

                $command = 'csproduct get uuid';
                $wmi_result = wmi_command($ip, $credential, $command, $log);
                unset($log->command);
                if ($wmi_result != false and $wmi_result['status'] == 0) {
                    $log->file = 'wmi_helper';
                    $log->function = 'windows_credentials';
                    $log->message = "Windows credentials complete. Credential set " . $credential->name . " working on " . $ip;
                    $log->severity = 6;
                    discovery_log($log);
                    unset($log->command);
                    return $credential;
                } else {
                    $log->severity = 7;
                    $log->function = 'windows_credentials';
                    $log->message = "Credential set for Windows named " . $credential->name . " not working on " . $ip;
                    discovery_log($log);
                }
            }
        }
        $log->file = 'wmi_helper';
        $log->function = 'windows_credentials';
        $log->message = 'Windows credentials complete. No working Windows credentials for ' . $ip . ' found.';
        $log->severity = 3;
        $log->severity_text = 'error';
        discovery_log($log);
        return false;
    }
}

if (! function_exists('execute_windows')) {
    /**
     * The SSH credentials test. 
     *
     * @access    public
     *
     * @category  Function
     *
     * @author    <NAME> <<EMAIL>>
     *
     * @param     ip        The target device's ip address
     *
     * @param     credentials The credentials object
     *
     * @param     command   The command to be run using SSH
     *
     * @param     display   Should we output to the screen or not?
     *
     * @return    false || $return array containing the output and status flag
     */
   function execute_windows($ip = '', $credentials, $command = '', $log)
    {

        $log->file = 'wmi_helper';
        $log->function = 'execute_windows';
        $log->command = '';

        if (empty($ip)) {
            $log->message = 'No IP supplied to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (!filter_var($ip, FILTER_VALIDATE_IP)) {
            $log->message = 'No valid IP supplied to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (!is_object($credentials)) {
            $log->message = 'No credentials passed to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if ($command == '') {
            $log->message = 'No command passed to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (php_uname('s') == 'Darwin') {
            $filepath = dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/open-audit/other";
            if (!file_exists('/usr/local/bin/winexe')) {
                $log->message = 'Winexe not installed on OSX, cannot run execute_windows.';
                discovery_log($log);
                return false;
            }
            $temp = explode('@', $credentials->credentials->username);
            $username = $temp[0];
            $domain = $temp[1];
            unset($temp);
            $command_string = 'winexe -U ' . $domain . '/' . $username . '%' . $credentials->credentials->password . ' //' . $ip . ' \'' . $command . '\'';
            $log->command = 'winexe -U ' . $domain . '/' . $username . '%****** //' . $ip . ' \'' . $command . '\'';
            exec($command_string, $return['output'], $return['status']);
        }

        if (php_uname('s') == 'Linux') {
            $filepath = dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/open-audit/other";
            $password = str_replace('$', '\$', $credentials->credentials->password);
            $password = str_replace("'", "", escapeshellarg($password));
            $username = str_replace("'", "", escapeshellarg($credentials->credentials->username));
            $temp = explode('@', $credentials->credentials->username);
            $username = $temp[0];
            $domain = $temp[1];
            unset($temp);
            // $command_string = "screen -D -m timeout 5m /usr/local/open-audit/other/winexe-static -U ".$domain . '/' . $username."%".$password." --uninstall //".$ip." \"$command\" ";
            // $log->command = "screen -D -m timeout 5m /usr/local/open-audit/other/winexe-static -U ".$domain . '/' . $username."%****** --uninstall //".$ip." \"$command\" ";
            // $echo = str_replace($password, '******', $command);
            // exec($command_string, $output, $return_var);
            if ($domain != '') {
                $domain .= '/';
            }
            $command_string = "screen -D -m timeout 5m ${filepath}/winexe-static -U \"${domain}${username}%******\" --uninstall //" . str_replace("'", "", escapeshellarg($ip))." \"$command\" ";
            $log->message = 'Attempting to execute command';
            $log->command = $command_string;
            $command_string = str_replace('******', $password, $command_string);<commit_msg>Remove unused code.<commit_after><?php
if (!defined('BASEPATH')) {
     exit('No direct script access allowed');
}
#
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************

/*
 * @package Open-AudIT
 * @author <NAME> <<EMAIL>>
 *
 * @version   2.1.1
 * @license http://www.gnu.org/licenses/agpl-3.0.html aGPL v3
 */


if (! function_exists('windows_credentials')) {
    /**
     * The SSH credentials test. 
     *
     * @access    public
     *
     * @category  Function
     *
     * @author    <NAME> <<EMAIL>>
     *
     * @param     ip          The target device's ip address
     *
     * @param     credentials An array of credentials objects
     *
     * @param     display     Should we echo the output to te screen?
     *
     * @return    false || credentials object with an additional flag for 'sudo' and root
     */
    function windows_credentials($ip = '', $credentials = array(), $log = null)
    {
        if (empty($log)) {
            $log = new stdClass();
        }
        $log->file = 'wmi_helper';
        $log->function = 'windows_credentials';
        $log->message = 'Windows credentials starting';
        discovery_log($log);


        if (empty($credentials)) {
            $log->severity = 3;
            $log->message = 'No credentials array passed to windows_credentials.';
            discovery_log($log);
            return false;
        }
        if (empty($ip) or !filter_var($ip, FILTER_VALIDATE_IP)) {
            $log->severity = 3;
            $log->message = 'No IP passed or bad IP to windows_credentials ('. $ip . ').';
            discovery_log($log);
            return false;
        }
        foreach ($credentials as $credential) {
            if ($credential->type == 'windows') {

            $log->file = 'wmi_helper';
            $log->function = 'windows_credentials';
            $log->message = 'Testing credential set ' . $credential->name;
            discovery_log($log);

                $command = 'csproduct get uuid';
                $wmi_result = wmi_command($ip, $credential, $command, $log);
                unset($log->command);
                if ($wmi_result != false and $wmi_result['status'] == 0) {
                    $log->file = 'wmi_helper';
                    $log->function = 'windows_credentials';
                    $log->message = "Windows credentials complete. Credential set " . $credential->name . " working on " . $ip;
                    $log->severity = 6;
                    discovery_log($log);
                    unset($log->command);
                    return $credential;
                } else {
                    $log->severity = 7;
                    $log->function = 'windows_credentials';
                    $log->message = "Credential set for Windows named " . $credential->name . " not working on " . $ip;
                    discovery_log($log);
                }
            }
        }
        $log->file = 'wmi_helper';
        $log->function = 'windows_credentials';
        $log->message = 'Windows credentials complete. No working Windows credentials for ' . $ip . ' found.';
        $log->severity = 3;
        $log->severity_text = 'error';
        discovery_log($log);
        return false;
    }
}

if (! function_exists('execute_windows')) {
    /**
     * The SSH credentials test. 
     *
     * @access    public
     *
     * @category  Function
     *
     * @author    <NAME> <<EMAIL>>
     *
     * @param     ip        The target device's ip address
     *
     * @param     credentials The credentials object
     *
     * @param     command   The command to be run using SSH
     *
     * @param     display   Should we output to the screen or not?
     *
     * @return    false || $return array containing the output and status flag
     */
   function execute_windows($ip = '', $credentials, $command = '', $log)
    {

        $log->file = 'wmi_helper';
        $log->function = 'execute_windows';
        $log->command = '';

        if (empty($ip)) {
            $log->message = 'No IP supplied to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (!filter_var($ip, FILTER_VALIDATE_IP)) {
            $log->message = 'No valid IP supplied to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (!is_object($credentials)) {
            $log->message = 'No credentials passed to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if ($command == '') {
            $log->message = 'No command passed to wmi_helper::execute_windows';
            discovery_log($log);
            return false;
        }

        if (php_uname('s') == 'Darwin') {
            $filepath = dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/open-audit/other";
            if (!file_exists('/usr/local/bin/winexe')) {
                $log->message = 'Winexe not installed on OSX, cannot run execute_windows.';
                discovery_log($log);
                return false;
            }
            $temp = explode('@', $credentials->credentials->username);
            $username = $temp[0];
            $domain = $temp[1];
            unset($temp);
            $command_string = 'winexe -U ' . $domain . '/' . $username . '%' . $credentials->credentials->password . ' //' . $ip . ' \'' . $command . '\'';
            $log->command = 'winexe -U ' . $domain . '/' . $username . '%****** //' . $ip . ' \'' . $command . '\'';
            exec($command_string, $return['output'], $return['status']);
        }

        if (php_uname('s') == 'Linux') {
            $filepath = dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/open-audit/other";
            $password = str_replace('$', '\$', $credentials->credentials->password);
            $password = str_replace("'", "", escapeshellarg($password));
            $username = str_replace("'", "", escapeshellarg($credentials->credentials->username));
            $temp = explode('@', $credentials->credentials->username);
            $username = $temp[0];
            $domain = $temp[1];
            unset($temp);
            if ($domain != '') {
                $domain .= '/';
            }
            $command_string = "screen -D -m timeout 5m ${filepath}/winexe-static -U \"${domain}${username}%******\" --uninstall //" . str_replace("'", "", escapeshellarg($ip))." \"$command\" ";
            $log->message = 'Attempting to execute command';
            $log->command = $command_string;
            $command_string = str_replace('******', $password, $command_string);
{'fraction_non_alphanumeric': 0.15391945114979036, 'fraction_numerical': 0.0034303138101893023, 'mean_word_length': 2.0049627791563274, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683319', 'n_tokens_mistral': 4513, 'n_tokens_neox': 4370, 'n_words': 1157}