HOSTS file

For discussions about security.
Message
Author
labbe5
Posts: 2159
Joined: Wed 13 Nov 2013, 14:26
Location: Canada

HOSTS file

#1 Post by labbe5 »

There is a good chance you are using a hosts file to block ads, banners, 3rd party Cookies, 3rd party page counters, web bugs, and even most hijackers.

Puppy can provide an app to do just that, it's called Pup Advert Blocker. But if you want to dig into the subject deeper, and see what you can do with a few command lines and a terminal for any desktop environment and Linux OS, have a look at this :

How to do this via a graphical file manager will vary, depending on which file manager and which desktop environment you're using. But, you can do this very easily from the command line, and it's the same for all desktop environments. If you downloaded your new hosts file to your Downloads directory, and called it hosts.txt, you can complete all the steps by entering the following at a command line prompt:

cd Downloads
tr -d '\15\32' < ./hosts.txt > ./hosts.new
su
<enter root password>
cd /etc
cp hosts hosts.bak
cp /home/<your-username>/Downloads/hosts.new /etc/hosts
(Answer "y" when asked if you want to overwrite the hosts file)

Source for this information is found here : http://pclosmag.com/html/Issues/201504/page06.html

linuxcbon
Posts: 1312
Joined: Thu 09 Aug 2007, 22:54

#2 Post by linuxcbon »

Hi labbe5,
do you know what the commands are doing at least ? what is Download ? what is tr ? what is su ? what is hosts.new for ? It's not recommended to give such code which doesn't apply to puppy.
And it's not even recommended to download hosts files from anywhere, better do it yourself.
Cheers.

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

#3 Post by Scooby »

linuxcbon wrote: do you know what the commands are doing at least ?
Explanation:

Code: Select all

cd Downloads
It is assumed you did download the file "hosts" to $HOME/Downloads
where $HOME is your users home directory

Code: Select all

 tr -d '\15\32' < ./hosts.txt > ./hosts.new
Use tr to remove the CR used at the end of every line in a Windows/DOS text file, leaving just a LF character at the end of every line, as *nix text files expect. If you don't perform this step on a hosts file that is formatted for Windows/DOS (and the one at winhelp2002.mvps.org is formatted for Windows/DOS), it won't work correctly under *nix. Your *nix computer will see it as one long line, and the lines won't be properly parsed.

Code: Select all

 su
 <enter root password>
login as root

Code: Select all

 cd /etc
Go to /etc dir

Code: Select all

cp hosts hosts.bak
 
MAke a backup of hosts file

Code: Select all

cp /home/<your-username>/Downloads/hosts.new /etc/hosts 
copy the new hosts file to /etc and ovewrite the old one
( I would do this with -f flag to force overwrite)




Or perhaps you meant more generally that code should be explained
Last edited by Scooby on Wed 15 Apr 2015, 20:08, edited 1 time in total.

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

#4 Post by Scooby »

linuxcbon wrote: And it's not even recommended to download hosts files from anywhere, better do it yourself.
Cheers.
If you want check it you could do something like

Code: Select all

cat /etc/hosts | grep -v ^0.0.0.0 | grep -v ^\#

That is show the lines that are not comments and are not mapped
to 0.0.0.0

If these doesn't show anything suspicious I wouldn't be scared to
use the hosts file form internet

User avatar
Galbi
Posts: 1098
Joined: Wed 21 Sep 2011, 22:32
Location: Bs.As. - Argentina.

#5 Post by Galbi »

Opening the hosts file with a *nix editor, let's say, Geany, and saving it again, will it then be in *nix format?

Tnx.
Remember: [b][i]"pecunia pecuniam parere non potest"[/i][/b]

linuxcbon
Posts: 1312
Joined: Thu 09 Aug 2007, 22:54

#6 Post by linuxcbon »

@Scooby I meant it's not nice to give a code without explanations and which is not so useful, because opening and saving in geany achieves the same result.
It's not recommended to use hosts files from the internet because many servers inside are no longer existing and so the big size of the hosts file will take more ram than needed. And that's why I add or remove servers manually. I even wrote a howto.

