1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The gitbrute command brute-forces a git commit hash prefix.
package main
import (
"bytes"
"crypto/sha1"
"flag"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
var (
prefix = flag.String("prefix", "bf", "Desired prefix")
force = flag.Bool("force", false, "Re-run, even if current hash matches prefix")
cpu = flag.Int("cpus", runtime.NumCPU(), "Number of CPUs to use. Defaults to number of processors.")
)
var (
start = time.Now()
startUnix = start.Unix()
)
func main() {
flag.Parse()
runtime.GOMAXPROCS(*cpu)
if _, err := strconv.ParseInt(*prefix, 16, 64); err != nil {
log.Fatalf("Prefix %q isn't hex.", *prefix)
}
hash := curHash()
if strings.HasPrefix(hash, *prefix) && !*force {
return
}
obj, err := exec.Command("git", "cat-file", "-p", hash).Output()
if err != nil {
log.Fatal(err)
}
i := bytes.Index(obj, []byte("\n\n"))
if i < 0 {
log.Fatalf("No \\n\\n found in %q", obj)
}
msg := obj[i+2:]
possibilities := make(chan try, 512)
go explore(possibilities)
winner := make(chan solution)
done := make(chan struct{})
for i := 0; i < *cpu; i++ {
go bruteForce(obj, winner, possibilities, done)
}
w := <-winner
close(done)
cmd := exec.Command("git", "commit", "--allow-empty", "--amend", "--date="+w.author.String(), "--file=-")
cmd.Env = append(os.Environ(), "GIT_COMMITTER_DATE="+w.committer.String())
cmd.Stdout = os.Stdout
cmd.Stdin = bytes.NewReader(msg)
if err := cmd.Run(); err != nil {
log.Fatalf("amend: %v", err)
}
}
type solution struct {
author, committer date
}
var (
authorDateRx = regexp.MustCompile(`(?m)^author.+> (.+)`)
committerDateRx = regexp.MustCompile(`(?m)^committer.+> (.+)`)
)
func bruteForce(obj []byte, winner chan<- solution, possibilities <-chan try, done <-chan struct{}) {
// blob is the blob to mutate in-place repeatedly while testing
// whether we have a match.
blob := []byte(fmt.Sprintf("commit %d\x00%s", len(obj), obj))
authorDate, adatei := getDate(blob, authorDateRx)
commitDate, cdatei := getDate(blob, committerDateRx)
s1 := sha1.New()
wantHexPrefix := []byte(strings.ToLower(*prefix))
hexBuf := make([]byte, 0, sha1.Size*2)
for t := range possibilities {
select {
case <-done:
return
default:
ad := date{startUnix - int64(t.authorBehind), authorDate.tz}
cd := date{startUnix - int64(t.commitBehind), commitDate.tz}
strconv.AppendInt(blob[:adatei], ad.n, 10)
strconv.AppendInt(blob[:cdatei], cd.n, 10)
s1.Reset()
s1.Write(blob)
if !bytes.HasPrefix(hexInPlace(s1.Sum(hexBuf[:0])), wantHexPrefix) {
continue
}
winner <- solution{ad, cd}
return
}
}
}
// try is a pair of seconds behind now to brute force, looking for a
// matching commit.
type try struct {
commitBehind int
authorBehind int
}
// explore yields the sequence:
// (0, 0)
//
// (0, 1)
// (1, 0)
// (1, 1)
//
// (0, 2)
// (1, 2)
// (2, 0)
// (2, 1)
// (2, 2)
//
// ...
func explore(c chan<- try) {
for max := 0; ; max++ {
for i := 0; i <= max-1; i++ {
c <- try{i, max}
}
for j := 0; j <= max; j++ {
c <- try{max, j}
}
}
}
// date is a git date.
type date struct {
n int64 // unix seconds
tz string
}
func (d date) String() string { return fmt.Sprintf("%d %s", d.n, d.tz) }
// getDate parses out a date from a git header (or blob with a header
// following the size and null byte). It returns the date and index
// that the unix seconds begins at within h.
func getDate(h []byte, rx *regexp.Regexp) (d date, idx int) {
m := rx.FindSubmatchIndex(h)
if m == nil {
log.Fatalf("Failed to match %s in %q", rx, h)
}
v := string(h[m[2]:m[3]])
space := strings.Index(v, " ")
if space < 0 {
log.Fatalf("unexpected date %q", v)
}
n, err := strconv.ParseInt(v[:space], 10, 64)
if err != nil {
log.Fatalf("unexpected date %q", v)
}
return date{n, v[space+1:]}, m[2]
}
func curHash() string {
all, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
log.Fatal(err)
}
h := string(all)
if i := strings.Index(h, "\n"); i > 0 {
h = h[:i]
}
return h
}
// hexInPlace takes a slice of binary data and returns the same slice with double
// its length, hex-ified in-place.
func hexInPlace(v []byte) []byte {
const hex = "0123456789abcdef"
h := v[:len(v)*2]
for i := len(v) - 1; i >= 0; i-- {
b := v[i]
h[i*2+0] = hex[b>>4]
h[i*2+1] = hex[b&0xf]
}
return h
}
|