16 Ocak 2020 Perşembe

DNS Metodları

Not:
Bazı IP-String veya String-IP dönüşümleri için DNS sunucusu gerekmez. Bu tür dönüşümler için IP-String Dönüşüm Metodları başlıklı yazıya göz atabilirsiniz.

DNS Nedir?
DNS verilen bilgisayar ismini IP adresine çevirir. DNS sunucularına IP adresleri ile erişiriz. Örneğin google dns sunucusunu IP adresi 8.8.8.8'dir.  Her DNS sunucusu bir zone ile ilgilidir. Master DNS, Slave DNS, Caching DNS gibi farklı çeşitleri olabilir.

DNS sunucuları genelde 53 numaralı port üzerinden UDP kullanır. Nadiren TCP kullanıldığı da olur. Açıklaması şöyle.
Normal DNS queries use UDP port 53, but longer queries (> 512 octets) will receive a 'truncated' reply, that results in a TCP 53 conversation to facilitate sending/receiving the entire query.
DNS sorgusunda her pakette tek bir soru olmalıdır. Aynı pakette A ve MX kaydı sorgulanamaz.

DNS isimleri büyük küçük harfe duyarlı değildir. Teknik dokümanlarda genelde küçük harf kullanılır.

DNS Kullanmak İstemezsek
Açıklaması şöyle
If one explicitly uses the IP address no DNS lookup is needed to access the server. Note though that most servers will not work if just accessed by IP address. It is pretty common that multiple domains are served on the same IP address. Thus the server also needs the domain name to find out which certificates to provide for HTTPS and which content to serve. If one just puts the IP address in the browser, the browser does not know which domain is meant and thus cannot provide this crucial information to the server.
DNS Over HTTP - DoH
2017 senesinde ortaya çıkan yeni bir öneri. Açıklaması şöyle
DNS over HTTPS (DoH) intends to solve the privacy concerns there are with unencrypted DNS
Firefox bir şekilde DoH destekliyor

DNS Over TLS - DoT
Bazıları DoT yönteminin DoH yöntemine göre daha avantajlı olduğunu söylüyor ancak sebebini anlamadım. Soru şöyle. DoT sorguları şifreli olarak TCP üzerinden 853 numaralı porta gönderir.
I've heard the argument against DNS-over-HTTPS that it is supposed to be a security nightmare for network defenders because it enables encrypted DNS over port 443, compared to DNS-over-TLS which goes through port 853.
DNS Sunucuları Arasında Senkronizasyon
Açıklaması şöyle
DNS also uses zone transfers (query type AXFR) to update other DNS servers with new records.
...
So your security audit should pay close attention to query type AXFR, and your DNS systems should only accept AXFR exchanges from specific IP addresses.
Açıklaması şöyle
Zone transfers always use TCP, due to zone transfers taking up >512 bytes by their very nature. (it would be a waste of bandwidth to begin with UDP at all)
Authoritative Name Server
whois komutu yazısına taşıdım.

TTL
TTL'in çok hızlı değiştirilmesi yöntemi malware (zararlı yazılım) tarafından kullanılıyor. Açıklaması şöyle. Flux akış demek.
This is called Fast Flux DNS Records. And it's usually how malware authors hide their infrastructure servers.
Hosts Dosyası
Windows'ta bazı IP'lerin DNS kullanılmadan çözümlenmesi istenirse c:\windows\system32\drivers\etc\hosts dosyası düzenlenebilir. Dosya şuna benzer
# 127.0.0.1       localhost
# ::1             localhost

Linux'ta ise /etc/hosts dosyası düzenlenebilir. Dosya şuna benzer
# ip           hostname
172.16.0.100   berry
DNS Header
Kod olarak şöyledir.
// DNS layer header's structure
struct dnsheader {
  unsigned short int query_id;
  unsigned short int flags;
  unsigned short int QDCOUNT;
  unsigned short int ANCOUNT;
  unsigned short int NSCOUNT;
  unsigned short int ARCOUNT;
};
dig komutu
dig komutu yazısına taşıdım.

A Record Nedir?
DNS Record Tipleri yazısına taşıdım.

AAAA Record Nedir?
DNS Record Tipleri yazısına taşıdım.

CName Nedir?
DNS Record Tipleri yazısına taşıdım.

