Professional SEO Logo

 

>> Client Login

Email:
Password:
Home Services Contact Us Portfolio Mission Search Site Blog  
                 

April 6, 2010

Finding a Text String in Protected Files on Mac OS X

Filed under: Mac OS X Tips — Eric @ 4:15 pm

Looking for strings of text in thousands of files at one time on a Mac can usually be accomplished using a simple "Find" function in a program such as BBedit. This method becomes problematic when you want to search for text strings within hidden files or files with restricted permissions such as config files in the hidden directories like /private or /usr.

Why would someone want to look in these folders? We for example recently had problems with our DNS changing hostnames without our knowledge and wanted to track down how this was happening. Because we knew the hostname was changing from powerbookG4.local to gmail.local we simply wanted to search for all system config files that might contain the text string "gmail".

After much searching and educating ourselves we found a simple solution using the command "grep" and Mac OS X's command line tool, Terminal.app. Because the text string "gmail" could appear in lots of files that might contain a gmail email address such as  myemail@gmail.com we made use of Regular Expressions to exclude text strings that would be email addresses and would thus give us false positives.

Here is the solution:

1. Open Terminal.app

2. change to root access by typing at the prompt # su root

Last login: Mon Apr  1 14:22:31 on console

powerbookG4:~ user$ su root

3. Enter your password when prompted

Password:

4. Navigate to the directory of files that you want to search using the cd command.

5. Enter the grep command to search for the appropriate text string: grep -irl '[^(@|o|n)]gmail[^(\.com)]' *

sh-3.2# grep -irl '[^(@|o|n)]gmail[^(\.com)]' *

I won't get into all the details of how grep works but the flags we used were -i (for case insensitive) -r (to search all files within the directory recursively) and -l (to list all the results). The next section is the regular expression of the text string you want to search for. Lastly the wildcard * just means to search in all files. If you wanted to only search in html files for example you could use *.html.

We used the regular expressions such as ^ to exclude any gmail email addresses and only find files where "gmail" occurred and exclude any strings such as xxxx@gmail.com.

After grep runs it will list all the files that contain your text string. Example list of files below.

var/log/asl/2010.04.04.U501.asl
var/log/asl/LongTTL.asl
var/log/daily.out
var/log/install.log
var/log/krb5kdc/kdc.log
var/log/samba/log.nmbd
var/log/secure.log
var/log/system.log

This method can be very useful if for example you know a certain virus string and are trying to find all the files on your system that have been affected by the virus string. An example of this would be for instance last summer's iframe injection trojans.


April 3, 2010

How to Clear the DNS Cache in Mac OSX Leopard

Filed under: Mac OS X Tips — Eric @ 7:40 pm

The Macintosh operating system and most computer operating systems for that matter will cache DNS requests. That is a good thing as it speeds up the DNS lookups within the same host. But sometimes this can become problematic when the cache contains values that are no longer valid or legitimate. Also nefarious software, viruses, or spyware can poison a DNS cache. Thus it can be necessary to clear (or reset) the DNS cache on your Mac and  to do that in Mac (OS Tiger and below) you open the Terminal.app program usually found within your Applications/Utilities/ folder and you type the following command:

lookupd -flushcache

However a Mac OSX 10.5 Leopard user will tell you this command did not work on their terminal. In Leopard a new command has been used to replace flushcache, so to clear DNScache in Leopard, just type this in your Terminal and hit enter.

dscacheutil -flushcache