Jump to content

1 step to end!


Pirosoft
 Share

Recommended Posts

Hi, i have created a script that create a service, this service wait on a specific port, a program send a string command in tcp socket and the service, if file .exe exist, run the program.

My problem is that the service can run the program only with RunAs function and need the user password. I want to run the program without use the user password, but the function Run don't go!

i send for example "C:\Programmi\Microsoft Office\Office12\WINWORD.EXE|D:\test.doc", the service split with pipe "|" and run

"C:\Programmi\Microsoft Office\Office12\WINWORD.EXE D:\test.doc"

this is my script:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

#include<Service.au3>
$sServiceName = "Acommand"
Global $ConnectedSocket = -1
Global $MainSocket

If $cmdline[0] > 0 Then
    Switch $cmdline[1]
        Case "install", "-i", "/i"
            InstallService()
        Case "remove", "-u", "/u", "uninstall"
            RemoveService()
        Case Else
            ConsoleWrite(" - - - Help - - - " & @crlf)
            ConsoleWrite("params : " & @crlf)
            ConsoleWrite("  -i : install service" & @crlf)
            ConsoleWrite("  -u : remove service" & @crlf)
            ConsoleWrite(" - - - - - - - - " & @crlf)
            Exit
            ;start service.
    EndSwitch
EndIf

_Service_init($sServiceName)

func main()
    Local $g_IP, $RogueSocket, $GOOEY, $edit, $input, $butt, $msg, $file, $szIP_Accepted
    Local $ret, $recv, $lenstr, $diffstr, $progstr, $pid

    Local $sUserName = "myusername"
    Local $sPassword = "mypassword"
    
    Local $g_IP = "127.0.0.1" ;@IPAddress1
    Local $nPORT = 33891

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ;Create a Listening "SOCKET"
    ;==============================================
    $MainSocket = TCPListen($g_IP, 33891, 100)
    If $MainSocket = -1 Then Exit
    $RogueSocket = -1
    
    ;==============================================
    While 1
        
        If $RogueSocket > 0 Then
            $recv = TCPRecv($RogueSocket, 2048)
            If Not @error Then
                TCPCloseSocket($RogueSocket)
                $RogueSocket = -1
            EndIf
        EndIf
        
        
        If $ConnectedSocket = -1 Then
            $ConnectedSocket = TCPAccept($MainSocket)
            If $ConnectedSocket < 0 Then
                $ConnectedSocket = -1
            Else                
                $szIP_Accepted = SocketToIP($ConnectedSocket)
                $file = FileOpen("C:\test.txt", 1);
                fileWrite($file, "IP: " & $szIP_Accepted & " > " & "Client connesso" & @CRLF);
                FileClose($file);
            EndIf

            ; If connected try to read some data
            ;--------------------
        Else
            ; EXECUTE AN UNCONDITIONAL ACCEPT IN CASE ANOTHER CLIENT TRIES TO CONNECT
            ;----------------------------------------------------------------
            $RogueSocket = TCPAccept($MainSocket)
            If $RogueSocket > 0 Then
                TCPSend($RogueSocket, "~~rejected")
            EndIf
            $szIP_Accepted = SocketToIP($ConnectedSocket)
            $recv = TCPRecv($ConnectedSocket, 2048)
            
            
            If $recv <> "" And $recv <> "~~bye"  Then
                $file = FileOpen("C:\test.txt", 1);
                fileWrite($file, "IP: " & $szIP_Accepted & " > " & $recv & @CRLF);
                
                $var = StringLeft($recv, 5)
                fileWrite($file, "IP: " & $szIP_Accepted & " var > " & $var & @CRLF);
                If ($var == "exec ") Then 
                    $lenstr = StringLen($recv)
                    $diffstr = $lenstr - 5
                    $progstr = StringRight($recv, $diffstr);
                    
                    ;Run ($progstr);                    
                    $arrstr = StringSplit($progstr, "|")
                    If FileExists($arrstr[1]) Then              
                        ;Run a command prompt as the user.      
                        RunAs($sUserName, @ComputerName, "", 0, $arrstr[1] & " " & $arrstr[2], @SystemDir)
                    EndIf

                    fileWrite($file, "IP: " & $szIP_Accepted & " > lunghezza " & $lenstr & " " & $diffstr & " stringa " & $progstr & @CRLF);
                EndIf
                FileClose($file);

            ElseIf @error Or $recv = "~~bye"  Then
                ; ERROR OCCURRED, CLOSE SOCKET AND RESET ConnectedSocket to -1              
                $file = FileOpen("C:\test.txt", 1);
                fileWrite($file, "IP: " & $szIP_Accepted & " > " & "Client disconnesso" & @CRLF);
                FileClose($file);
                TCPCloseSocket($ConnectedSocket)
                $ConnectedSocket = -1
            EndIf
        EndIf
            
    WEnd
EndFunc


;SERVICE FUNCTION
Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "Acommand", '"' & @ScriptFullPath & '"')
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
        _SelfRun($sServiceName,"start")
    EndIf
    Exit
EndFunc   ;==>InstallService

Func RemoveService()
    _StopService("", $sServiceName)
    _DeleteService("", $sServiceName)
    if not @error then ConsoleWrite("service removed successfully" & @crlf)
    Exit
EndFunc   ;==>RemoveService

Func _SelfRun($servicename,$action)
    $sCmdFile = 'sc ' & $action & ' "' & $servicename & '"' & @CRLF & 'del ' & @TempDir & '\runsvc.bat'
    FileWrite(@TempDir & "\runsvc.bat", $sCmdFile)
    Run(@TempDir & "\runsvc.bat", @TempDir, @SW_HIDE)
    Exit
EndFunc

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP
Link to comment
Share on other sites

I have written a similar program that ran as a service on many computers.. i think i used

ShellExecute() as this gave me more flexibility with the commands i could send the client.. i could not only execute programs but also run shell commands..

Ok, interesting! but if you want launch word, or openoffice, you can? Can you post the code of your program?

thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...