API equivalent to etc/hosts? - winapi

Is there a Windows (or Windows Device Driver) API that's equivalent to editing the etc/hosts file, i.e. to override or change the hostname-to-IPaddress mapping?
I'd want for a scenario where DNS might not exist on a LAN, or where the hosts in question aren't known to DNS, but where I know the host name and the IP addresses.
If not, then I take it that editing etc/hosts programmatically is the only way to do this?
How often or how soon does an edit to etc/hosts take effect -- i.e. if I edit it then will my next network connection request to a given host name use the new IP address, or is a stale value cached somewhere?

Windows has the file %WINDIR%\System32\drivers\etc\hosts which is exactly equivalent to the *nix /etc/hosts file.

Related

set browser to open local address instead of internet address

I use 2 NICs card on my windows, one of the local and another one is connected to the internet, now my question is how can I set the specific browser to open just the Local website instead of the internet? I set the route command to route the IP address that I want to open locally but when I check the logs I find the IP open with internet NIC instead of Local NIC
There two methods of getting this done as per your request the first method would work for you:-
The first method:-
we can use static route route but we have to obtain the IDs for our NICs by running the command below:-
netsh interface ipv4 show interfaces
The above command will get you the NIC ID
Add a persistent route by using the command below in CMD this will lock the specified traffic from a specific network to the specified network interface using the NIC IDx see syntax command below via CMD.
route -p add local_subnet mask local_subnet_mask 172.132.45.201 IF
interface_ID
For example command below, please change the values as per your environment or network:-
route -p add 192.168.0.6 mask 255.255.255.255 172.132.45.201 IF 13
The Second Method:
You will need to edit your hosts file that will enable you to override the DNS for a domain on a specific machine.
Modifying your hosts file causes your machine to look directly at the IP address that you specify
Modifying the hosts file involves adding entries to it to the file . Each entry contains the IP address to which you want the site to resolve.
192.168.190.4 www.local_domain.com
192.168.190.4 local_domain.com
13.14.15.16 www.external_domain.com
13.14.15.16 external_domain.com
Below are the steps to edit the hosts file in Windows 10/11 and Windows Server 2012 and newer versions (you didn't specify which version of Windows).
Press the Windows key.
Type Notepad in the search field.
In the search results, right-click Notepad and select Run as administrator.
From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts
Make the necessary changes to the file.
Select File > Save to save your changes.
For Windows, you may have to reboot your computer and this will work independently of the NIC setup.

Blocking websites from DNS Level

Is it possible to block website from DNS Level. Is it possible to create DNS server in windows server 2012?
I know you can do it pre-DNS pevel. Prior to DNS lookup, windows will check the hosts file for IP to domain mapping. You can set facebook.com to 192.168.1.1 and it will use that ip for facebook rather than looking it up in DNS, and thus blocking it. This would require modifying the host file on every machine you want to sensor though. Its a file in the system32 folder.

Windows - etc/hosts configuration not working

I made this entry in my C:\windows\system32\drivers\etc\hosts file:
convert2mp3.net convert2mp3.com
so that my browser always calls convert2mp3.net instead of convert2mp3.com if i accidentially call convert2mp3.com
But it does not work. I also flushed the DNS cache by executing ipconfig /flushdns in the CMD. I also tried to use another browser but still no redirect.
What am I doing wrong?
The /etc/host file is used to force the resolution of a domain to a specified IP Address. Your line is missing the ip Address help for win7
It should be:
127.0.0.1 mywebsite
You cannot change DNS names like that, especially not in an HTTP context.
So the problem is you sometimes type a .com address in your browser where the site you want to go to actually is at the same address - only ending in ".net".
Even if you could change the hosts file to let the .com address point to the .net server's address, the request is likely to return an error, because their server isn't configured to handle requests to the .com address, which your browser will still send as a host header.
So create a bookmark in your browser and click that, instead of typing DNS addresses yourself if you're making that mistake that often.

Is there a way to simulate DNS Hijacking for testing purposes?

I'm working on an application that connects to URLs, and it responds differently depending on whether or not an address resolves in DNS. I need to find a way to simulate DNS Hijacking so that I can test that my application handles it correctly.
Anybody know a way to do that?
Set up a DNS Server on a second pc and use this as your referenced DNS. Then you can shutdown it for sometime or modify the answers to try your handling behavior. If you dont have a second machine you can also set it up in a virtual machine.
Just add the "hijacked" hosts into your hosts file. In Linux, this should be in /etc/hosts; in Windows, %SYSTEMDIR%\drivers\etc\hosts..
The entries are in the format ip.addr.ess.here hostname1 hostname2 (there should already be entries for localhost, so add others to match your taste)
When you're done, remove (or comment out) from the hosts file again.

How do I create a subdomain on windows?

I just want to add a couple subdomains to my computers FQDN so that I can have some Vitrual Servers (apache.)
Any ideas?
Note: I need to add the aliases to my machine, like a.foo.com and b.foo.com. I don't think apache makes those aliases.
Alternative: I decided that instead of trying to add stuff to my domain name (since then I'd have to edit the dns server) I would configure virtual hosts for a separate port. That way it's all up to my computer. Thanks for the help nonetheless!
If this is on a Windows domain: go into the DNS management tool on the DC, and add host entries (CN records, not A records) pointing the desired names to the original computer name.
If you're doing this all on one (non-server) box: edit the hosts file, and point the names to the right IP address.

Resources