running bat file from php to add user to windows - php

PHP file
<?php
echo exec("rules.bat");
?>
BAT file
net user username Welcome12! /ADD /FULLNAME:Test /PASSWORDCHG:NO
When I try to run the php script I get the following
C:\inetpub\wwwroot\e\sso\home>net user username Welcome12! /ADD /FULLNAME:Test /PASSWORDCHG:NO
But no user is added. I can run the BAT file from the same directory and the user is added. I've also tried to run the following in my php script and it doesn't work
<?php
echo exec("net user username Welcome12! /ADD /FULLNAME:Test /PASSWORDCHG:NO");
?>
I've also tried using system, shell_exec, passthru
When I run
echo exec("whoami");
it shows my user that has admin rights

What windows version?
Does it have Admin Rights? as to add a user to windows you will have to run in Admin mode / Root Rights.
Even running net user username Welcome12! /ADD /FULLNAME:Test /PASSWORDCHG:NO
in Command, returns: Access is denied.
Web server has 1User with rights, When logged into the Device i.e. RDP you possibly have Admin rights.
Stating the obvious here, But this is what it sounds to me

echo exec(" start cmd /c net user $directoryname $password /ADD /FULLNAME:$username /PASSWORDCHG:NO");
this resolved it

Related

Creating Windows 2019 User from PHP using command line

I have a requirement to add a user to my windows 2019 web server from a PHP web application
I am using a command line command:
NET USER sbarker mypassword /ADD /FULLNAME:"Sue Barker" /PASSWORDCHG:NO /PASSWORDREQ:YES /LOGONPASSWORDCHG:NO /EXPIRES:NEVER
This works fine from a standard command line when logged on as administrator on the server (as you would expect).
To implement this from PHP I am using the following, which I believe to be the correct syntax/format:
<?php
$username = "sbarker";
$userpass = "secure1";
$fullname = "Sue Barker";
echo exec("start cmd /C:\Users\Administrator NET USER $username $userpass /ADD /FULLNAME:$fullname /PASSWORDCHG:NO /PASSWORDREQ:YES /LOGONPASSWORDCHG:NO /EXPIRES:NEVER");
?>
This produces no new user, probably because of permissions.
What permissions do I need to set to allow user creation?
What are my liabilities from a security point of view?
Is there a better way of creating a windows user from a PHP web application.
EDIT:
Tried the following with no success:
<?php
$test = shell_exec('C:\\WINDOWS\\system32\\cmd.exe /c 2>&1 "NET USER test test /ADD"');
echo "<pre>$test</pre>";
?>
The result was a System error 5 has occurred. Access is denied. error
After extensive research and dead ends, it looks like Microsoft has locked this down to such an extent that programmatically adding users is not possible. Personally I wish they would give developers the choice to execute batch scripts with elevated privileges rather than 'just saying no'.
The basic answer is that it looks like this is not possible.

Trying to run bash command in windows /usr/bin/env: ‘php’: Permission denied

This is my command that I've made but I get an error , I'm on WIN10
The error says that the user account that tries to run the command on that system has not the permissions / rights to do so. Please check, what user is trying to run that command and either give appropriate rights or create / change the permissions on that file.

How to run Shell Script/AppleScript in PHP?

I'm trying to run AppleScript in PHP. The following is my code:
<?php
echo "Hello World!";
shell_exec('osascript -e \'tell app "Finder" to make new Finder window\'');
shell_exec('osascript -e \'display dialog "hello" \'');
shell_exec('osascript ~/openExcel.scpt');
echo "DONE";
?>
When I run each command individually in terminal, I can get the expected result. However, when I open this .php file in server as a webpage, I can only get "Hello World!DONE". The middle part (3 exec commands) have no output result at all. I don't understand.
As far as I know you can't execute AppleScript from a webserver for security reasons. The problem is that AppleEvents can only be send through the window server by the user that's only currently logged in as the windowserver user (console user) or as root. By default apache will run as _www as it's shells and _www user can't run osascript on the command line.
The workaround is running the shell command as console user or as root.
Create an AppleScript Application and save it with the app extension, then link from the PHP page. I use it on some projects in localhost, it works.

shell_exec launch firefox from remote client

Trying to launch a Firefox window in Centos/RHEL5 with Gnome via a PHP. I want the ability to take screenshots of the requested page.
Gave my WWW user sudo rights and confirmed this is working on the server, firefox window opens successfully if the php script is run on the server (as WWW user, hence the whoami echo to confirm user I am running as), however if the PHP page is called from a remote user the firefox window does not open, however the whoami returns the WWW username.
<?php
$page= 'index.html';
$launch= "sudo /usr/bin/firefox \"http://another.server.com/".$page."\"";
echo shell_exec('whoami');
echo shell_exec($launch);
?>
The WWW user login shell is /bin/bash and no pass required. Running on a private network, not concerned about the sudo access to the WWW user

php problem to execute CutyCapt command

i'm having this problem
sh: CutyCapt: Permission denied
my php code is
<?php
echo exec('CutyCapt --url=http://www.google.com --out=/var/www/google.png --javascript=on 2>&1');
?>
When calling an executable from PHP, it is called with the permissions of the user PHP runs as (often the Apache server, for example).
That user account does not have permission to call that executable - probably because it belongs to a different user, and has the "executable" bit only for that user or group.
That's all that can be said for sure without more information.

Categories