C# 版 防止 DNS 污染,获取域名真实 IP 地址
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 地址的更多相关文章
- 获取客户端真实IP地址
Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...
- Java中使用HttpRequest获取用户真实IP地址端口
import javax.servlet.http.HttpServletRequest; /** * 自定义访问对象工具类 * * 获取对象的IP地址等信息 * @author X-rapido * ...
- Java获取客户端真实IP地址
Java代码 import javax.servlet.http.HttpServletRequest; /** * 获取对象的IP地址等信息 */ public class IPUtil { /** ...
- Java 获取客户端真实IP地址
本文基于方法 HttpServletRequest.getHeader 和 HttpServletRequest.getRemoteAddr 介绍如何在服务器端获取客户端真实IP地址. 业务背景 服务 ...
- java如何获取访问真实IP地址?
java如何获取访问真实IP地址 解决方法: 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址,如果没有代理,则获取真实ip public static String getIp( ...
- 伪造IP及获取客户端真实IP地址
Fiddler支持自定义规则,可以实现对HTTP请求数据发送给Server前或HTTP应答数据发送给浏览器前进行修改.下面的例子将演示如何向所有HTTP请求数据中增加一个头.1)打开Fiddler,点 ...
- 绕过CDN获取服务器真实IP地址
相关视频链接:(https://blog.sechelper.com/20220914/penetration-testing-guide/cdn-bypass) CDN(Content Delive ...
- CDN下nginx获取用户真实IP地址
随着nginx的迅速崛起,越来越多公司将apache更换成nginx. 同时也越来越多人使用nginx作为负载均衡, 并且代理前面可能还加上了CDN加速,但是随之也遇到一个问题:nginx如何获取用户 ...
- LNAMP架构中后端Apache获取用户真实IP地址的2种方法(转)
一.Nginx反向代理配置: 1.虚拟主机配置 复制代码代码如下: location / { try_files $uri @apache;} location @apache {interna ...
随机推荐
- VisualStudio 发布代码的一个坑
现象:为了测试把时间改为2016-1-1,测试完成后改为实际时间.以后每次发布代码,生成的dll还是2016-1-1,而非发布时间 导致问题:生成的dll和实际代码不匹配,原因不明 解决过程: 1.修 ...
- Unity-EasyTouch插件之ReservedArea的运用(主要是避免JoyStick与Touch的矛盾)
昨天有人问我,easytouch插件中有个小bug,其实也不算是bug,插件的设计者早就考虑到这样的情况. 他说同时用easyjoystick和easytouch会发生,移动摇杆的时候,touch(触 ...
- zxing生成二维码和读取二维码
当然,首先要导入zxing的jar包. 生成二维码代码: package com.imooc.zxing; import java.io.File; import java.nio.file.Path ...
- [Eclipse插件] Eclipse设置Tab键为空格(ctrl+shirt+f格式化生效)!
自定义format格式,用空格替换Tab键,ctrl+shit+f格式化后生效: 设置Eclipse中按Tab键为4个空格,这里标记下! Window-->Preferences-->Ja ...
- go语言基础之匿名变量和多重赋
1.匿名变量 package main //必须有一个main包 import "fmt" func test() (a, b, c int) { return 1, 2, 3 } ...
- http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html
http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html http://blog.sina.com.cn/s/blog_538 ...
- ZOJ3622 Magic Number(水题)
分析: 举个样例xxx(三位数)为魔力数,则xxx|(xxx+1000*y),那么xxx|1000,这个就是结论 同理:四位数xxxx|10000,五位数xxxxx|100000 代码: #inclu ...
- 贯通Spark Streaming JobScheduler内幕实现和深入思考
本节主要内容: 一.SparkStreaming Job生成深度思考 二.SparkStreaming Job生成源码解析 JobScheduler的地位非常的重要,所有的关键都在JobSchedul ...
- 练oj时的小技巧(大多都在oj记录里,这是被忘记的部分)
1. getline()函数,头文件为#include<string> getline(istream &in, string &s):从输入流读入一行到string s ...
- 【实践】关于p 标签内嵌 p标签的bug
项目中遇到了一点小问题: 是这样的,在输入框包裹元素 p标签中想内嵌一个p 标签用作显示提示字符,谁知发生了一下一幕: 页面结构: <p class="modify-info-wrap ...