@Galbi yes that's it :) .

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post by musher0 »

Hello, everyone.

In line with linuxcbon's comment:

The authors of winhelp2002.mvps.org are pretty strict and have a heavy
hand. For ex. they block the excellent search engine info.com
(very useful to search rare awk or bash examples, among other things).

To get my info.com back, I had to comment line 3864 of the hosts file
like so:
# 0.0.0.0 ccs.infospace.com
A few years back they had blocked die.net, the international repository
for Linux man files... I had to send them an e-mail about it. (I was not the
only one who noticed, I'm sure.)

Remember: those guys are well-meaning, but they are WhineDose
experts, not linux users. They put in their hosts file anything that appears
suspicious to a WhineDose user.

@labbe5: Puppy does not use a su or a sudo command. Please adapt
your references to the Puppy environment before you dump them on us.
Thank you. (You do that a lot, unfortunately, whatever the value of the
info you provide...)

@Galbi: I use leafpad to change End-of-Line (EOL) characters to Line-
Feed (LF) only. It's easier and clearer to do than in geany. (Does geany
really do it? Check the file size before and after, you'll see.)

BFN.

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#8 Post by mikeb »

I add some of the more annoying ad sites to my routers blocklist...eg google ads and facebook are sooo common.

Check yer routers interface as its nice to do it there if you have several machines....or several systems :D

linefeeds...there is that perl dos2unix script that seems to be in puppy ...I have used it to deal with sources that won't build dues to similar problem.
Geany tends to maintain whatever the file is/has.

mike

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

#9 Post by Scooby »

linuxcbon wrote:@Scooby I meant it's not nice to give a code without explanations and which is not so useful, because opening and saving in geany achieves the same result.
yeah well say someone wanted to automate download and
install it would be useful to know how to do it from cmdline
I would use the tool suggested by mikeb dos2unix but
now I learnt how to do it with tr
linuxcbon wrote: It's not recommended to use hosts files from the internet because many servers inside are no longer existing and so the big size of the hosts file will take more ram than needed.
well this certainly sounds like a task that would be easily automated.
Read hosts file and check if they still exist. Maybe one could use dnslookup for this??????

The problem of good sites being nulled is of course
a little bit annoying but easy enough to edit out in hosts file.
In my opinion the ads are more annoying still.

Anyway I saw musher say that puppy doesn't have su
How do you switch user in puppy then?

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#10 Post by mikeb »

Anyway I saw musher say that puppy doesn't have su
How do you switch user in puppy then?
I believe he was saying giving examples of code using su would confuse in puppy so need to be edited to suit.... su is usually around. No sudo and its fiddly to make work too as I found on the other hand.

mike

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

mikeb wrote:
Anyway I saw musher say that puppy doesn't have su
How do you switch user in puppy then?
I believe he was saying giving examples of code using su would confuse in puppy so need to be edited to suit.... su is usually around. No sudo and its fiddly to make work too as I found on the other hand.

mike
Thanks, mikeb.

Yeah, I meant: "edit the stuff you quote" for Puppy; adapt it. If you are
posting in the Puppy forum, you should show some knowledge of Puppy.
Unlike me (see paragraph below; shame-shame-shame). ;)

I stand corrected: yep, there is a < su > in Puppy, but no < sudo >.

BFN.

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#12 Post by mikeb »

As your personal secretary now I hope you are not expecting any weekend perks :shock:

mike

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#13 Post by musher0 »

mikeb wrote:As your personal secretary now I hope you are not expecting any weekend perks :shock:

mike
I wouldn't go out with you, no. :shock:
But a coffee would be nice. :lol:
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

#14 Post by Scooby »

*DOESN'T WORK ON PUPPY*

Did the automation of checking if servers are no longer exising.

It's a hack

Download it here


The processed_hosts.tar.gz archive contains script for checking a hosts file
and output of a run of it that is a hosts file with dead servers removed