MX Record Nedir?
DNS Record Tipleri yazısına taşıdım.

TXT Record Nedir?
DNS Record Tipleri yazısına taşıdım.

Ters DNS Nedir?
Ters DNS (Reverse DNS) verilen IP adresini sunucu ismine çevirir. Özellikle e-posta sunucuları tarafından kullanılır. Gönderilen e-postaların içine e-posta sunucusu kendi IP adresini ekler. Alıcı taraf IP adresini ters DNS sunucusunda sorgulayarak IP adresi ile e-posta adresini eşleştirmeye çalışır. İşlem başarılıysa e-postayı içeri alır, değilse dikkate almaz. Ters DNS, DNS standardının bir parçasıdır.

Ters DNS normal IP adresleri için çalışmayabilir. Ters DNS için getnameinfo() metodu kullanılır.

Örnek
Şöyle yaparız. Belirtilen string şeklindeki IP için tüm adresler bulunur. Daha sonra ilk adres için getnameinfo() çağrısı yapılır.
struct addrinfo *ai;
struct addrinfo hints = {
    .ai_flags = AI_NUMERICHOST,
    .ai_family = AF_UNSPEC,
};

int res = getaddrinfo(argv[1], NULL, &hints, &ai);
if (res) {
    fprintf(stderr, "%s: %s\n", argv[1], gai_strerror(res));
    return 1;
}

char node[NI_MAXHOST];
res = getnameinfo(ai->ai_addr, ai->ai_addrlen, node, sizeof node, NULL, 0, NI_NAMEREQD);
freeaddrinfo(ai);

if (res) {
    fprintf(stderr, "%s: %s\n", argv[1], gai_strerror(res));
    exit(1);
}
Ubuntu
DNS ayarlarını değiştirdikten sonra
sudo service network-manager restart yapmak gerekir.

Metin Domain İsmi veya Metin IP ile Bulma 
Bazı metodlar sadece IP donüşüm (address translation) yaparken, bazıları isim çözümlemesi (name lookup) yapar.

gethostbyaddr
gethostbyaddr yazısına taşıdım.

getaddrinfo - posix
getaddrinfo metodu yazısına taşıdım.

Java
InetAddress Sınıfı yazısına taşıdım.


C#
DNS Sınıfı yazısına taşıdım.

Sadece Metin Domain İsmi ile Bulma
Aşağıdaki metodların tamamı artık kullanılmamalıdır. Sadece eski kodlarda karşımıza çıkabildikleri için not aldım.

gethostbyname - superseded by getaddrinfo
gethostbyname yazısına taşıdım.

gethostbyname_r - superseded by getaddrinfo
gethostbyname statik bellek alanı kullandığı için kullanımı tavsiye edilmiyor. Bunun yerine gethostbyname_r kullanılabilir. Aşağıdaki örnekte hata kontrolü yok: Metoda geçilen hostbuf ve result parametrelerinin farkını ben de tam anlamadım.

string hostname = "myhost";
struct hostent hostbuf;
struct hostent *result;
int herr;

int tmplen = 1024;
char *tmp = malloc (tmplen);//tamponun büyüklüğünün yeterli olup olmadığını kontrol etmiyoruz
int hres = gethostbyname_r (hostname.c_str(),&hostbuf,tmp,tmplen,&result, &herr);

if (ret == 0 && result != NULL) //Hata yoksa
{

    char** pAddr = result->h_addr_list;
    while(*pAddr)
    {
        //(*pAddr)[0],(*pAddr)[1] ile ip adresini al
        ++pAddr;
    }
}
free(tmp);
   

Kendi Sunucumuzun İsmini Bulma
gethostname
Bu metod ile kendi sunucumuzun ismini bulabiliriz. Örnek:

Yukarıdaki örnekte 80 karakterlik yer ayrılmış. Ancak HOST_NAME_MAX macrosu kadar yer ayırmak yeterli. Böylece farklı platformlarda bile çalışan bir kod yazmış oluruz.

Java
InetAddress.getHostName() metodu ile yapılır.

C#
Environment.MachineName ile bulunabilir.

Kendi Sunucumuzun IP Adresini Bulma
Bir sunucunun birden fazla IP adresi olabilir!
C#
DNS sınıfı yazısına taşıdım.

Hiç yorum yok:

Yorum Gönder