using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text; namespace TestDnsResolver
{
class Program
{
static void Main(string[] args)
{
GFWDnsResolver dnsResolver = GFWDnsResolver.Instance();
string domain = "www.google.com";
string ip = dnsResolver.GFWResolve(domain);
Console.WriteLine(ip + " " + domain);
Console.ReadKey();
}
} public class GFWDnsResolver
{
private static GFWDnsResolver resolver = null; private static string DNS_SERVER = "8.8.8.8"; private Encoding coding = Encoding.UTF8; private bool debug = false;
private bool cache = false; private int maxTryTimes = ;
private int waitTimes = ; private Dictionary<string, string> dnsCache = new Dictionary<string, string>(); string[] blackList = {
"74.125.127.102", "74.125.155.102", "74.125.39.102", "74.125.39.113",
"209.85.229.138",
"128.121.126.139", "159.106.121.75", "169.132.13.103", "192.67.198.6",
"202.106.1.2", "202.181.7.85", "203.161.230.171", "203.98.7.65",
"207.12.88.98", "208.56.31.43", "209.145.54.50", "209.220.30.174",
"209.36.73.33", "211.94.66.147", "213.169.251.35", "216.221.188.182",
"216.234.179.13", "243.185.187.39", "37.61.54.158", "4.36.66.178",
"46.82.174.68", "59.24.3.173", "64.33.88.161", "64.33.99.47",
"64.66.163.251", "65.104.202.252", "65.160.219.113", "66.45.252.237",
"72.14.205.104", "72.14.205.99", "78.16.49.15", "8.7.198.45", "93.46.8.89"}; public static GFWDnsResolver Instance()
{
if (resolver == null)
{
resolver = new GFWDnsResolver();
}
return resolver;
} private GFWDnsResolver() { } private bool IsBadReply(string ip)
{
for (int i = ; i < blackList.Length; i++)
{
if (blackList[i].Equals(ip))
{
return true;
}
}
return false;
} public string GFWResolve(string domain)
{
IPAddress[] address = Dns.GetHostAddresses(domain);
string ip = address[].ToString();
if (!IsBadReply(ip))
{
return ip;
}
else if (cache && dnsCache.ContainsKey(domain))
{
return dnsCache[domain];
} for (int i = ; i < maxTryTimes; i++)
{
ip = Resolve(domain);
if (IsBadReply(ip) || ip == null)
{
continue;
}
else
{
if (cache)
{
dnsCache.Add(domain, ip);
}
return ip;
}
}
return string.Empty;
} private void HexDump(byte[] bytes)
{
Console.WriteLine(BytesToHex(bytes));
} private string BytesToHex(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < bytes.Length; i++)
{
sb.AppendFormat("{0:X2}", bytes[i]);
} string hex = sb.ToString();
return hex;
} private string Resolve(string domain)
{
byte[] recvData = new byte[];
byte[] data = BuildRequestData(domain);
string result = null;
if (debug)
{
Console.WriteLine(" =============== dns query request package dump: ================");
HexDump(data);
} IPEndPoint iep = new IPEndPoint(IPAddress.Parse(DNS_SERVER), );
Socket dataSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
dataSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, * );
dataSocket.SendTo(data, iep); byte[] respData = new byte[];
for (int i = ; i < waitTimes; i++)
{
try
{
int intReceived = dataSocket.Receive(respData);
byte[] dataReceive = new byte[intReceived];
Array.Copy(respData, dataReceive, intReceived); if (debug)
{
Console.WriteLine("============ dns query answer package dump");
HexDump(dataReceive);
} string ip = DecodeDnsResponse(dataReceive, domain);
if (IsBadReply(ip))
{
continue;
}
else
{
result = ip;
break;
}
}
catch (SocketException ex)
{
throw ex;
}
} dataSocket.Close();
return result;
} private byte[] BuildRequestData(string host)
{
// head + (host length +1) + eof sign + qtype + qclass
int size = + host.Length + + + ;
using (MemoryStream buff = new MemoryStream(size))
{
byte[] tmp = null; Random random = new Random();
byte[] seq = new byte[];
random.NextBytes(seq);
buff.Write(seq, , seq.Length); byte[] header = new byte[] { 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
buff.Write(header, , header.Length); string[] parts = host.Split('.');
for (int i = ; i < parts.Length; i++)
{
buff.WriteByte((byte)parts[i].Length); byte[] partsByte = coding.GetBytes(parts[i]);
buff.Write(partsByte, , partsByte.Length);
} tmp = new byte[] { 0x00 };
buff.Write(tmp, , tmp.Length); tmp = new byte[] { 0x00, 0x01, 0x00, 0x01 };
buff.Write(tmp, , tmp.Length); return buff.ToArray();
}
} private string DecodeDnsResponse(byte[] resp, string host)
{
using (MemoryStream stream = new MemoryStream(resp))
{
using (BinaryReader buffer = new BinaryReader(stream))
{
//parse the query answer count.
int pos = ;
stream.Position = pos;
ushort qncount = buffer.ReadUInt16(); //skip query answer field
pos = + + host.Length + + ;
stream.Position = pos;
for (int i = ; i < qncount; i++)
{
stream.Position = pos;
byte pointFlg = buffer.ReadByte();
if ((pointFlg & 0xc0) == 0xc0)
{
pos += ;
}
else
{
pos += + host.Length + ;
} stream.Position = pos;
ushort queryType = buffer.ReadUInt16(); if (debug)
{
Console.WriteLine("qncount:" + qncount + "pos:" + pos + "queryType:" + queryType);
} pos += ;
stream.Position = pos;
int dataLen = buffer.ReadByte();
pos += ; //A record
if (queryType == 0x0001)
{
if (debug)
{
Console.WriteLine("parse A record");
} string ip = string.Empty;
for (int j = ; j < dataLen; j++)
{
stream.Position = pos;
int v = buffer.ReadByte();
v = v > ? v : 0x0ff & v;
ip += v + (j == dataLen - ? "" : ".");
pos += ;
}
return ip;
}
else
{
pos += dataLen;
}
}
return string.Empty;
}
}
}
}
}

C# 版 防止 DNS 污染,获取域名真实 IP 地址的更多相关文章

  1. 获取客户端真实IP地址

    Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...

  2. Java中使用HttpRequest获取用户真实IP地址端口

    import javax.servlet.http.HttpServletRequest; /** * 自定义访问对象工具类 * * 获取对象的IP地址等信息 * @author X-rapido * ...

  3. Java获取客户端真实IP地址

    Java代码 import javax.servlet.http.HttpServletRequest; /** * 获取对象的IP地址等信息 */ public class IPUtil { /** ...

  4. Java 获取客户端真实IP地址

    本文基于方法 HttpServletRequest.getHeader 和 HttpServletRequest.getRemoteAddr 介绍如何在服务器端获取客户端真实IP地址. 业务背景 服务 ...

  5. java如何获取访问真实IP地址?

    java如何获取访问真实IP地址 解决方法: 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址,如果没有代理,则获取真实ip public static String getIp( ...

  6. 伪造IP及获取客户端真实IP地址

    Fiddler支持自定义规则,可以实现对HTTP请求数据发送给Server前或HTTP应答数据发送给浏览器前进行修改.下面的例子将演示如何向所有HTTP请求数据中增加一个头.1)打开Fiddler,点 ...

  7. 绕过CDN获取服务器真实IP地址

    相关视频链接:(https://blog.sechelper.com/20220914/penetration-testing-guide/cdn-bypass) CDN(Content Delive ...

  8. CDN下nginx获取用户真实IP地址

    随着nginx的迅速崛起,越来越多公司将apache更换成nginx. 同时也越来越多人使用nginx作为负载均衡, 并且代理前面可能还加上了CDN加速,但是随之也遇到一个问题:nginx如何获取用户 ...

  9. LNAMP架构中后端Apache获取用户真实IP地址的2种方法(转)

    一.Nginx反向代理配置: 1.虚拟主机配置 复制代码代码如下: location / {    try_files $uri @apache;} location @apache {interna ...

随机推荐

  1. express结合EJS模板渲染HTML

    注意:以下是在Windwo环境下 运行: npm install ejs 然后你的目录node_modules下将增加ejs文件夹 app.js var express = require(" ...

  2. [Eclipse插件] 安装和使用JD-Eclipse插件

    JD-Core 是一个免费的库,从一个或多个“.class”文件中 重构Java源代码.JD-Core可以用来恢复丢失的源代码,并深究Java运行时类库.支持Java 5的功能:如注释,泛型或键入“枚 ...

  3. C语言大总结

    C语言大总结 一. C语言基本常识 1.语言由函数组成 2.main是程序入口 3.C语言中不能出现中文或中文字符 (凝视和字符串除外) keyword : C语言提供表示特殊含义的单词 特点 : 1 ...

  4. iOS:iOS开发系列–打造自己的“美图秀秀”(上)

    来源: KenshinCui 链接:http://www.cnblogs.com/kenshincui/p/3959951.html 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功 ...

  5. python中子类调用父类的方法

    1子类调用父类构造方法 class Animal(object): def __init__(self): print("init Animal class~") def run( ...

  6. (转)Akka学习笔记

    Akka学习笔记系列文章: <Akka学习笔记:ACTORS介绍> <Akka学习笔记:Actor消息传递(1)> <Akka学习笔记:Actor消息传递(2)> ...

  7. aspcms 幻灯片用列表调用

    首先找到网站目录/inc/AspCms_MainClass.asp 增加主函数 Public Function parseSlideList(str) if not isExistStr(conten ...

  8. Discuz常见小问题2-如何清空,删除,清除全部DIY的数据

    如果所有diy都不想要了,手动清空_common_block._common_diy_data与_common_template_block表,然后删除\data\diy\下的所有子文件夹,保证你以前 ...

  9. 【菜鸟也疯狂UML系列】——浅析UML四种关系

    在UML中.关系是很重要的.它抽象出对象之间的联系,让对象构成某个联系起来的结构.以下将简要分析一下UML中的四种关系:关联.依赖,泛化,实现. 一.举例罗列 1.关联(Association) 关联 ...

  10. FFMpeg框架代码阅读

    http://blog.csdn.net/wstarx/article/details/1572393 FFMPEG源码分析(二) http://www.cnblogs.com/qingquan/ar ...