You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下如何用脚本监控HTTP/HTTPS协议并拦截替换指定URL?

可行的简易实现方案

针对Windows系统下拦截并替换默认浏览器的HTTP/HTTPS请求,这里提供两种适合业余开发者的简易方案:

方案1:自定义URL协议处理程序(最易上手)

Windows允许用户注册自定义程序处理HTTP/HTTPS协议,你可以把系统默认的HTTP/HTTPS协议关联改成自己的程序,在程序内完成URL判断、替换后,再调用真实浏览器打开目标地址。

实现步骤:

  1. 修改注册表关联协议:

    • 打开注册表编辑器(运行regedit),找到HKEY_CLASSES_ROOT\http\shell\open\command,将默认值改为你的程序路径,格式如"C:\path\to\your\program.exe" "%1"
    • 重复上述操作修改HKEY_CLASSES_ROOT\https\shell\open\command
    • 注意:修改前务必备份原注册表项,方便后续恢复默认设置。
  2. 编写处理程序(以Python为例):
    用Python编写简单脚本,接收传入的URL,执行替换逻辑后调用默认浏览器打开:

    import sys
    import webbrowser
    import re
    
    def replace_url(original_url):
        # 自定义替换逻辑:将example.com替换为example2.org
        replaced_url = re.sub(r"https?://example\.com", "http://www.example2.org", original_url)
        return replaced_url
    
    if __name__ == "__main__":
        if len(sys.argv) > 1:
            original_url = sys.argv[1]
            target_url = replace_url(original_url)
            # 调用默认浏览器打开替换后的URL
            webbrowser.open(target_url)
    

    用pyinstaller -F your_script.py将脚本打包成exe文件,再关联到注册表即可。

优缺点:

  • 优点:实现简单,无需复杂API,适合新手;能拦截所有通过系统协议触发的URL打开请求(如桌面快捷方式、文档内链接等)
  • 缺点:无法拦截浏览器内部跳转的请求(如浏览器内点击链接),仅对外部触发的URL请求有效

方案2:Hook ShellExecuteEx函数(拦截更全面)

如果需要覆盖更多场景(包括程序内部调用打开URL的情况),可以使用Hook技术。推荐用EasyHook(.NET平台),上手相对容易。

实现思路:

  • Hook系统的ShellExecuteExW函数,检测到打开HTTP/HTTPS URL时,修改参数中的URL为目标地址,再调用原函数执行。

简单示例(C# + EasyHook):

  1. 先安装EasyHook NuGet包
  2. 编写Hook代码:
    using EasyHook;
    using System;
    using System.Runtime.InteropServices;
    
    public class URLInterceptor
    {
        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
    
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct SHELLEXECUTEINFO
        {
            public int cbSize;
            public uint fMask;
            public IntPtr hwnd;
            public string lpVerb;
            public string lpFile;
            public string lpParameters;
            public string lpDirectory;
            public int nShow;
            public IntPtr hInstApp;
            public IntPtr lpIDList;
            public string lpClass;
            public IntPtr hkeyClass;
            public uint dwHotKey;
            public IntPtr hIcon;
            public IntPtr hProcess;
        }
    
        public static bool Hooked_ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo)
        {
            // 检查是否为HTTP/HTTPS URL
            if (lpExecInfo.lpFile != null && (lpExecInfo.lpFile.StartsWith("http://") || lpExecInfo.lpFile.StartsWith("https://")))
            {
                // 执行URL替换逻辑
                string originalUrl = lpExecInfo.lpFile;
                string replacedUrl = originalUrl.Replace("https://example.com", "http://www.example2.org");
                lpExecInfo.lpFile = replacedUrl;
            }
            // 调用原函数完成打开操作
            return ShellExecuteEx(ref lpExecInfo);
        }
    
        public static void Main()
        {
            // 安装Hook
            LocalHook.Create(
                LocalHook.GetProcAddress("shell32.dll", "ShellExecuteExW"),
                new ShellExecuteExDelegate(Hooked_ShellExecuteEx),
                null
            );
    
            // 保持程序运行
            Console.WriteLine("拦截器已启动...按回车键退出");
            Console.ReadLine();
        }
    
        public delegate bool ShellExecuteExDelegate(ref SHELLEXECUTEINFO lpExecInfo);
    }
    

优缺点:

  • 优点:能拦截更多场景的URL打开请求(包括程序内部调用ShellExecute的情况)
  • 缺点:需要了解基础Hook概念,部分杀毒软件可能误报,需以管理员权限运行

注意事项

  • 测试时建议在虚拟机或备用系统上验证,避免修改注册表导致系统异常
  • 方案1中,若要恢复默认浏览器,只需将注册表项改回原浏览器路径即可
  • 方案2需注意32位/64位系统的兼容性问题

内容的提问来源于stack exchange,提问作者szw0407

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:00:58