-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhibp_paste.go
102 lines (82 loc) · 1.78 KB
/
hibp_paste.go
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
/*
Copyright (c) 2017, AverageSecurityGuy
# All rights reserved.
Finds all pastes listed on Have I Been Pwnd for the specified email and
downloads each identified paste if it is available.
Usage:
$ go run hibp_paste.go email_address
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
)
type Paste struct {
Source string
Id string
}
func check(e error) {
if e != nil {
fmt.Printf("Error: %s\n", e.Error())
}
}
func get(url string) []byte {
fmt.Println(url)
resp, err := http.Get(url)
check(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
check(err)
if resp.StatusCode == 200 {
return body
} else {
fmt.Printf("Document not available: %d\n", resp.StatusCode)
return []byte("")
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run hibp_paste.go email_address")
os.Exit(1)
}
email := os.Args[1]
re := regexp.MustCompile(fmt.Sprintf(".*%s.*", email))
/*
Get Paste List from HIBP.
*/
url := fmt.Sprintf("https://haveibeenpwned.com/api/v2/pasteaccount/%s", email)
data := get(url)
var pastes []Paste
json.Unmarshal(data, &pastes)
/*
Download Each Paste
*/
for _, p := range pastes {
var url string
var data []byte
switch p.Source {
case "Pastebin":
url = fmt.Sprintf("https://pastebin.com/raw/%s", p.Id)
data = get(url)
case "Slexy":
url = fmt.Sprintf("http://slexy.org/raw/%s", p.Id)
data = get(url)
default:
fmt.Printf("Paste source does not support raw viewing - %s: %s\n", p.Source, p.Id)
data = []byte("")
}
// Print only the data that matches our email.
matches := re.FindAllString(string(data), -1)
if len(matches) > 0 {
fmt.Println(strings.Join(matches, "\n"))
}
fmt.Println("")
time.Sleep(10 * time.Second)
}
}