修正 connect api

This commit is contained in:
sin365 2023-10-06 15:40:41 +08:00
parent 5fd7f68e65
commit 8b0a8012a3
4 changed files with 170 additions and 15 deletions

View File

@ -24,6 +24,7 @@
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -59,6 +60,7 @@
<Compile Include="Filelog.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ws2_32.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="RefLib\EasyHook.dll" />

View File

@ -94,6 +94,7 @@ namespace AxibugInject
new Dconnect(connect_Hooked),
this);
connectHook.ThreadACL.SetExclusiveACL(new int[1]);
}
catch (Exception ex)
{
@ -209,18 +210,37 @@ namespace AxibugInject
// public uint sin6_scope_id;
//}
[DllImport("Ws2_32.dll")]
public static extern int connect(IntPtr SocketHandle, ref sockaddr_in addr, int addrsize);
public static extern int connect(IntPtr SocketHandle, ref sockaddr_in_old addr, int addrsize);
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
delegate int Dconnect(IntPtr SocketHandle, ref sockaddr_in addr, int addrsize);
static int connect_Hooked(IntPtr SocketHandle, ref sockaddr_in addr, int addrsize)
delegate int Dconnect(IntPtr SocketHandle, ref sockaddr_in_old addr, int addrsize);
static int connect_Hooked(IntPtr SocketHandle, ref sockaddr_in_old addr, int addrsize)
{
ConsoleShow.Log($"connect[调用]SocketHandle->{SocketHandle} addr->{addr} addrsize->{addrsize}");
ConsoleShow.Log($"connect sockaddr_in 详情 :sin_family->{addr.sin_family} sin_addr->{addr.sin_addr} sin_port->{addr.sin_port}");
ConsoleShow.Log($"connect sockaddr_in 详情 :sin_family->{addr.sin_family} sin_addr->{addr.sin_addr}" +
$" sin_port->{GetPort(addr.sin_port)}");
/*ConsoleShow.Log($"connect sockaddr_in 详情 :sin_family->{addr.sin_family} sin_addr->{addr.sin_addr.s_b1}.{addr.sin_addr.s_b2}.{addr.sin_addr.s_b3}.{addr.sin_addr.s_b4}" +
$" sin_port->{GetPort(addr.sin_port)}");*/
// call original API...
return connect(SocketHandle, ref addr, addrsize);
}
static int GetPort(ushort Tbed)
{
if (Tbed < 256)
return Tbed;
byte gao = (byte)(Tbed >> 8);
byte di = (byte)(Tbed & 0xff);
ushort a = (ushort)(gao << 8);
ushort b = (ushort)di;
//ushort newBed = (ushort)(a | di);
ushort newT = (ushort)(gao | di << 8);
return newT;
}
#endregion

View File

@ -125,10 +125,22 @@ namespace AxibugInject
}
*/
/*
* struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
*/
/// <summary>
/// Internet socket address structure.
/// </summary>
public struct sockaddr_in
public struct sockaddr_in_old
{
/// <summary>
/// Protocol family indicator.
@ -149,6 +161,93 @@ namespace AxibugInject
//public string sin_zero;
public long sin_zero;
}
/// <summary>
/// Internet socket address structure.
/// </summary>
public struct sockaddr_in
{
/// <summary>
/// Protocol family indicator.
/// </summary>
public short sin_family;
/// <summary>
/// Protocol port.
/// </summary>
public ushort sin_port;
/// <summary>
/// Actual address value.
/// </summary>
public in_addr sin_addr;
/// <summary>
/// Address content list.
/// </summary>
[MarshalAs(UnmanagedType.LPStr, SizeConst=8)]
public string sin_zero;
//public long sin_zero;
}
[StructLayout(LayoutKind.Explicit, Size = 4)]
public struct in_addr
{
[FieldOffset(0)] internal byte s_b1;
[FieldOffset(1)] internal byte s_b2;
[FieldOffset(2)] internal byte s_b3;
[FieldOffset(3)] internal byte s_b4;
[FieldOffset(0)] internal ushort s_w1;
[FieldOffset(2)] internal ushort s_w2;
[FieldOffset(0)] internal uint S_addr;
/// <summary>
/// can be used for most tcp & ip code
/// </summary>
internal uint s_addr
{
get { return S_addr; }
}
/// <summary>
/// host on imp
/// </summary>
internal byte s_host
{
get { return s_b2; }
}
/// <summary>
/// network
/// </summary>
internal byte s_net
{
get { return s_b1; }
}
/// <summary>
/// imp
/// </summary>
internal ushort s_imp
{
get { return s_w2; }
}
/// <summary>
/// imp #
/// </summary>
internal byte s_impno
{
get { return s_b4; }
}
/// <summary>
/// logical host
/// </summary>
internal byte s_lh
{
get { return s_b3; }
}
}
/*
public enum SocketFlags
{

View File

@ -116,27 +116,61 @@ namespace AxibugRedirector
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
private static bool RegGACAssembly()
{
var dllName = "EasyHook.dll";
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (!RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
var dllPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
Thread.Sleep(100);
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep(100);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
Console.WriteLine("{0} registered to GAC successfully.", dllName);
else
{
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep(100);
Console.WriteLine("{0} registered to GAC failed.", dllName);
return false;
}
dllName = "AxibugInject.dll";
dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
if (!RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
dllPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
Thread.Sleep(100);
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep(100);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
Console.WriteLine("{0} registered to GAC successfully.", dllName);
else
{
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep(100);
Console.WriteLine("{0} registered to GAC failed.", dllName);
return false;
}
return true;
}
//private static bool RegGACAssembly()
//{
// var dllName = "EasyHook.dll";
// var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
// if (!RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
// {
// new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
// Thread.Sleep(100);
// }
// dllName = "AxibugInject.dll";
// dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
// new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
// if (!RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
// {
// new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
// Thread.Sleep(100);
// }
// return true;
//}
private static bool InstallHookInternal(int processId)
{