The script check_hosts uses getent to check if servers are alive
Maybe not existent in puppy?? In alphaos I aint got dig or hosts or even
dnslookup. But I guess script could be adapted to use any of them

Takes quite a bit of time to run it

10549 of 29351 servers removed so I guess linuxcbon was right in issue a warning about that


Anyway is there a troll-warning going on in regards to labbe5?
I enjoy his posts in security and think they are informative
Last edited by Scooby on Fri 17 Apr 2015, 20:38, edited 1 time in total.

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#15 Post by mikeb »

Anyway is there a troll-warning going on in regards to labbe5?
don't see why... promotes good discussion from my experience..

now about the troll warning about me.... the rumours are true.
10549 of 29351 servers removed so I guess linuxcbon was right in issue a warning about that
that would indeed be a lot of wasted overhead.

mike

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#16 Post by musher0 »

Hi, Scooby.

Thanks for that. I'll be trying it in a minute.

As to labbe5, I don't think he's a troll, far from it. I believe he's well-
meaning, and the info that he provides can indeed be useful, if somewhat
specialized at times. He just needs to be more aware of the PuppyLinux
context and edit his info accordingly, that's all.

BFN.

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#17 Post by musher0 »

Hello again, Scooby.

A couple of thoughts:
* Have you informed the authors of this hosts lists that they have a lot of
dead wood in their list?
* Maybe provide an already processed, ready-to-use, hosts list for the
PuppyLinux newbies, as well as your script?

... script still working... :)

BFN.

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

linuxcbon
Posts: 1312
Joined: Thu 09 Aug 2007, 22:54

#18 Post by linuxcbon »

About the code, yes it doesn't apply to puppy and easier is to save in geany...
I used to use ready-made hosts files but then I used a similar program to Scooby's using dnslookup and noticed too many servers were down. Plus the fact that using such stuff, you lose control of what is going on, they can block sites that you need etc. So I decided to manually create my own hosts file. I wrote a howto and it's easy, the hardest are the 100 first lines. Now it is 1400 lines...but even with 20 lines, you already notice a difference. 8)

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

#19 Post by Scooby »

musher0 wrote: * Have you informed the authors of this hosts lists that they have a lot of
dead wood in their list?
nope
musher0 wrote: * Maybe provide an already processed, ready-to-use, hosts list for the
PuppyLinux newbies, as well as your script
Actually there was a cleaned hosts file
in the archive availible for download

However it included my hostname I
have to edit and re-upload

*EDIT* edited out my hostname and re-uploaded

Scooby
Posts: 599
Joined: Sat 03 Mar 2012, 09:04

SOLVED

#20 Post by Scooby »

SOLVED

My system was setup to always do DNS

Now I created /etc/nsswitch.conf
and added line

Code: Select all

hosts:      files dns nis 

Now it will check /etc/hosts first and then if that fails try DNS

The fix also means that my script doesn't work anymore
and since this is default behaviour on puppy my script won't
work on them


------------------------------------------------------------------
I just noticed an ad from googleads.g.doubleclick.net
and thought to manually hack it in my hosts file

But to my chagrin it was already there


how come it isn't blocked?

I have

Code: Select all

network.dns.disablePrefetch=true

Also it seems the html is of either version below

Code: Select all

<script src="http://pagead2.googlesyndication.com/pagead/js/r20150414/r20110914/abg.js"></script>


<img src="http://pagead2.googlesyndication.com/simgad/14629189109557694214" class="img_ad" onload="tick('1ad');" border="0" width="200">
Why isn't it blocked?

Also when I try at the cmd line

Code: Select all

 getent hosts pagead2.googlesyndication.com 
2a00:1450:400f:803::1019 pagead46.l.doubleclick.net pagead2.googlesyndication.com
I get this? Is this some IPv6 trickery?

Any input why Sh** isn't blocked?

I tried adding the site with the www prefix also???

Also tried 127.0.0.1 instead of 0.0.0.0 as suggested in post in linuxcbon
howto thread
Last edited by Scooby on Fri 17 Apr 2015, 20:37, edited 1 time in total.

Post Reply