Created
April 15, 2021 23:04
-
-
Save vxcute/30b1ea4ab792c1395e8c9cb8e92c384f to your computer and use it in GitHub Desktop.
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
// client | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Collections; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Net.Sockets; | |
using System.Net; | |
using System.Runtime.InteropServices; | |
namespace poc_client | |
{ | |
class Client | |
{ | |
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] | |
static extern IntPtr GetProcAddress(IntPtr hModule, string procName); | |
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] | |
public static extern IntPtr GetModuleHandle(string lpModuleName); | |
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
private delegate bool IsDbg(); | |
static void Main(string[] args) | |
{ | |
Connect2Server("127.0.0.1", "Send Me APIs"); | |
} | |
public static List<string> StringToList(string data) | |
{ | |
var list = new List<string>(); | |
foreach (var str in data.Split()) | |
list.Add(str); | |
return list; | |
} | |
static void Connect2Server(String server, String message) | |
{ | |
try | |
{ | |
Int32 port = 30333; | |
TcpClient client = new TcpClient(server, port); | |
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); | |
NetworkStream stream = client.GetStream(); | |
data = new Byte[256]; | |
String apis = String.Empty; | |
Int32 bytes = stream.Read(data, 0, data.Length); | |
apis = System.Text.Encoding.ASCII.GetString(data, 0, bytes); | |
List<string> APIs = StringToList(apis); | |
IntPtr ModHandle = GetModuleHandle("kernel32.dll"); | |
IntPtr Proc = GetProcAddress(ModHandle, APIs[0]); | |
IsDbg Isdbg = (IsDbg)Marshal.GetDelegateForFunctionPointer(Proc, typeof(IsDbg)); | |
if (Isdbg()) | |
Console.WriteLine("Under A Debugger Ouch"); | |
stream.Close(); | |
client.Close(); | |
} | |
catch (ArgumentNullException e) | |
{ | |
Console.WriteLine("ArgumentNullException: {0}", e); | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException: {0}", e); | |
} | |
Console.WriteLine("\n Press Enter to continue..."); | |
Console.Read(); | |
} | |
} | |
} | |
// Server | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
class Server | |
{ | |
public static void Main() | |
{ | |
server(); | |
} | |
public static void server() | |
{ | |
TcpListener server = null; | |
string[] apis = new string[] | |
{ | |
"IsDebuggerPresent", | |
"WriteProcessMemory", | |
"ReadProcessMemory", | |
"LoadLibraryA" | |
}; | |
try | |
{ | |
Int32 port = 30333; | |
IPAddress ip = IPAddress.Parse("127.0.0.1"); | |
server = new TcpListener(ip, port); | |
server.Start(); | |
Byte[] bytes = new Byte[256]; | |
while (true) | |
{ | |
Console.Write("Waiting for a connection... "); | |
TcpClient client = server.AcceptTcpClient(); | |
Console.WriteLine("Connected!"); | |
NetworkStream stream = client.GetStream(); | |
using (StreamWriter writer = new StreamWriter(stream)) | |
{ | |
foreach (var api in apis) | |
writer.WriteLine(api); | |
} | |
client.Close(); | |
} | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException: {0}", e); | |
} | |
finally | |
{ | |
server.Stop(); | |
} | |
Console.WriteLine("\nHit enter to continue..."); | |
Console.Read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment