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. android 检查能否上网

    文章一: 首先在,AndroidManifest.xml 中增加访问权限: <uses-permission android:name="android.permission.ACCE ...

  2. Flash&Flex大全

    官方在线帮助(没标英文的都是中文) 用于 Adobe Flash Platform 的 ActionScript 3.0 参考 更多参考使这样的链接下载离线版:http://help.adobe.co ...

  3. Atom 有什么优秀插件?

    蓝色 ,主业三流青春校园小说作家兼反差萌段子手… 韦易笑等 130 人赞同 若是C / C++的话,我推荐ATOM的这几个插件主要用于代码补全,实时语法检测,以及代码格式调整,其实就是Clang的那一 ...

  4. python的globals()使用

    使用命令pyrasite-shell pid,可以与进程进行shell交互,获取,在shell里执行globals(),可以获取整个进程的全部全局变量,比如django应用.flask应用的变量,而不 ...

  5. [转]SSIS: By coding

    本文转自:http://www.codeproject.com/Articles/604197/SSIS-By-coding Introduction SSIS better known as “SQ ...

  6. arithmetic-slices

    https://leetcode.com/problems/arithmetic-slices/ public class Solution { public int numberOfArithmet ...

  7. PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa)

    PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa) 由于Zend新产品ZendGuardLoader的面世,Zend Optimizer已经不支持php5.3了,官方给出 ...

  8. 关于configure和Makefile

    http://blog.csdn.net/lltaoyy/article/details/7615833 转篇文章,讲的不是很清楚,再附上几个资料连接,来自http://www.linuxdw.com ...

  9. 云计算之路-Azure vs 阿里云:从负载均衡中摘/挂虚拟机

    @小尾鱼 在 试用Azure:上不了高速的跑车,无法跨Cloud Service的DNS服务器一文的评论中提了一个很好的问题: 问个问题,使用了负载均衡以后,程序发布的时候博客园是怎么避免用户访问到正 ...

  10. 主动通知Android系统图库进行更新

    项目中遇到调用图库进行图片的选择,因为不能主动及时更新,遂实现代码调用实现主动及时更新. 废话不多刷,看代码. 方式一,发送一个广播, sendBroadcast(new Intent(Intent. ...