Jump to content

build EXE on target machine... (hardcode some info)


jOte-

Recommended Posts

// 2>NUL||@GOTO :BATCH
/*
:BATCH
@ECHO OFF

MODE CON: COLS=120 LINES=20 BUFFER=100
COLOR 08

SET CSC=

FOR /F "TOKENS=1,2 DELIMS=#" %%a IN ('"PROMPT #$H#$E# & ECHO ON & FOR %%b IN (1) DO REM"') DO (
  SET "DEL=%%a"
)

CLS
ECHO.
CALL :H "-- REQUIREMENTS -----------------------------------------------------------------------------------"


FOR /R "%SystemRoot%\Microsoft.NET\Framework\" %%# IN ("*csc.exe") DO SET "CSC=%%#"
IF NOT DEFINED CSC (
  CALL :CE "   [!] no .net framework or csc.exe found!"
  EXIT /B 10
) ELSE (
  CALL :I "   csc.exe found"
)

CALL :H "-- COMPILING --------------------------------------------------------------------------------------"
CALL :NCE "   [-] Delete old executable file if exists."

DEL /Q /F "%~n0.exe" >NUL 2>NUL

IF NOT EXIST "%~n0.exe" (
  CALL :C "   [+] Creating new executable file."
  ECHO.
  CALL %CSC% /nologo /reference:"" /warn:0 /out:"%~n0.exe" "%~dpsfnx0" || (
      ECHO.
      CALL :CE "   [!] Creating executable file FAILED!"
      EXIT /B %errorlevel% 
  )
)

CALL :I "   Start running executable file."

CALL :H "-- START ----------------------------------------------------------------------------------------"
ECHO.

%~n0.exe %*

ECHO.
CALL :H "-- END ------------------------------------------------------------------------------------------"
ECHO.
CALL :I "   End running executable file."

TIMEOUT 10 > NUL 2>&1

ENDLOCAL & EXIT /B %errorlevel%

:COLORTEXT
ECHO OFF
<NUL SET /p ".=%DEL%" > "%~2"
FINDSTR /v /a:%1 /R "^$" "%~2" NUL
DEL "%~2" > NUL 2>&1 & ECHO.
GOTO :EOF

:I
:INFO
CALL :COLORTEXT 09 %1
GOTO :EOF

:H
:HEADER
ECHO.
CALL :COLORTEXT 0F %1
ECHO.
GOTO :EOF

:C
:CREATE
CALL :COLORTEXT 02 %1
GOTO :EOF

:NCE
:NONCRITICALERR
CALL :COLORTEXT 0D %1
GOTO :EOF

:CE
:CRITICALERR
CALL :COLORTEXT 04 %1
ECHO.
TIMEOUT 30
EXIT /B %errorlevel% 

GOTO :EOF

REM C# CODE AFTER THIS...
REM 
*/
using System;

class ftw {
  public static void Main() {
    try {
      Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + "\n");
      Console.WriteLine("--- you asked 4me?");
      Console.WriteLine("----- Compile your code on target machine...");
      Console.WriteLine("--- Most of the time Antivirus will allow that...");
    } catch (Exception e) {
      Console.WriteLine("Error: {0}", e);
    }
  }
}

 

Link to comment
Share on other sites

i had that idea running into the winscp scripting question...

// 2>NUL||@GOTO :BATCH
/*
:BATCH
@ECHO OFF

MODE CON: COLS=120 LINES=20 BUFFER=100
COLOR 08

SET CSC=

FOR /F "TOKENS=1,2 DELIMS=#" %%a IN ('"PROMPT #$H#$E# & ECHO ON & FOR %%b IN (1) DO REM"') DO (
  SET "DEL=%%a"
)

CLS
ECHO.
CALL :H "-- REQUIREMENTS -----------------------------------------------------------------------------------"


FOR /R "%SystemRoot%\Microsoft.NET\Framework\" %%# IN ("*csc.exe") DO SET "CSC=%%#"
IF NOT DEFINED CSC (
  CALL :CE "   [!] no .net framework or csc.exe found!"
  EXIT /B 10
) ELSE (
  CALL :I "   [i] csc.exe found"
)

CALL :H "-- COMPILING --------------------------------------------------------------------------------------"
CALL :NCE "   [-] Delete old executable file if exists."

DEL /Q /F "%~n0.exe" >NUL 2>NUL

IF NOT EXIST "%~n0.exe" (
  CALL :OK "   [+] Creating new executable file."
  ECHO.
  CALL %CSC% /nologo /reference:"WinSCPnet.dll" /warn:0 /out:"%~n0.exe" "%~dpsfnx0" || (
      ECHO.
      CALL :WTF "     [!] Creating executable file FAILED!"
      EXIT /B %errorlevel% 
  )
)

CALL :I "   [i] Start running executable file."

CALL :H "-- START ----------------------------------------------------------------------------------------"
ECHO.

%~n0.exe %*

ECHO.
CALL :H "-- END ------------------------------------------------------------------------------------------"
ECHO.
CALL :I "  [i] End running executable file."

TIMEOUT 10 > NUL 2>&1

ENDLOCAL & EXIT /B %errorlevel%

:COLORTEXT
:: COLOR [attr]

:: attr	Specifies color attribute of console output
:: Color attributes are specified by TWO hex digits — the first corresponds to the background; the second the foreground. 
:: Each digit can be any of the following values:

:: 0	=	Black	 	8	=	Gray
:: 1	=	Blue	 	9	=	Light Blue
:: 2	=	Green	 	A	=	Light Green
:: 3	=	Aqua	 	B	=	Light Aqua
:: 4	=	Red	 	  C	=	Light Red
:: 5	=	Purple	D	=	Light Purple
:: 6	=	Yellow	E	=	Light Yellow
:: 7	=	White	 	F	=	Bright White

ECHO OFF
<NUL SET /p ".=%DEL%" > "%~2"
FINDSTR /v /a:%1 /R "^$" "%~2" NUL
DEL "%~2" > NUL 2>&1 & ECHO.
GOTO :EOF


:I
:INFO
CALL :COLORTEXT 09 %1
GOTO :EOF

:H
:HEADER
ECHO.
CALL :COLORTEXT 0F %1
ECHO.
GOTO :EOF

:C
:CREATE
:OK
CALL :COLORTEXT 02 %1
GOTO :EOF


:NCE
:NONCRITICALERR
CALL :COLORTEXT 0D %1
GOTO :EOF

:CE
:WTF
:CRITICALERR
CALL :COLORTEXT 04 %1
ECHO.
TIMEOUT 20
EXIT /B %errorlevel% 

GOTO :EOF


*/

