Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can grep be malicious?

Status
Not open for further replies.

hfaix

MIS
Nov 25, 2003
596
US
Does anyone know of a way to edit a file (any way possible) with /usr/bin/grep? I hope not and I can't think of a way, but if anyone else can, then I would appreciate it. I was wondering if a search and replace was possible with grep. I don't want to do it and don't need an example, I just want to know if it's possible.

Thanks again,

 
not with the grep command exclusive, but can have piped commands that follow that can manipulate a file.



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
Or can be a home made grep program placed somewhere in PATH.
 

The following command will delete all contents of a file named myfile.

grep foo myfile > myfile

The reason for this is that the shell 'opens' the file that will be written to (in overwrite mode), before starting the grep process, or any other process preceeding the '>' character, effectively causing the grep to operate on an empty file.

This doesn't really 'edit' the file, but it does provide a way to modify the file in some way, and that is what you asked for.

Sneaky command lines could be constructed to modify the file, but this will all depend on the context.

C
 
Thanks! That's what I wanted to know. I appreciate it. I won't be allowing grep with sudo. I'm going to have to test the | with sudo commands.

Thanks again. This forum rocks.
 
i think you can allow grep with sudo because though you can do with grep what roylec said, it is only if you have permission to write to that file. note that the input and output file need not be the same and its only the output file that will be affected, that only when it is writeable. as long as the user is running a shell with a userid that does not have write access to that file, he cannot damage that file.

put simply, the proper way to protect such a file is through chmod. whether grep is made sudo or not does not make any difference at all. if the file is writeable, i can do a simple 'ls > file'. on the other hand if it is not writeable i cant do anything with ls or grep to damage that file.
 
you can do a

cat file > file

and it will do the same thing as the grep did in roylec's example. RR was correct in saying the using file and group permissions are a better way to protect a file.



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top