-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fetcher.go
134 lines (125 loc) · 3.04 KB
/
fetcher.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
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
package lastpass
import (
"golang.org/x/crypto/pbkdf2"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
)
type blob struct {
bytes []byte
keyIterationCount int
}
type session struct {
id string
keyIterationCount int
cookieJar http.CookieJar
}
func login(username, password string) (*session, error) {
iterationCount, err := requestIterationCount(username)
if err != nil {
return nil, err
}
return make_session(username, password, iterationCount)
}
func make_session(username, password string, iterationCount int) (*session, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
client := &http.Client{
Jar: cookieJar,
}
res, err := client.PostForm(
"https://lastpass.com/login.php",
url.Values{
"method": []string{"mobile"},
"web": []string{"1"},
"xml": []string{"1"},
"username": []string{username},
"hash": []string{string(makeHash(username, password, iterationCount))},
"iterations": []string{fmt.Sprint(iterationCount)},
})
if err != nil {
return nil, err
}
defer res.Body.Close()
var response struct {
SessionId string `xml:"sessionid,attr"`
}
err = xml.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &session{response.SessionId, iterationCount, cookieJar}, nil
}
func fetch(s *session) (*blob, error) {
u, err := url.Parse("https://lastpass.com/getaccts.php")
if err != nil {
return nil, err
}
u.RawQuery = (&url.Values{
"mobile": []string{"1"},
"b64": []string{"1"},
"hash": []string{"0.0"},
"PHPSESSID": []string{s.id},
}).Encode()
client := &http.Client{
Jar: s.cookieJar,
}
res, err := client.Get(u.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil && err != io.EOF {
return nil, err
}
b, err = base64.StdEncoding.DecodeString(string(b))
if err != nil {
return nil, err
}
return &blob{b, s.keyIterationCount}, nil
}
func requestIterationCount(username string) (int, error) {
res, err := http.DefaultClient.PostForm(
"https://lastpass.com/iterations.php",
url.Values{
"email": []string{username},
})
if err != nil {
return 0, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
count, err := strconv.Atoi(string(b))
if err != nil {
return 0, err
}
return count, nil
}
func makeKey(username, password string, iterationCount int) []byte {
if iterationCount == 1 {
b := sha256.Sum256([]byte(username + password))
return b[:]
}
return pbkdf2.Key([]byte(password), []byte(username), iterationCount, 32, sha256.New)
}
func makeHash(username, password string, iterationCount int) []byte {
key := makeKey(username, password, iterationCount)
if iterationCount == 1 {
b := sha256.Sum256([]byte(string(encodeHex(key)) + password))
return encodeHex(b[:])
}
return encodeHex(pbkdf2.Key([]byte(key), []byte(password), 1, 32, sha256.New))
}