[Request] explain temporary files - mktemp
Welcome, Guest.
He could not keep viruses out of Windows! Do you want Bill Gates' MANDATORY Covid-19 Vaccine? Sign this petition!
"Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." -Benjamin Franklin

Author Topic: [Request] explain temporary files - mktemp  (Read 3776 times)

Offline JayVii

  • Contributor
  • *******
  • Posts: 1759
    • JayVii's Page
[Request] explain temporary files - mktemp
« on: June 19, 2016, 08:56:24 AM »
So Ben (a former Administrator on here) posted a script somehwere in the forum to block ads with your /etc/hosts-file. Back then I didn't quite understand what this thing really does, but only few weeks later I got more into bash and scripts and got a rough idea.

Well, today i'm much further into it and understand pretty much most shell-script i look at.
Also, I'm still maintaing and occasionally updating Ben's old script on one of my Git-Repos (NotABug).

The script makes use of temporary files ("Selfdestruct") to store adblock-lists in, before inserting them into your /etc/hosts using the mktemp command.
So yes, I completely understand what this command does (also read rough explainations on it here.

What I would like to know, though is:
Why does he use mktemp:


Code: [Select]
Selfdestructinghosts=$(mktemp)
wget -O - https://URL/TO/ADSERVERLIST1.txt >> $Selfdestructinghosts
wget -O - https://URL/TO/ADSERVERLIST2.txt >> $Selfdestructinghosts


instead of simply writing it all to a file manually like so:
Code: [Select]
wget -O - https://URL/TO/ADSERVER-LIST1.txt > /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST2.txt >> /tmp/adserverlist
or maybe:
Code: [Select]
touch /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST1.txt >> /tmp/adserverlist
wget -O - https://URL/TO/ADSERVER-LIST2.txt >> /tmp/adserverlist

Is it just "good practice" or are there any other benefits to it?


EDIT: If you need to see the script, to understand my question, see HERE (Line 18-19, Line 23-45 & the deletion of the temporary files in Line 54)


Thanks for explaining in advance :)

Offline misko_2083

  • Newbie
  • **
  • Posts: 12
Re: [Request] explain temporary files - mktemp
« Reply #1 on: April 25, 2017, 04:14:19 AM »
The script is run as root. If someone reads your script and does

Code: [Select]
ln -s /etc/passwd /tmp/adserverlistAnd you write to that temp file, it's going to be "ouch". It's better to have unpredictable file name. The attacker would have to have phisycal access to your computer but it's still a good practice.