Skip to content

伪造IP思路

L edited this page Jan 21, 2019 · 3 revisions

代理

请求时设置代理

简单来说就是在请求对象上设置Proxy,也可以通过HttpWebRequest等对象实现

/// <summary>
/// 在请求时设置代理
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="url"></param>
public static void SpoofIpAddressBySetProxyWhileRequest(string ip, int port,string url)
{
    //https://stackoverflow.com/questions/29856543/httpclient-and-using-proxy-constantly-getting-407
            
    // First create a proxy object

    var proxy = new WebProxy()
    {
        Address = new Uri($"http://{ip}:{port}"),
        UseDefaultCredentials = false,

        //如果该代理需要账号密码,设置账号密码
        // *** These creds are given to the proxy server, not the web server ***
        //Credentials = new NetworkCredential(
        //    userName: proxyUserName,
        //    password: proxyPassword);
    };

    // Now create a client handler which uses that proxy

    var httpClientHandler = new HttpClientHandler()
    {
        Proxy = proxy,
        UseProxy = true
    };

    // Omit this part if you don't need to authenticate with the web server:
    //if (needServerAuthentication)
    //{
    //    httpClientHandler.PreAuthenticate = true;
    //    httpClientHandler.UseDefaultCredentials = false;

    //    // *** These creds are given to the web server, not the proxy server ***
    //    httpClientHandler.Credentials = new NetworkCredential(
    //        userName: serverUserName,
    //        password: serverPassword);
    //}

    // Finally, create the HTTP client object

    var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
    var response = client.GetAsync(url).Result;
    var str = response.Content.ReadAsStringAsync().Result;
}
string address = $"{ip}:{port}";
WebResponse response = null;
HttpWebRequest request = null;
try
{
    WebProxy webProxy = new WebProxy(address, true);
    webProxy.Credentials = CredentialCache.DefaultCredentials;

    request = (HttpWebRequest)WebRequest.Create(url);
    request.Proxy = webProxy;
    request.Timeout = 15 * 1000;
    request.ReadWriteTimeout = 15 * 1000;

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    response = request.GetResponse();
    stopwatch.Stop();
    var totalMilliseconds= stopwatch.Elapsed.TotalMilliseconds;

    response.Dispose();
}
catch(Exception ex)
{

}

全局代理

修改计算机配置

/// <summary>
/// 全局设置代理
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public static void SpoofIpAddressBySetProxyForSystem(string ip,int port)
{
    SetProxy($"{ip}:{port}");
    //在这里发送请求
    UnSetProxy();
}
/// <summary>
/// 设置代理
/// </summary>
/// <param name="ip_port">IP地址和端口</param>
public static void SetProxy(string ip_port)
{
    //打开注册表
    RegistryKey regKey = Registry.CurrentUser;
    string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);
    //更改健值,设置代理,
    optionKey.SetValue("ProxyEnable", 1);
    if (ip_port.Length == 0)
    {
        optionKey.SetValue("ProxyEnable", 0);
    }
    optionKey.SetValue("ProxyServer", ip_port);

    //激活代理设置
    InternetSetOption(0, 39, IntPtr.Zero, 0);
    InternetSetOption(0, 37, IntPtr.Zero, 0);
}

/// <summary>
/// 不使用代理设置
/// </summary>
public static void UnSetProxy()
{
    RegistryKey regKey = Registry.CurrentUser;
    string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);
    //更改健值,设置代理,
    optionKey.SetValue("ProxyEnable", 0);
    //激活代理设置
    InternetSetOption(0, 39, IntPtr.Zero, 0);
    InternetSetOption(0, 37, IntPtr.Zero, 0);
}

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Console/CodeLibrary/SpoofIpAddress/SpoofIpAddressDemo3.cs

代码伪造

使用发包的形式发送请求

利用第三方库:https://github.com/chmorgan/sharppcap

修改X-Forwarded-For

这个要根据服务端采用的技术来决定
比如服务端读取的是Header头"X-Forwarded-For",则可以通过伪造X-Forwarded-For来伪造IP

var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-Forwarded-For", getRandomIp());

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Console/CodeLibrary/SpoofIpAddress/SpoofIpAddressDemo.cs

Clone this wiki locally