[Yugong Series] February 2023 .NET CORE Tool Case - Lib.Harmony's AOP Interception

Article directory

foreword

API interception refers to restricting or controlling the functions and behaviors of programs by intercepting the calls of an application programming interface (API). It is usually an interception between the application and the operating system to prevent the application from accessing certain operating system functions or resources.

API interception can be used to monitor and analyze API calls to ensure that API callers comply with security rules. It can also be used to ensure that the API is not abused, and to ensure that API callers pay a reasonable fee. In addition, it can also be used to collect statistics on API calls to let developers and operators understand the usage of the API.

Github URL of Lib.Harmony: https://github.com/pardeike/Harmony

Lib.Harmony official website: https://harmony.pardeike.net/

1. Custom interception

1. Installation package

Lib.Harmony
copy

2. Basic use

2.1 Define the intercepted class

public class Student
{
    public string GetDetails(string name)
    {
        return $"Hello everyone, I am a blogger:{name}";
    }
}
copy

2.2 Define the interception class

/// <summary>
/// Custom interceptor (intercept Student and GetDetails methods)
/// </summary>
[HarmonyPatch(typeof(Student))]
[HarmonyPatch(nameof(Student.GetDetails))]
public class HookStudent
{
    public static bool Prefix()
    {
        Console.WriteLine($"Prefix");
        return true;
    }

    public static void Postfix()
    {
        Console.WriteLine($"Postfix");
    }

    public static void Finalizer()
    {
        Console.WriteLine($"Finalizer");
    }
}
copy

2.3 run

using ConsoleTest;
using HarmonyLib;
using System.Reflection;

var student = new Student();
Console.WriteLine(student.GetDetails("Yugong moved the code"));

var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
harmony.PatchAll(Assembly.GetExecutingAssembly());

Console.WriteLine(student.GetDetails("Yugong moved the code"));

Console.ReadLine();
copy

Using PatchAll can automatically discover the interception class, thereby automatically intercepting the GetDetails method call, and found that when the method is called for the second time, all three lifecycle methods of Harmony are called.

3. Parameter tampering

3.1 Intercept class modification

/// <summary>
/// Custom interceptor (intercept Student and GetDetails methods)
/// </summary>
[HarmonyPatch(typeof(Student))]
[HarmonyPatch(nameof(Student.GetDetails))]
public class HookStudent
{
    public static bool Prefix(ref string name, ref string __result)
    {
        if ("Yugong moved the code".Equals(name))
        {
            __result = $"Yugong moved the code has been renamed";
            return false;
        }

        if ("code move Yugong".Equals(name))
        {
            name = "code move Yugong";
        }

        return true;
    }

    public static void Postfix()
    {
        Console.WriteLine($"Postfix");
    }

    public static void Finalizer()
    {
        Console.WriteLine($"Finalizer");
    }
}
copy

3.2 run

using ConsoleTest;
using HarmonyLib;
using System.Reflection;

var student = new Student();
Console.WriteLine(student.GetDetails("Yugong moved the code"));

var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
harmony.PatchAll(Assembly.GetExecutingAssembly());

Console.WriteLine(student.GetDetails("Yugong moved the code"));
Console.WriteLine(student.GetDetails("code move Yugong"));


Console.ReadLine();
copy

The default value returned by the Prefix method indicates that the native method needs to be called, and the tampered parameters will be passed to the native method.

Two, WPF custom interception

custom message box

public class TG_MessageBox
{
    public void TG_Show(string messageBoxText)
    {
        MessageBox.Show(messageBoxText);
    }
}
copy

1. Automatic interception of registration in App

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    //Automatically intercept registration
    protected override void OnStartup(StartupEventArgs e)
    {

        base.OnStartup(e);

        var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
        harmony.PatchAll(Assembly.GetExecutingAssembly());
    }
}
copy

2. Automatic interception class

[HarmonyPatch(typeof(TG_MessageBox))]
[HarmonyPatch(nameof(TG_MessageBox.TG_Show))]
[HarmonyPatch(new[] { typeof(string) })]
public class HookMessageBox
{
    public static bool Prefix(ref string messageBoxText)
    {
        if (messageBoxText.Contains("Yu Gong"))
        {
            messageBoxText = "Yu Gong's blog: https://blog.csdn.net/aa2528877987";
        }

        return true;
    }
}
copy

3. run

Add two buttons that pop up the prompt box in the form MainWindow.xaml:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Button Content="Pop up the default prompt box" Width="120" Height="30" Click="ShowDialog_OnClick"></Button>
        <Button Content="Yugong blog site" Width="120" Height="30" Click="ShowBadMessageDialog_OnClick"></Button>
    </StackPanel>
</Window>
copy
public partial class MainWindow : Window
{
    public TG_MessageBox tG_MessageBox { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        tG_MessageBox = new TG_MessageBox();
    }

    private void ShowDialog_OnClick(object sender, RoutedEventArgs e)
    {

        tG_MessageBox.TG_Show("https://blog.csdn.net/aa2528877987 is a website for programmers keen on technology sharing");
    }

    private void ShowBadMessageDialog_OnClick(object sender, RoutedEventArgs e)
    {
        tG_MessageBox.TG_Show("Yu Gong");
    }
}
copy

You can see that the popup message has been tampered with

Tags: api AOP app WPF

Posted by Tandem on Thu, 16 Mar 2023 15:52:47 +0530