Coder Social home page Coder Social logo

noankama's Introduction

NoAnkama

How to use ?

First of all, you have to initialize the class by passing informations about the process. You have 3 differents ways to create the class.

WinsockHook winsockHook = new WinsockHook("Dofus"); // Here the process name is Dofus, so the class will get its handle.
WinsockHook winsockHook = new WinsockHook(1234); // Here the process id is 1234, so the class will find the process which it correspond and get its handle.
WinsockHook winsockHook = new WinsockHook(process); // Here the process is directly passed to the constructor, so it will get its handle.

You have to specify the IP address you want to blacklist, and the IP address on which you want to redirect the connection.

winsockHook.SourceIPs = new List<System.Net.IPAddress>
{
  System.Net.IPAddress.Parse("213.248.126.40"),
  System.Net.IPAddress.Parse("213.248.126.41"),
};
winsockHook.RemoteIP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 5555);

Then to hook the function you have just to do this:

winsock.Hook();

You can also Unhook the function by doing this:

winsock.Unhook();

The class will detect automatically if the function is already hooked. If the property

public bool AllowReplace { get; set; }

is not set to True then, the function won't be hooked because it will cause crash.

I add some events to get some informations about the hook.

public class WinsockHookEventArgs : EventArgs
{

  public WinsockHookEventArgs(Process process, IntPtr apiAdd, IntPtr hookAdd, IntPtr continueAdd)
  {
    Process = process;
    ApiAdd = apiAdd;
    HookAdd = hookAdd;
    ContinueAdd = continueAdd;
  }

  public Process Process { get; private set; }
  public IntPtr ApiAdd { get; private set; }
  public IntPtr HookAdd { get; private set; }
  public IntPtr ContinueAdd { get; private set; }
  
}

public delegate void WinsockHookEventHandler(object sender, Events.WinsockHookEventArgs e);
public event WinsockHookEventHandler OnHook;
public event WinsockHookEventHandler OnUnhook;
public event WinsockHookEventHandler OnHookDetected;

Example

using System;
using System.Collections.Generic;
using System.Threading;

namespace NoAnkama
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleColor consoleColor = Console.ForegroundColor;
            WinsockHook winsockHook = new WinsockHook("Dofus");
            winsockHook.SourceIPs = new List<System.Net.IPAddress>
            {
                System.Net.IPAddress.Parse("213.248.126.40"),
                System.Net.IPAddress.Parse("213.248.126.41"),
            };
            winsockHook.RemoteIP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 5555);
            winsockHook.AllowReplace = true;

            winsockHook.OnHook += (sender, e) =>
             {
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine("----------------");
                 Console.WriteLine("API function hooked !");
                 PrintEventArgs(e);
                 Console.WriteLine("----------------");
                 Console.ForegroundColor = consoleColor;
                 Console.WriteLine();
             };
            winsockHook.OnUnhook += (sender, e) =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("----------------");
                Console.WriteLine("API function unhooked !");
                PrintEventArgs(e);
                Console.WriteLine("----------------");
                Console.ForegroundColor = consoleColor;
                Console.WriteLine();
            };
            winsockHook.OnHookDetected += (sender, e) =>
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("----------------");
                Console.WriteLine("API function is already hooked !");
                PrintEventArgs(e);
                Console.WriteLine("----------------");
                Console.ForegroundColor = consoleColor;
                Console.WriteLine();
            };

            winsockHook.Hook();
            Thread.Sleep(10000);
            winsockHook.Unhook();

            Console.Read();
        }

        static void PrintEventArgs(Events.WinsockHookEventArgs e)
        {
            Console.WriteLine("Process: {0} (PID={1})", e.Process.ProcessName, e.Process.Id);
            Console.WriteLine("\"connect\" add: 0x{0}", e.ApiAdd.ToInt32().ToString("X8"));
            Console.WriteLine("Continue add: 0x{0}", e.ContinueAdd.ToInt32().ToString("X8"));
            Console.WriteLine("Hook add: 0x{0}", e.HookAdd.ToInt32().ToString("X8"));
        }
    }
}

You can find it in Program.cs.

Demo

demo

TODO

  • Dispose
  • Support 64-bit
  • Support 32-bit
  • Avoid crashes
  • Pretty Code

Informations

I'm sorry if my code isn't pretty, I wrote it quick and the errors swelled me. But otherwise, I think my code works on every platforms. If you have any problems, please create an issue.

The code is based on the idea of Luax: MiniHook a detours alternative

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.