using System;
using WinSCP;

class ftw {
  public static int Main() {
    try {
      SessionOptions sessionOptions = new SessionOptions {
        Protocol = Protocol.Sftp,
        HostName = "192.168.*.*",
        PortNumber = 22,
        UserName = "***********************",
        Password = "********",
        SshHostKeyFingerprint = "ssh-****** 512 **:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**"
      };

      using (Session session = new Session()){
        session.FileTransferred += FileTransferred;
        session.Open(sessionOptions);
       
        SynchronizationResult synchronizationResult;
        synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Remote, @"C:\Users\unkown\Pictures\","/home/lnxuser/Pictures/", false);
        synchronizationResult.Check();
        
        CommandExecutionResult commandResult;
        commandResult = session.ExecuteCommand("/home/lnxyser/Pictures/resize.sh");
        commandResult.Check();
        
        Console.WriteLine(commandResult.Output);
      }
      
      
      return 0;
    } catch (Exception e) {
      Console.WriteLine("Error: {0}", e);
      return 1;
    }
  }
  
  private static void FileTransferred(object sender, TransferEventArgs e) {
    if (e.Error == null) {
      Console.WriteLine("Upload of {0} succeeded", e.FileName);
    } else {
      Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error);
    }
    if (e.Chmod != null) {
      if (e.Chmod.Error == null) {
        Console.WriteLine("Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
      } else {
        Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
      }
    } else {
      Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
    }
    if (e.Touch != null) {
      if (e.Touch.Error == null) {
        Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
      } else {
        Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
      }
    } else {
      Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
    }
  }
}

 

Link to comment
Share on other sites

There is a way to do this in pure powershell without call csc since it is .net.

You will have the option to compile to disk or in memory.  Since I do most stuff in memory with reflections or add-type, I hardly use it.

I have to look up the way for 2.0 Powershell but here is the "add-type" way.

http://www.ingmarverheij.com/run-and-compile-net-code-in-res-workspace-manager-using-powershell/

 

Link to comment
Share on other sites

  • 2 weeks later...

Now that is a rabbit hole but a fun one so I will let you run down it since I am all about the learning but I will point you in the direction to begin with some side notes.

Seeing you are interested in compiling .NET code, I assume you know .NET.  I bet when you saw my reply you tried to google how to compile .NET code with Powershell in memory and got information overload?  Here is a better search that I did since I, at my roots, am a C# developer.  I went the .NET route.  I searched how to programmically compile .NET code in C#.  Powershell has access to the .NET library so you can adapt .NET code to Powershell.  Key things to keep in mind.  Temp files are generated like in any compilation so you have to make sure to include the function that determines where they go and to clean them up when done.  Also, keep in mind what versions of Powershell has access to what versions of .NET.  Like 2.0 Posh can only access 2.0 .NET.

Begin here.

https://support.microsoft.com/en-us/help/304655/how-to-programmatically-compile-code-using-c-compiler

Here is the class that does it:

https://msdn.microsoft.com/en-us/library/system.codedom.compiler(v=vs.110).aspx

And here is the in memory part:

https://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.generateinmemory(v=vs.110).aspx

 

Link to comment
Share on other sites

I 'm  just interested in doing stuff in different ways....

on the payroll scripts  - if it run... ship it - 

or

the script you make for yourself!

the bad way .... or the good way

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

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