Last active
November 13, 2024 12:39
-
-
Save ixe013/22c9eb16540dd234030c0b2436992bbe to your computer and use it in GitHub Desktop.
A polyglot batch file/JScript.net application that listens on a port. Try it with `polyglot-listener.bat 1234` to listen on port 1234
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@if (@X)==(@Y) @end /* JScript comment | |
@echo off | |
REM Polyglot code adapted from https://stackoverflow.com/a/24396149/591064 | |
setlocal enableDelayedExpansion | |
REM Too few arguments? | |
if "x%1" == "x" goto usage_help | |
REM Find the latest JScript.Net compiler | |
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do ( | |
set "jsc=%%v" | |
) | |
REM Always rebuild | |
IF exist %APPDATA%\listener.exe del %APPDATA%\listener.exe | |
echo.Compiling the listener script using !jsc! | |
"!jsc!" /nologo /out:"%APPDATA%\listener.exe" "%~dpsfnx0" | |
IF exist %APPDATA%\listener.exe ( | |
echo.Binary saved as %APPDATA%\listener.exe | |
echo.Launching the listener on %2 %1 | |
start "Dummy server on TCP port %1" %APPDATA%\listener.exe %* | |
) else ( | |
echo.Unable to build polyglot code | |
) | |
goto end_of_batch_file | |
:usage_help | |
echo.Usage: | |
echo. %0 port [ip] | |
:end_of_batch_file | |
endlocal & exit /b %errorlevel% | |
*/ | |
import System; | |
import System.Net; | |
import System.Net.Sockets; | |
import System.Text; | |
function StartListening(port, ipAddress:IPAddress) { | |
// Establish the local endpoint for the socket. | |
// Dns.GetHostName returns the name of the host running the application. | |
var localEndPoint = new IPEndPoint(ipAddress, parseInt(port)); | |
// Create a TCP/IP socket. | |
var listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp ); | |
// Bind the socket to the local endpoint and listen for incoming connections. | |
try { | |
listener.Bind(localEndPoint); | |
listener.Listen(10); | |
// Start listening for connections. | |
while (true) { | |
// Data buffer for incoming data. | |
var data:byte[] = new byte[1024]; | |
Console.WriteLine("Waiting for a TCP connection on {0}:{1}...", ipAddress, port); | |
var handler = listener.Accept(); | |
Console.WriteLine("Connected to {0}", handler.RemoteEndPoint); | |
try { | |
while (handler.Receive(data) > 0); | |
} finally { | |
Console.WriteLine("Disconected\n"); | |
} | |
handler.Shutdown(SocketShutdown.Both); | |
handler.Close(); | |
} | |
} catch (e) { | |
Console.WriteLine(e.ToString()); | |
} | |
} | |
function GetThisHostIPv4Address() { | |
var ipAddress = IPAddress.Parse("127.0.0.1"); // Default to localhost | |
var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); | |
//Let's see if we have something in the DNS that looks like | |
//an IPv4 address | |
for(var i=0; i<ipHostInfo.AddressList.length; ++i) { | |
if (ipHostInfo.AddressList[i].AddressFamily == AddressFamily.InterNetwork) { | |
ipAddress = ipHostInfo.AddressList[i]; | |
break; | |
} | |
} | |
return ipAddress; | |
} | |
//----- MAIN --------- | |
var arguments:String[] = Environment.GetCommandLineArgs(); | |
if (arguments.length == 2) { | |
StartListening(arguments[1], GetThisHostIPv4Address()); | |
} else if (arguments.length == 3) { | |
StartListening(arguments[1], IPAddress.Parse(arguments[2])); | |
} else { | |
Console.WriteLine("Usage: listener port [ip]"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment