• Stars
    star
    1,655
  • Rank 27,206 (Top 0.6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A wrapper around grep, to help you grep for things

gf

A wrapper around grep to avoid typing common patterns.

What? Why?

I use grep a lot. When auditing code bases, looking at the output of meg, or just generally dealing with large amounts of data. I often end up using fairly complex patterns like this one:

â–¶ grep -HnrE '(\$_(POST|GET|COOKIE|REQUEST|SERVER|FILES)|php://(input|stdin))' *

It's really easy to mess up when typing all of that, and it can be hard to know if you haven't got any results because there are non to find, or because you screwed up writing the pattern or chose the wrong flags.

I wrote gf to give names to the pattern and flag combinations I use all the time. So the above command becomes simply:

â–¶ gf php-sources

Pattern Files

The pattern definitions are stored in ~/.gf as little JSON files that can be kept under version control:

â–¶ cat ~/.gf/php-sources.json
{
    "flags": "-HnrE",
    "pattern": "(\\$_(POST|GET|COOKIE|REQUEST|SERVER|FILES)|php://(input|stdin))"
}

To help reduce pattern length and complexity a little, you can specify a list of multiple patterns too:

â–¶ cat ~/.gf/php-sources-multiple.json
{
    "flags": "-HnrE",
    "patterns": [
        "\\$_(POST|GET|COOKIE|REQUEST|SERVER|FILES)",
        "php://(input|stdin)"
    ]
}

There are some more example pattern files in the examples directory.

You can use the -save flag to create pattern files from the command line:

â–¶ gf -save php-serialized -HnrE '(a:[0-9]+:{|O:[0-9]+:"|s:[0-9]+:")'

Auto Complete

There's an auto-complete script included, so you can hit 'tab' to show you what your options are:

â–¶ gf <tab>
base64       debug-pages  fw           php-curl     php-errors   php-sinks    php-sources  sec          takeovers    urls

Bash

To get auto-complete working you need to source the gf-completion.bash file in your .bashrc or similar:

source ~/path/to/gf-completion.bash

Zsh

To get auto-complete working you need to enable autocomplete (not needed if you have oh-my-zsh) using autoload -U compaudit && compinit or by putting it into .zshrc

Then source the gf-completion.zsh file in your .zshrc or similar:

source ~/path/to/gf-completion.zsh

Note: if you're using oh-my-zsh or similar you may find that gf is an alias for git fetch. You can either alias the gf binary to something else, or unalias gf to remove the git fetch alias.

Using custom engines

There are some amazing code searching engines out there that can be a better replacement for grep. A good example is the silver searcher. It's faster (like way faster) and presents the results in a more visually digestible manner. In order to utilize a different engine, add engine: <other tool> to the relevant pattern file:

# Using the silver searcher instead of grep for the aws-keys pattern:
# 1. Adding "ag" engine
# 2. Removing the E flag which is irrelevant for ag
{
  "engine": "ag",
  "flags": "-Hanr",
  "pattern": "([^A-Z0-9]|^)(AKIA|A3T|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{12,}"
}
  • Note: Different engines use different flags, so in the example above, the flag E has to be removed from the aws-keys.json file in order for ag to successfully run.

Install

If you've got Go installed and configured you can install gf with:

â–¶ go get -u github.com/tomnomnom/gf

If you've installed using go get, you can enable auto-completion to your .bashrc like this:

â–¶ echo 'source $GOPATH/src/github.com/tomnomnom/gf/gf-completion.bash' >> ~/.bashrc

Note that you'll have to restart your terminal, or run source ~/.bashrc for the changes to take effect.

To get started quickly, you can copy the example pattern files to ~/.gf like this:

â–¶ cp -r $GOPATH/src/github.com/tomnomnom/gf/examples ~/.gf

My personal patterns that I've included as examples might not be very useful to you, but hopefully they're still a reasonable point of reference.

Contributing

I'd actually be most interested in new pattern files! If you've got something you regularly grep for then feel free to issue a PR to add new pattern files to the examples directory.

Bug fixes are also welcome as always :)

More Repositories

1

gron

Make JSON greppable!
Go
13,551
star
2

waybackurls

Fetch all the URLs that the Wayback Machine knows about for a domain
Go
3,189
star
3

assetfinder

Find domains and subdomains related to a given domain
Go
2,818
star
4

httprobe

Take a list of domains and probe for working HTTP and HTTPS servers
Go
2,728
star
5

hacks

A collection of hacks and one-off scripts
Go
2,056
star
6

meg

Fetch many paths for many hosts - without killing the hosts
Go
1,548
star
7

anew

A tool for adding new lines to files, skipping duplicates
Go
1,229
star
8

unfurl

Pull out bits of URLs provided on stdin
Go
972
star
9

qsreplace

Accept URLs on stdin, replace all query string values with a user-supplied value
Go
687
star
10

fff

The Fairly Fast Fetcher. Requests a bunch of URLs provided on stdin fairly quickly.
Go
364
star
11

dotfiles

.vimrc, .bashrc etc
Vim Script
324
star
12

rawhttp

A Go library for making HTTP requests with complete control
Go
116
star
13

linkheader

Golang HTTP Link header parser
Go
90
star
14

burl

A Broken-URL Checker
Go
78
star
15

concurl

Make concurrent requests with the curl command-line tool
Go
70
star
16

comb

Combine the lines from two files in every combination
Go
46
star
17

gahttp

Async / concurrent HTTP requests for Go
Go
45
star
18

blocksort

A tool for sorting blocks of lines
Go
34
star
19

securitytxt

A security.txt parser for Go
Go
31
star
20

vumeter

Little HTML canvas VU meter visualisation
JavaScript
28
star
21

phargs

A toolkit for writing CLI scripts in PHP
PHP
25
star
22

xtermcolor

Golang package and command to convert color.Colour to the nearest xterm/bash/shell color
Go
25
star
23

eater-cpu

Go
23
star
24

ASCIIPoint

An ASCII presentation tool in PHP
PHP
20
star
25

twarch

A Twitter Archive thing
PHP
16
star
26

symwatch

A tool to run a command when the target of a symlink changes
Go
15
star
27

cowtalks

A shell script for running lightning talks with a cow as a compere.
Shell
14
star
28

getgithubrepos

A tool to list the SSH clone URLs for all GitHub repos for a given user
Go
12
star
29

phpsecuritytxt

A security.txt parser for PHP
PHP
11
star
30

sheep

I can't draw
JavaScript
10
star
31

globwatch

Golang package to watch a glob pattern for changes.
Go
10
star
32

tomnomnom.github.io

GitHub Pages
HTML
9
star
33

new-stuff-in-php-5.4

For PHP Leeds
PHP
8
star
34

tomnomnom.com

Source for tomnomnom.com
PHP
8
star
35

phpwol

Wake On LAN for PHP
PHP
8
star
36

git-talk

Slides for teaching PlatOps people about git
6
star
37

go-learning

Bits and pieces of Go while I'm learning
Go
6
star
38

Pwas

A webserver written in PHP
PHP
5
star
39

PHP-Evolution-Sim

Basic evolution simulated in PHP
PHP
4
star
40

gotemplate

Template for my Go projects
Shell
4
star
41

leedshack2018

A stack-based VM and assembler, built in 2 days at Leeds Hack 2018
Go
4
star
42

Writing-Testable-PHP

Talk from The Digital Barn
PHP
4
star
43

rplex

A simple general purpose lexer library for Go
Go
4
star
44

fclock

Toy rotating bar clock canvas thing
JavaScript
3
star
45

graphite-client

Really simple plain-text graphite client
JavaScript
3
star
46

api.tomnomnom.com

API shenanigans
PHP
3
star
47

finishingtouchautos.co.uk

Source for finishingtouchautos.co.uk
CSS
3
star
48

tomhudson.co.uk

Source for tomhudson.co.uk
PHP
3
star
49

ansible-play

Playing with ansible
Smarty
3
star
50

build-a-vm-talk

Code and slides from my Hey!Stac talk at the Belgrave in Leeds on the 28th of October 2014.
PHP
2
star
51

crtmas

Merry CRTmas
JavaScript
2
star
52

myfirstwebsite

Test repo plz ignore
PHP
2
star
53

tacho

HTML Canvas Tachometer
JavaScript
2
star
54

flatclass

Flatten deep inheritance trees in PHP to aid debugging
PHP
2
star
55

bouncy.tomnomnom.com

Canvas Bouncy Ball
JavaScript
2
star
56

sbt.org.uk

Source for sbt.org.uk
PHP
2
star
57

branchdemo

CPU Branch Predictor Demo
Go
2
star
58

All-About-SPL

All About SPL talk for LeedsPHP
PHP
2
star
59

readable-code

Slides for Readable Code talk
PHP
1
star
60

victoria-hudson.co.uk

victoria-hudson.co.uk source
PHP
1
star
61

n-things-about-mongo

Slides from LeedsPHP talk on 2013-08-19
1
star
62

numbers.tomnomnom.com

Source for numbers.tomnomnom.com
Go
1
star
63

unit-testing

Unit testing examples
PHP
1
star
64

node-in-production-talk

Node In Production talk slides etc for NodeUpNorth
JavaScript
1
star
65

fixaholic.uk

Source for fixaholic.uk
PHP
1
star
66

rainbow-waves

Canvas Rainbow Waves
JavaScript
1
star
67

Sbt

SBT library of... stuff.
PHP
1
star