HackBrowserData/core/common.go

291 lines
6.6 KiB
Go
Raw Normal View History

2020-06-25 20:33:23 +00:00
package core
2020-06-19 12:43:31 +00:00
import (
"database/sql"
"hack-browser-data/log"
"hack-browser-data/utils"
2020-06-23 10:30:00 +00:00
"os"
2020-06-25 20:33:23 +00:00
"sort"
2020-06-22 09:23:18 +00:00
"time"
2020-06-19 12:43:31 +00:00
_ "github.com/mattn/go-sqlite3"
2020-06-22 09:23:18 +00:00
"github.com/tidwall/gjson"
2020-06-19 12:43:31 +00:00
)
2020-06-22 09:23:18 +00:00
const (
bookmarkID = "id"
bookmarkAdded = "date_added"
bookmarkUrl = "url"
bookmarkName = "name"
bookmarkType = "type"
bookmarkChildren = "children"
)
2020-06-25 20:33:23 +00:00
var (
2020-06-28 09:18:02 +00:00
FullDataSlice []*BrowserData
FullData = new(BrowserData)
2020-06-25 20:33:23 +00:00
)
2020-06-19 12:43:31 +00:00
type (
BrowserData struct {
BrowserName string
2020-06-25 20:33:23 +00:00
LoginDataSlice
BookmarkSlice
CookieMap
HistorySlice
2020-06-19 12:43:31 +00:00
}
2020-06-25 20:33:23 +00:00
LoginDataSlice []loginData
BookmarkSlice []bookmarks
CookieMap map[string][]cookies
HistorySlice []history
loginData struct {
2020-06-25 09:37:18 +00:00
UserName string
2020-06-25 20:33:23 +00:00
encryptPass []byte
2020-06-25 09:37:18 +00:00
Password string
LoginUrl string
CreateDate time.Time
2020-06-19 12:43:31 +00:00
}
2020-06-25 20:33:23 +00:00
bookmarks struct {
ID int64
2020-06-25 09:37:18 +00:00
DateAdded time.Time
URL string
Name string
Type string
2020-06-19 12:43:31 +00:00
}
2020-06-25 20:33:23 +00:00
cookies struct {
2020-06-22 10:34:41 +00:00
KeyName string
encryptValue []byte
Value string
Host string
Path string
IsSecure bool
IsHTTPOnly bool
HasExpire bool
IsPersistent bool
CreateDate time.Time
ExpireDate time.Time
2020-06-19 12:43:31 +00:00
}
2020-06-25 20:33:23 +00:00
history struct {
2020-06-23 02:42:10 +00:00
Url string
Title string
VisitCount int
LastVisitTime time.Time
2020-06-19 12:43:31 +00:00
}
)
2020-06-28 09:18:02 +00:00
func ParseResult(dbname string) {
2020-06-22 09:23:18 +00:00
switch dbname {
case utils.Bookmarks:
parseBookmarks()
2020-06-23 02:42:10 +00:00
case utils.History:
parseHistory()
2020-06-28 09:18:02 +00:00
case utils.Cookies:
parseCookie()
case utils.LoginData:
parseLogin()
2020-06-22 09:23:18 +00:00
}
}
2020-06-25 20:33:23 +00:00
var bookmarkList BookmarkSlice
2020-06-22 09:23:18 +00:00
func parseBookmarks() {
bookmarks, err := utils.ReadFile(utils.Bookmarks)
2020-06-25 20:33:23 +00:00
defer os.Remove(utils.Bookmarks)
2020-06-22 09:23:18 +00:00
if err != nil {
log.Println(err)
}
r := gjson.Parse(bookmarks)
if r.Exists() {
roots := r.Get("roots")
roots.ForEach(func(key, value gjson.Result) bool {
2020-06-22 10:34:41 +00:00
getBookmarkChildren(value)
2020-06-22 09:23:18 +00:00
return true
})
}
2020-06-25 20:33:23 +00:00
sort.Slice(bookmarkList, func(i, j int) bool {
return bookmarkList[i].ID < bookmarkList[j].ID
})
FullData.BookmarkSlice = bookmarkList
2020-06-22 09:23:18 +00:00
}
2020-06-22 10:34:41 +00:00
var queryLogin = `SELECT origin_url, username_value, password_value, date_created FROM logins`
2020-06-22 09:23:18 +00:00
2020-06-23 02:42:10 +00:00
func parseLogin() {
2020-06-25 20:33:23 +00:00
var loginItemList LoginDataSlice
login := loginData{}
2020-06-22 10:34:41 +00:00
loginDB, err := sql.Open("sqlite3", utils.LoginData)
2020-06-25 20:33:23 +00:00
defer os.Remove(utils.LoginData)
2020-06-19 12:43:31 +00:00
defer func() {
2020-06-22 10:34:41 +00:00
if err := loginDB.Close(); err != nil {
2020-06-19 12:43:31 +00:00
log.Println(err)
}
}()
if err != nil {
log.Println(err)
}
2020-06-22 10:34:41 +00:00
err = loginDB.Ping()
rows, err := loginDB.Query(queryLogin)
2020-06-19 12:43:31 +00:00
defer func() {
if err := rows.Close(); err != nil {
log.Println(err)
}
}()
for rows.Next() {
var (
2020-06-22 10:34:41 +00:00
url, username, password string
pwd []byte
create int64
2020-06-19 12:43:31 +00:00
)
2020-06-22 09:23:18 +00:00
err = rows.Scan(&url, &username, &pwd, &create)
2020-06-25 20:33:23 +00:00
login = loginData{
2020-06-19 12:43:31 +00:00
UserName: username,
2020-06-25 20:33:23 +00:00
encryptPass: pwd,
2020-06-19 12:43:31 +00:00
LoginUrl: url,
}
2020-06-28 09:18:02 +00:00
if utils.VersionUnder80 {
password, err = utils.DecryptStringWithDPAPI(pwd)
} else {
password, err = utils.DecryptChromePass(pwd)
}
if create > time.Now().Unix() {
login.CreateDate = utils.TimeEpochFormat(create)
} else {
login.CreateDate = utils.TimeStampFormat(create)
}
2020-06-22 10:34:41 +00:00
login.Password = password
2020-06-19 12:43:31 +00:00
if err != nil {
log.Println(err)
}
2020-06-23 02:42:10 +00:00
loginItemList = append(loginItemList, login)
2020-06-19 12:43:31 +00:00
}
2020-06-25 20:33:23 +00:00
sort.Sort(loginItemList)
FullData.LoginDataSlice = loginItemList
2020-06-19 12:43:31 +00:00
}
2020-06-22 09:23:18 +00:00
2020-06-22 10:34:41 +00:00
var queryCookie = `SELECT name, encrypted_value, host_key, path, creation_utc, expires_utc, is_secure, is_httponly, has_expires, is_persistent FROM cookies`
2020-06-23 02:42:10 +00:00
func parseCookie() {
2020-06-25 20:33:23 +00:00
cookie := cookies{}
cookieMap := make(map[string][]cookies)
2020-06-22 10:34:41 +00:00
cookieDB, err := sql.Open("sqlite3", utils.Cookies)
2020-06-25 20:33:23 +00:00
defer os.Remove(utils.Cookies)
2020-06-22 10:34:41 +00:00
defer func() {
if err := cookieDB.Close(); err != nil {
log.Println(err)
}
}()
if err != nil {
log.Println(err)
}
err = cookieDB.Ping()
rows, err := cookieDB.Query(queryCookie)
defer func() {
if err := rows.Close(); err != nil {
log.Println(err)
}
}()
for rows.Next() {
var (
key, host, path, value string
2020-06-23 02:42:10 +00:00
isSecure, isHTTPOnly, hasExpire, isPersistent int
2020-06-22 10:34:41 +00:00
createDate, expireDate int64
encryptValue []byte
)
err = rows.Scan(&key, &encryptValue, &host, &path, &createDate, &expireDate, &isSecure, &isHTTPOnly, &hasExpire, &isPersistent)
2020-06-25 20:33:23 +00:00
cookie = cookies{
2020-06-22 10:34:41 +00:00
KeyName: key,
Host: host,
Path: path,
encryptValue: encryptValue,
2020-06-23 02:42:10 +00:00
IsSecure: utils.IntToBool(isSecure),
IsHTTPOnly: utils.IntToBool(isHTTPOnly),
HasExpire: utils.IntToBool(hasExpire),
IsPersistent: utils.IntToBool(isPersistent),
2020-06-22 10:34:41 +00:00
CreateDate: utils.TimeEpochFormat(createDate),
ExpireDate: utils.TimeEpochFormat(expireDate),
}
2020-06-25 09:37:18 +00:00
// remove prefix 'v10'
2020-06-28 09:18:02 +00:00
if utils.VersionUnder80 {
value, err = utils.DecryptStringWithDPAPI(encryptValue)
} else {
value, err = utils.DecryptChromePass(encryptValue)
}
2020-06-25 20:33:23 +00:00
cookie.Value = value
if _, ok := cookieMap[host]; ok {
cookieMap[host] = append(cookieMap[host], cookie)
} else {
cookieMap[host] = []cookies{cookie}
}
2020-06-22 10:34:41 +00:00
}
2020-06-25 20:33:23 +00:00
FullData.CookieMap = cookieMap
2020-06-22 10:34:41 +00:00
}
2020-06-23 10:30:00 +00:00
var queryHistory = `SELECT url, title, visit_count, last_visit_time FROM urls`
2020-06-22 09:23:18 +00:00
2020-06-23 02:42:10 +00:00
func parseHistory() {
2020-06-25 20:33:23 +00:00
var historyList HistorySlice
h := history{}
2020-06-23 02:42:10 +00:00
historyDB, err := sql.Open("sqlite3", utils.History)
2020-06-25 20:33:23 +00:00
defer os.Remove(utils.History)
2020-06-23 02:42:10 +00:00
defer func() {
if err := historyDB.Close(); err != nil {
log.Println(err)
}
}()
if err != nil {
log.Println(err)
}
err = historyDB.Ping()
2020-06-23 10:30:00 +00:00
rows, err := historyDB.Query(queryHistory)
2020-06-23 02:42:10 +00:00
defer func() {
if err := rows.Close(); err != nil {
log.Println(err)
}
}()
for rows.Next() {
var (
url, title string
visitCount int
lastVisitTime int64
)
err := rows.Scan(&url, &title, &visitCount, &lastVisitTime)
2020-06-25 20:33:23 +00:00
h = history{
2020-06-23 02:42:10 +00:00
Url: url,
Title: title,
VisitCount: visitCount,
LastVisitTime: utils.TimeEpochFormat(lastVisitTime),
}
if err != nil {
log.Println(err)
continue
}
2020-06-25 20:33:23 +00:00
historyList = append(historyList, h)
2020-06-23 02:42:10 +00:00
}
2020-06-25 20:33:23 +00:00
sort.Slice(historyList, func(i, j int) bool {
return historyList[i].VisitCount > historyList[j].VisitCount
})
FullData.HistorySlice = historyList
2020-06-22 09:23:18 +00:00
}
2020-06-22 10:34:41 +00:00
func getBookmarkChildren(value gjson.Result) (children gjson.Result) {
2020-06-25 20:33:23 +00:00
b := bookmarks{}
b.ID = value.Get(bookmarkID).Int()
2020-06-22 10:34:41 +00:00
nodeType := value.Get(bookmarkType)
b.DateAdded = utils.TimeEpochFormat(value.Get(bookmarkAdded).Int())
b.URL = value.Get(bookmarkUrl).String()
b.Name = value.Get(bookmarkName).String()
children = value.Get(bookmarkChildren)
if nodeType.Exists() {
b.Type = nodeType.String()
bookmarkList = append(bookmarkList, b)
if children.Exists() && children.IsArray() {
for _, v := range children.Array() {
children = getBookmarkChildren(v)
}
}
}
return children
2020-06-22 09:23:18 +00:00
}