Пример #1
0
void DNSParser(const char *dns_over_tcp, char *buffer){
	char *orig = buffer;
	char *dnsovertcp = dns_over_tcp;
	char InnerBuffer[128];
	unsigned short qc, ac;

	buffer += sprintf(buffer, "TCPLength:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "QueryIdentifier:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "Flags:%x\n", GET_16_BIT_U_INT(dnsovertcp));

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "QuestionCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp));
	qc = GET_16_BIT_U_INT(dnsovertcp);

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "AnswerCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp));
	ac = GET_16_BIT_U_INT(dnsovertcp);

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "NameServerCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

	dnsovertcp += 2; /* sizeof(unsigned short) */
	buffer += sprintf(buffer, "AdditionalCount:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

	dnsovertcp += 2; /* sizeof(unsigned short) */

	for(; qc != 0; --qc){
		dnsovertcp += DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
		buffer += sprintf(buffer, "QuestionName:%s\n", InnerBuffer);

		buffer += sprintf(buffer, "QuestionType:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

		dnsovertcp += 2; /* sizeof(unsigned short) */
		buffer += sprintf(buffer, "QuestionClass:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

		dnsovertcp += 2; /* sizeof(unsigned short) */
	}

	for(; ac != 0; --ac){
		unsigned short rt, dl;
		dnsovertcp += DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
		buffer += sprintf(buffer, "ResourceName:%s\n", InnerBuffer);


		buffer += sprintf(buffer, "ResourceType:%hu\n", GET_16_BIT_U_INT(dnsovertcp));
		rt = GET_16_BIT_U_INT(dnsovertcp);

		dnsovertcp += 2; /* sizeof(unsigned short) */
		buffer += sprintf(buffer, "ResourceClass:%hu\n", GET_16_BIT_U_INT(dnsovertcp));

		dnsovertcp += 2; /* sizeof(unsigned short) */
		buffer += sprintf(buffer, "TimeToLive:%u\n", GET_32_BIT_U_INT(dnsovertcp));

		dnsovertcp += 4; /* sizeof(unsigned int) */
		buffer += sprintf(buffer, "ResourceDataLength:%hu\n", GET_16_BIT_U_INT(dnsovertcp));
		dl = GET_16_BIT_U_INT(dnsovertcp);

		dnsovertcp += 2; /* sizeof(unsigned short) */
		switch(rt){
			case DNS_TYPE_A: /* A, IPv4 address */
				buffer += sprintf(buffer, "IPv4Addres:%d.%d.%d.%d\n", GET_8_BIT_U_INT(dnsovertcp), GET_8_BIT_U_INT(dnsovertcp + 1), GET_8_BIT_U_INT(dnsovertcp + 2), GET_8_BIT_U_INT(dnsovertcp + 3));
				break;
			case DNS_TYPE_AAAA: /* AAAA, IPv6 address */
				buffer += sprintf(buffer, "IPv6Addres:%x:%x:%x:%x:%x:%x:%x:%x \n",
					GET_16_BIT_U_INT(dnsovertcp), GET_16_BIT_U_INT(dnsovertcp + 2), GET_16_BIT_U_INT(dnsovertcp + 4), GET_16_BIT_U_INT(dnsovertcp + 6),
					GET_16_BIT_U_INT(dnsovertcp + 8), GET_16_BIT_U_INT(dnsovertcp + 10), GET_16_BIT_U_INT(dnsovertcp + 12), GET_16_BIT_U_INT(dnsovertcp + 14)
					);
				break;
			case DNS_TYPE_CNAME: /* CNAME */
				DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
				buffer += sprintf(buffer, "CName:%s\n", InnerBuffer);
				break;
			default:
				break;
		}
		dnsovertcp += dl;
	}
}
Пример #2
0
void DNSParser(char *dns_over_tcp, char *buffer){
	char *dnsovertcp	=	dns_over_tcp;
	char InnerBuffer[128]		=	{0};
	unsigned short qc, ac;

	buffer += sprintf(buffer, "TCPLength:%hu\n", DNSGetTCPLength(DNSGetDNSBody(dnsovertcp)));

	buffer += sprintf(buffer, "QueryIdentifier:%hu\n", DNSGetQueryIdentifier(DNSGetDNSBody(dnsovertcp)));

	buffer += sprintf(buffer, "Flags:%x\n", DNSGetFlags(DNSGetDNSBody(dnsovertcp)));

	qc = DNSGetQuestionCount(DNSGetDNSBody(dnsovertcp));
	buffer += sprintf(buffer, "QuestionCount:%hu\n", qc);

	ac = DNSGetAnswerCount(DNSGetDNSBody(dnsovertcp));
	buffer += sprintf(buffer, "AnswerCount:%hu\n", ac);

	buffer += sprintf(buffer, "NameServerCount:%hu\n", DNSGetNameServerCount(DNSGetDNSBody(dnsovertcp)));

	buffer += sprintf(buffer, "AdditionalCount:%hu\n", DNSGetAdditionalCount(DNSGetDNSBody(dnsovertcp)));

	dnsovertcp = DNSJumpHeader(DNSGetDNSBody(dns_over_tcp));

	for(; qc != 0; --qc){
		DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
		buffer += sprintf(buffer, "QuestionName:%s\n", InnerBuffer);

		buffer += sprintf(buffer, "QuestionType:%hu\n", DNSGetRecordType(dnsovertcp));

		buffer += sprintf(buffer, "QuestionClass:%hu\n", DNSGetRecordClass(dnsovertcp));
	}

	dnsovertcp = DNSJumpOverQuestionRecords(DNSGetDNSBody(dns_over_tcp));

	while(ac != 0){
		unsigned short rt, dl;
		dnsovertcp = DNSGetAnswerRecordPosition(DNSGetDNSBody(dns_over_tcp), DNSGetAnswerCount(DNSGetDNSBody(dns_over_tcp)) - ac + 1);

		DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
		buffer += sprintf(buffer, "ResourceName:%s\n", InnerBuffer);

		rt = DNSGetRecordType(dnsovertcp);
		buffer += sprintf(buffer, "ResourceType:%hu\n", rt);

		buffer += sprintf(buffer, "ResourceClass:%hu\n", DNSGetRecordClass(dnsovertcp));

		buffer += sprintf(buffer, "TimeToLive:%u\n", (unsigned int)DNSGetTTL(dnsovertcp));

		dl = DNSGetResourceDataLength(dnsovertcp);
		buffer += sprintf(buffer, "ResourceDataLength:%hu\n", dl);

		dnsovertcp = DNSGetResourceDataPos(dnsovertcp);
		switch(rt){
			case DNS_TYPE_A: /* A, IPv4 address */
				buffer += sprintf(buffer, "IPv4Addres:%d.%d.%d.%d\n", GET_8_BIT_U_INT(dnsovertcp), GET_8_BIT_U_INT(dnsovertcp + 1), GET_8_BIT_U_INT(dnsovertcp + 2), GET_8_BIT_U_INT(dnsovertcp + 3));
				break;
			case DNS_TYPE_AAAA: /* AAAA, IPv6 address */
				buffer += sprintf(buffer, "IPv6Addres:%x:%x:%x:%x:%x:%x:%x:%x\n",
					GET_16_BIT_U_INT(dnsovertcp), GET_16_BIT_U_INT(dnsovertcp + 2), GET_16_BIT_U_INT(dnsovertcp + 4), GET_16_BIT_U_INT(dnsovertcp + 6),
					GET_16_BIT_U_INT(dnsovertcp + 8), GET_16_BIT_U_INT(dnsovertcp + 10), GET_16_BIT_U_INT(dnsovertcp + 12), GET_16_BIT_U_INT(dnsovertcp + 14)
					);
				break;
			case DNS_TYPE_CNAME: /* CNAME */
				DNSGetHostName(dns_over_tcp + 2, dnsovertcp, InnerBuffer);
				buffer += sprintf(buffer, "CName:%s\n", InnerBuffer);
				break;
			default:
				break;
		}
		dnsovertcp = DNSGetAnswerRecordPosition(DNSGetDNSBody(dns_over_tcp), DNSGetAnswerCount(dns_over_tcp) - ac + 1);
		--ac;
	}
}
Пример #3
0
static int Query(ThreadContext *Context, CompatibleAddr *ClientAddr)
{
	int		State;

	char	RequestingDomain[256];

	char	ClientIP[LENGTH_OF_IPV6_ADDRESS_ASCII + 1];

	if( Family == AF_INET )
	{
		strcpy(ClientIP, inet_ntoa(ClientAddr -> Addr4.sin_addr));
	} else {
		IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), ClientIP);
	}

	Context -> ClientIP = ClientIP;

	RequestingDomain[0] = '\0';
	DNSGetHostName(Context -> RequestEntity,
				   DNSJumpHeader(Context -> RequestEntity),
				   RequestingDomain
				   );

	StrToLower(RequestingDomain);

	Context -> RequestingDomain = RequestingDomain;

	Context -> RequestingType =
		(DNSRecordType)DNSGetRecordType(DNSJumpHeader(Context -> RequestEntity));

	Context -> RequestingDomainHashValue = ELFHash(RequestingDomain, 0);

	Context -> CurrentTime = time(NULL);

	State = QueryBase(Context);

	switch( State )
	{
		case QUERY_RESULT_DISABLE:
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.Direction = 1;
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.RecursionAvailable = 1;
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.ResponseCode = RefusingResponseCode;
			if( Family == AF_INET )
			{
				_SendTo(ListenSocketUDP,
						Context -> RequestEntity,
						Context -> RequestLength,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr4),
						sizeof(struct sockaddr)
						);
			} else {
				_SendTo(ListenSocketUDP,
						Context -> RequestEntity,
						Context -> RequestLength,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr6),
						sizeof(struct sockaddr_in6)
						);
			}
			return -1;
			break;

		case QUERY_RESULT_ERROR:
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.Direction = 1;
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.RecursionAvailable = 1;
			((DNSHeader *)(Context -> RequestEntity)) -> Flags.ResponseCode = 2;
			if( Family == AF_INET )
			{
				_SendTo(ListenSocketUDP,
						Context -> RequestEntity,
						Context -> RequestLength,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr4),
						sizeof(struct sockaddr)
						);
			} else {
				_SendTo(ListenSocketUDP,
						Context -> RequestEntity,
						Context -> RequestLength,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr6),
						sizeof(struct sockaddr_in6)
						);
			}
			return -1;
			break;

		default: /* Succeed */
			if(State > MaximumMessageSize)
			{
				State = MaximumMessageSize;
				((DNSHeader *)(ExtendableBuffer_GetData(Context -> ResponseBuffer))) -> Flags.TrunCation = 1;
			}

			if( Family == AF_INET )
			{
				_SendTo(ListenSocketUDP,
						ExtendableBuffer_GetData(Context -> ResponseBuffer),
						State,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr4),
						sizeof(struct sockaddr)
						);
			} else {
				_SendTo(ListenSocketUDP,
						ExtendableBuffer_GetData(Context -> ResponseBuffer),
						State,
						0,
						(struct sockaddr *)&(ClientAddr -> Addr6),
						sizeof(struct sockaddr_in6)
						);
			}
			return 0;
	}
}
Пример #4
0
DNSDataInfo DNSParseData(const char *DNSBody,
						const char *DataBody,
						int DataLength,
						void *Buffer,
						int BufferLength,
						const ElementDescriptor *Descriptor,
						int CountOfDescriptor,
						int Num)
{
	DNSDataInfo Result = {DNS_DATA_TYPE_UNKNOWN, 0};

	const char *PendingData = DataBody;

	if( Num > CountOfDescriptor || DataLength <= 0 )
		return Result;

	while(Num != 1)
	{
		switch(Descriptor -> element)
		{
			case DNS_LABELED_NAME:
				PendingData = DNSJumpOverName(PendingData);
				break;

			case DNS_CHARACTER_STRING:
				PendingData += strlen(PendingData) + 1;
				break;

			case DNS_IPV6_ADDR:
				PendingData += 16;
				break;

			case DNS_IPV4_ADDR:
			case DNS_32BIT_UINT:
				PendingData += 4;
				break;

			case DNS_DNSKEY_FLAGS:
			case DNS_16BIT_UINT:
				PendingData += 2;
				break;

			case DNS_DNSKEY_PROTOCOL:
			case DNS_DNSKEY_ALGORITHM:
			case DNS_8BIT_UINT:
				PendingData += 1;
				break;

			default:
				return Result;
				break;
		}

		--Num;
		++Descriptor;
	}

	switch(Descriptor -> element)
	{
		case DNS_LABELED_NAME:
			if(BufferLength < DNSGetHostNameLength(DNSBody, PendingData))
				break;

			Result.DataLength = DNSGetHostNameLength(DNSBody, PendingData);
			DNSGetHostName(DNSBody, PendingData, (char *)Buffer);
			Result.DataType = DNS_DATA_TYPE_STRING;
			break;

		case DNS_CHARACTER_STRING:
			if( BufferLength < GET_8_BIT_U_INT(PendingData) + 1 )
			{
				break;
			}

			memcpy(Buffer, PendingData + 1, GET_8_BIT_U_INT(PendingData));
			((char *)Buffer)[GET_8_BIT_U_INT(PendingData)] = '\0';

			Result.DataLength = GET_8_BIT_U_INT(PendingData) + 1;
			Result.DataType = DNS_DATA_TYPE_STRING;

			break;

		case DNS_32BIT_UINT:
			{
				uint32_t Tmp = GET_32_BIT_U_INT(PendingData);
				if(BufferLength < 4)
					break;
				memcpy(Buffer, &Tmp, 4);
				Result.DataLength = 4;
				Result.DataType = DNS_DATA_TYPE_UINT;
			}
			break;

		case DNS_16BIT_UINT:
			{
				uint16_t Tmp = GET_16_BIT_U_INT(PendingData);
				if(BufferLength < 2)
					break;
				memcpy(Buffer, &Tmp, 2);
				Result.DataLength = 2;
				Result.DataType = DNS_DATA_TYPE_UINT;
			}
			break;

		case DNS_DNSKEY_PROTOCOL:
		case DNS_8BIT_UINT:
			if(BufferLength < 1)
				break;
			*(char *)Buffer = *PendingData;
			Result.DataLength = 1;
			Result.DataType = DNS_DATA_TYPE_UINT;
			break;

		case DNS_DNSKEY_FLAGS:
			if( BufferLength < 17 )
			{
				break;
			}

			Result.DataLength = 17;
			BinaryOutput(PendingData, 2, Buffer);
			Result.DataType = DNS_DATA_TYPE_STRING;
			break;

		case DNS_DNSKEY_ALGORITHM:
			{
				const char *Name;

				Name = DNSSECGetAlgorithmName(*PendingData);

				if( BufferLength < strlen(Name) + 1 + 4 )
				{
					break;
				}

				Result.DataLength = sprintf(Buffer, "%d %s", (unsigned char)*PendingData, Name) + 1;
				Result.DataType = DNS_DATA_TYPE_STRING;
			}
			break;

		case DNS_DNSKEY_PUBLIC_KEY:
			if( BufferLength < DataLength - 4 + 1 )
			{
				break;
			}

			Result.DataLength = DataLength - 4 + 1;
			memcpy(Buffer, PendingData, DataLength - 4);
			((char *)Buffer)[DataLength - 4] = '\0';
			Result.DataType = DNS_DATA_TYPE_STRING;
			break;

		case DNS_DNSSIG_SIGNATURE:
			if( BufferLength < sizeof("(      bytes binary object)") )
			{
				break;
			}

			Result.DataLength = sprintf(Buffer, "(%d bytes binary object)", (int)(DataLength - (PendingData - DataBody)));
			Result.DataType = DNS_DATA_TYPE_STRING;

			break;

		case DNS_IPV4_ADDR:
			if(BufferLength < 16)
				break;
			Result.DataLength =
			sprintf((char *)Buffer, "%u.%u.%u.%u",	GET_8_BIT_U_INT(PendingData),
											GET_8_BIT_U_INT(PendingData + 1),
											GET_8_BIT_U_INT(PendingData + 2),
											GET_8_BIT_U_INT(PendingData + 3)
				);
			Result.DataType = DNS_DATA_TYPE_STRING;
			break;

		case DNS_IPV6_ADDR:
			if(BufferLength < 40)
				break;
			Result.DataLength =
			sprintf((char *)Buffer, "%x:%x:%x:%x:%x:%x:%x:%x",	GET_16_BIT_U_INT(PendingData),
														GET_16_BIT_U_INT(PendingData + 2),
														GET_16_BIT_U_INT(PendingData + 4),
														GET_16_BIT_U_INT(PendingData + 6),
														GET_16_BIT_U_INT(PendingData + 8),
														GET_16_BIT_U_INT(PendingData + 10),
														GET_16_BIT_U_INT(PendingData + 12),
														GET_16_BIT_U_INT(PendingData + 14)

				);
			Result.DataType = DNS_DATA_TYPE_STRING;
			break;

		default:
			break;
	}
	return Result;
}
Пример #5
0
static int Query(	SOCKET				*PrimarySocket,
					SOCKET				*SecondarySocket,
					DNSQuaryProtocol	PrimaryProtocol,
					char				*QueryContent,
					int					QueryContentLength,

					SOCKET				*ClientSocket,
					CompatibleAddr		*ClientAddr,
					ExtendableBuffer	*Buffer
					)
{
	int					State;

	DNSRecordType		SourceType;
	char				*DNSBody = DNSGetDNSBody(QueryContent);

	char				ProtocolCharacter = ' ';

	char				QueryDomain[256];

	char				DateAndTime[32];

	QueryContext		Context;

	GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));

	QueryDomain[0] = '\0';
	DNSGetHostName(DNSBody, DNSJumpHeader(DNSBody), QueryDomain);

	SourceType = (DNSRecordType)DNSGetRecordType(DNSJumpHeader(DNSBody));

	Context.PrimarySocket = PrimarySocket;
	Context.SecondarySocket = SecondarySocket;
	Context.PrimaryProtocolToServer = PrimaryProtocol;
	Context.ProtocolToSrc = DNS_QUARY_PROTOCOL_TCP;
	Context.Compress = TRUE;

	State = QueryBase(&Context,

					  QueryContent,
					  QueryContentLength,

					  Buffer,
					  QueryDomain,
					  SourceType,
					  &ProtocolCharacter
					  );

	switch( State )
	{
		case QUERY_RESULT_DISABLE:
			((DNSHeader *)DNSBody) -> Flags.Direction = 1;
			((DNSHeader *)DNSBody) -> Flags.ResponseCode = 5;
			send(*ClientSocket, QueryContent, QueryContentLength, 0);
			if( Family == AF_INET )
			{
				PRINT("%s[R][%s:%d][%s][%s] Refused.\n", DateAndTime, inet_ntoa(ClientAddr -> Addr4.sin_addr), ClientAddr -> Addr4.sin_port, DNSGetTypeName(SourceType), QueryDomain);
			} else {
				char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};

				IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr);

				PRINT("%s[R][%s:%d][%s][%s] Refused.\n", DateAndTime, Addr, ClientAddr -> Addr6.sin6_port, DNSGetTypeName(SourceType), QueryDomain);

			}
			return -1;
			break;

		case QUERY_RESULT_ERROR:
			if( ErrorMessages == TRUE )
			{
				int		ErrorNum = GET_LAST_ERROR();
				char	ErrorMessage[320];

				ErrorMessage[0] ='\0';

				GetErrorMsg(ErrorNum, ErrorMessage, sizeof(ErrorMessage));
				if( Family == AF_INET )
				{
					printf("%s[%c][%s][%s][%s] Error occured : %d : %s .\n",
						   DateAndTime,
						   ProtocolCharacter,
						   inet_ntoa(ClientAddr -> Addr4.sin_addr),
						   DNSGetTypeName(SourceType),
						   QueryDomain,
						   ErrorNum,
						   ErrorMessage
						   );
				} else {
					char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};

					IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr);

					printf("%s[%c][%s][%s][%s] Error occured : %d : %s .\n",
						   DateAndTime,
						   ProtocolCharacter,
						   Addr,
						   DNSGetTypeName(SourceType),
						   QueryDomain,
						   ErrorNum,
						   ErrorMessage
						   );
				}
			}
			return -1;
			break;

		default: /* Succeed */
			send(*ClientSocket, ExtendableBuffer_GetData(Buffer), State, 0);

			if( ShowMassages == TRUE )
			{
				char InfoBuffer[3072];
				InfoBuffer[0] = '\0';
				GetAllAnswers(DNSGetDNSBody(ExtendableBuffer_GetData(Buffer)), InfoBuffer);

				if( Family == AF_INET )
				{
					PRINT("%s[%c][%s][%s][%s] :\n%s", DateAndTime, ProtocolCharacter, inet_ntoa(ClientAddr ->Addr4.sin_addr), DNSGetTypeName(SourceType), QueryDomain, InfoBuffer);
				} else {
					char Addr[LENGTH_OF_IPV6_ADDRESS_ASCII] = {0};

					IPv6AddressToAsc(&(ClientAddr -> Addr6.sin6_addr), Addr);

					PRINT("%s[%c][%s][%s][%s] :\n%s", DateAndTime, ProtocolCharacter, Addr, DNSGetTypeName(SourceType), QueryDomain, InfoBuffer);
				}
			}
			return 0;
			break;

	}
}