Jump to content

shell_exec pass $_GET variable to .EXE


Derpitis

Recommended Posts

I'm trying to take info from $_GET and pass it along to an exe file that needs 1 argument supplied to it, and used the input from $_GET to be that argument but it's executing the .exe and NOT passing the variable, it then hangs the .exe till I end process.

 

index form for sending:




    <form action="/test.php" method="get">
    <input type="text" name="input" />


    <input type="submit" value=" OK " />


 

the php script that it sends to.

 

  

 



      <html>
    <head><title>PHP Get Send</title></head>
    
    <body>
    
    <?php
    $var = $_GET['input'];
    
    $x = shell_exec("test.exe ".$var);
    
    echo $x;
    ?>
    
    
    </body>
    
    </html>


 

I can't figure this out, at all. It just refuses to properly execute the exe with the variable sent. I can do it from command prompt, etc, a bat file, but soon as i try and do it from PHP it hangs the exe and doesn't send the variable.

 

Please do not post about "You aren't sanitizing your inputs" just to get free karma/reputation or whatever this site uses, obviously this is made purely for debugging why this doesn't work. I got dozens of replies on stack overflow and none of which were about my issue, just people farming positive rep by all saying "sanitize your exec!!!!!!!"

 

 

The process opens and just hangs there not doing anything till I end the process.

Edited by Derpitis
Link to comment
Share on other sites

Not that it matters but this is the exe it's just an autohotkey exe.

%text% = %1%
WinWait, UltimateScape | Dagonoth,
IfWinNotActive, UltimateScape | Dagonoth , WinActivate, UltimateScape | Dagonoth
WinWaitActive, UltimateScape | Dagonoth 
Send, {SHIFTDOWN};;{SHIFTUP}yell{SPACE}%text%.{ENTER}

And that works flawlessly outside of PHP compiled to an exe

Edited by Derpitis
Link to comment
Share on other sites

Ive removed the the need for the exe to be passed a variable and the exe still hangs... strange

<html>
<head><title>PHP Get Send</title></head>

<body>

<?php
$var = $_GET['input'];
$myfile = fopen("yell.txt", "w");
$txt = "{$var}";
fwrite($myfile, $txt);
fclose($myfile);
exec("test2.exe");

?>

</body>

</html>

The exe now just  reads from the text file and types it out. But launching it from php make sit hang.

 

heres the exe code.

WinWait, UltimateScape | Dagonoth, 
IfWinNotActive, UltimateScape | Dagonoth, , WinActivate, UltimateScape | Dagonoth, 
WinWaitActive, UltimateScape | Dagonoth, 
FileRead, text, yell.txt
Send, {SHIFTDOWN};;{SHIFTUP}yell{SPACE}%text%{ENTER}
exit

Link to comment
Share on other sites

I've now re-written the exe thinking maybe that was the issue and moved off of AHK onto C#, the issue still occures where when PHP launches the exe it doesn't work.

 

PHP code.

<html>
<head><title>PHP Get Send</title></head>

<body>

<?php
$var = $_GET['input'];
$myfile = fopen("yell.txt", "w");
$txt = "::yell {$var}";
fwrite($myfile, $txt);
fclose($myfile);
shell_exec("yell.bat");

?>

</body>

</html>

C# Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        [DllImport("user32.dll")]
        public static extern Int32 SetForegroundWindow(int hWnd);

        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        private void BringToFront(string className, string CaptionName)
        {
            SetForegroundWindow(FindWindow(className, CaptionName));
        }
           public System.Timers.Timer MyTimer { get; set; }
    int counter;

        private void Form1_Load(object sender, EventArgs e)
        {
            
        MyTimer = new System.Timers.Timer();
        MyTimer.Interval = 1000;
        MyTimer.Elapsed+=new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
        MyTimer.Start();

            System.IO.StreamReader myFile =
               new System.IO.StreamReader("c:\\wamp\\www\\yell.txt");
            string text = myFile.ReadToEnd();
            BringToFront("SunAwtFrame", "UltimateScape | Dagonoth");
            SendKeys.Send(text);
            SendKeys.Send("{enter}");
        }
 void  myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (++counter == 5)
        {
            System.Windows.Forms.Application.Exit();
        }
    }

    }
}

Link to comment
Share on other sites

Hi and welcome to phpfreaks.  I expect there will be a few hurt feelings from your comparison to that other site - which many here dislike for reasons including, but not limited to, the one you have given. :-) We don't operate like that here, and while there is the option to like a post, it's more akin to a facebook like than an upvote.  We are generally a far more altruistic community than that other place.

 

That aside, onto the problem at hand: do you get the same behaviour using system(), exec() and passthru()? do you have access to the task manager to see if the exe is actually loading when called with PHP, and if it is, is it using the expected memory?

Link to comment
Share on other sites

Yeah the same thing happens with exec, passthru etc, all methods of launching the exe. The exe launches according to task manager but uses 3.5mb memory flat, every time. (when it should use around 7.5mb) it never changes how much memory it uses when it hangs after being told to launch by PHP. Oh I've also tried running apache in user mode, instead of as SYSTEM. Same deal so I know its not a permissions/user interaction problem. For now I solved it by making a windows task to open the exe every 2 minutes but this isnt ideal as I would prefer it to jsut open the moment the PHP writes to the text file.

Edited by Derpitis
Link to comment
Share on other sites

It sounds a lot like it's not catching the hook to your MMO.  I don't think there is any specific fault in the PHP.  Although the best test would be to just make a bat file that outputs the contents directly to a text file like this:

@echo off
echo %* > C:\users\%username%\Desktop\test.txt
echo "Write Complete to C:\users\%username%\Desktop\test.txt"

Edit: OK, seem to lost the second half of post.  The point here is to establish where the problem lies, if it is with the PHP or with the app you are calling.  calling this with your original PHP code should get you the write complete message in the browser and the test.txt file on your desktop

Edited by Muddy_Funster
Link to comment
Share on other sites

The PHP file does catch the input from the user and save it right when I try. And I already knew PHP is at fault because I can launch the exe manually a thousand times and it never fails or hangs.

 

I ghetto fix'd this for the time being where I created a windows task to just run the exe every 120 seconds. So it'll open the exe, the exe reads the txt file and outputs it into the games chat.

 

So I guess it's resolved but not properly and it's not all that great, just because using PHP's methods of launching the exe decides to hang it.

 

No idea why.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.