mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
refactor: Optimize traversal browser data logic (#311)
* refactor: Refactor package names and imports for better code organization. * refactor: Package imports and variable types for consistency * chore: Disable unused-parameter rule in revive. * refactor: Refactor and organize data extraction and browserdata parse. * fix: rename wrong error message info
This commit is contained in:
+170
@@ -0,0 +1,170 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type DataType int
|
||||
|
||||
const (
|
||||
ChromiumKey DataType = iota
|
||||
ChromiumPassword
|
||||
ChromiumCookie
|
||||
ChromiumBookmark
|
||||
ChromiumHistory
|
||||
ChromiumDownload
|
||||
ChromiumCreditCard
|
||||
ChromiumLocalStorage
|
||||
ChromiumSessionStorage
|
||||
ChromiumExtension
|
||||
|
||||
YandexPassword
|
||||
YandexCreditCard
|
||||
|
||||
FirefoxKey4
|
||||
FirefoxPassword
|
||||
FirefoxCookie
|
||||
FirefoxBookmark
|
||||
FirefoxHistory
|
||||
FirefoxDownload
|
||||
FirefoxCreditCard
|
||||
FirefoxLocalStorage
|
||||
FirefoxSessionStorage
|
||||
FirefoxExtension
|
||||
)
|
||||
|
||||
var itemFileNames = map[DataType]string{
|
||||
ChromiumKey: fileChromiumKey,
|
||||
ChromiumPassword: fileChromiumPassword,
|
||||
ChromiumCookie: fileChromiumCookie,
|
||||
ChromiumBookmark: fileChromiumBookmark,
|
||||
ChromiumDownload: fileChromiumDownload,
|
||||
ChromiumLocalStorage: fileChromiumLocalStorage,
|
||||
ChromiumSessionStorage: fileChromiumSessionStorage,
|
||||
ChromiumCreditCard: fileChromiumCredit,
|
||||
ChromiumExtension: fileChromiumExtension,
|
||||
ChromiumHistory: fileChromiumHistory,
|
||||
YandexPassword: fileYandexPassword,
|
||||
YandexCreditCard: fileYandexCredit,
|
||||
FirefoxKey4: fileFirefoxKey4,
|
||||
FirefoxPassword: fileFirefoxPassword,
|
||||
FirefoxCookie: fileFirefoxCookie,
|
||||
FirefoxBookmark: fileFirefoxData,
|
||||
FirefoxDownload: fileFirefoxData,
|
||||
FirefoxLocalStorage: fileFirefoxLocalStorage,
|
||||
FirefoxHistory: fileFirefoxData,
|
||||
FirefoxExtension: fileFirefoxExtension,
|
||||
FirefoxSessionStorage: UnsupportedItem,
|
||||
FirefoxCreditCard: UnsupportedItem,
|
||||
}
|
||||
|
||||
// Filename returns the filename for the item, defined by browser
|
||||
// chromium local storage is a folder, so it returns the file name of the folder
|
||||
func (i DataType) Filename() string {
|
||||
if fileName, ok := itemFileNames[i]; ok {
|
||||
return fileName
|
||||
}
|
||||
return UnsupportedItem
|
||||
}
|
||||
|
||||
// TempFilename returns the temp filename for the item with suffix
|
||||
// eg: chromiumKey_0.temp
|
||||
func (i DataType) TempFilename() string {
|
||||
const tempSuffix = "temp"
|
||||
tempFile := fmt.Sprintf("%s_%d.%s", i.Filename(), i, tempSuffix)
|
||||
return filepath.Join(os.TempDir(), tempFile)
|
||||
}
|
||||
|
||||
// IsSensitive returns whether the item is sensitive data
|
||||
// password, cookie, credit card, master key is unlimited
|
||||
func (i DataType) IsSensitive() bool {
|
||||
switch i {
|
||||
case ChromiumKey, ChromiumCookie, ChromiumPassword, ChromiumCreditCard,
|
||||
FirefoxKey4, FirefoxPassword, FirefoxCookie, FirefoxCreditCard,
|
||||
YandexPassword, YandexCreditCard:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// FilterSensitiveItems returns the sensitive items
|
||||
func FilterSensitiveItems(items []DataType) []DataType {
|
||||
var filtered []DataType
|
||||
for _, item := range items {
|
||||
if item.IsSensitive() {
|
||||
filtered = append(filtered, item)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// DefaultFirefoxTypes returns the default items for the firefox browser
|
||||
var DefaultFirefoxTypes = []DataType{
|
||||
FirefoxKey4,
|
||||
FirefoxPassword,
|
||||
FirefoxCookie,
|
||||
FirefoxBookmark,
|
||||
FirefoxHistory,
|
||||
FirefoxDownload,
|
||||
FirefoxCreditCard,
|
||||
FirefoxLocalStorage,
|
||||
FirefoxSessionStorage,
|
||||
FirefoxExtension,
|
||||
}
|
||||
|
||||
// DefaultYandexTypes returns the default items for the yandex browser
|
||||
var DefaultYandexTypes = []DataType{
|
||||
ChromiumKey,
|
||||
ChromiumCookie,
|
||||
ChromiumBookmark,
|
||||
ChromiumHistory,
|
||||
ChromiumDownload,
|
||||
ChromiumExtension,
|
||||
YandexPassword,
|
||||
ChromiumLocalStorage,
|
||||
ChromiumSessionStorage,
|
||||
YandexCreditCard,
|
||||
}
|
||||
|
||||
// DefaultChromiumTypes returns the default items for the chromium browser
|
||||
var DefaultChromiumTypes = []DataType{
|
||||
ChromiumKey,
|
||||
ChromiumPassword,
|
||||
ChromiumCookie,
|
||||
ChromiumBookmark,
|
||||
ChromiumHistory,
|
||||
ChromiumDownload,
|
||||
ChromiumCreditCard,
|
||||
ChromiumLocalStorage,
|
||||
ChromiumSessionStorage,
|
||||
ChromiumExtension,
|
||||
}
|
||||
|
||||
// item's default filename
|
||||
const (
|
||||
fileChromiumKey = "Local State"
|
||||
fileChromiumCredit = "Web Data"
|
||||
fileChromiumPassword = "Login Data"
|
||||
fileChromiumHistory = "History"
|
||||
fileChromiumDownload = "History"
|
||||
fileChromiumCookie = "Cookies"
|
||||
fileChromiumBookmark = "Bookmarks"
|
||||
fileChromiumLocalStorage = "Local Storage/leveldb"
|
||||
fileChromiumSessionStorage = "Session Storage"
|
||||
fileChromiumExtension = "Secure Preferences" // TODO: add more extension files and folders, eg: Preferences
|
||||
|
||||
fileYandexPassword = "Ya Passman Data"
|
||||
fileYandexCredit = "Ya Credit Cards"
|
||||
|
||||
fileFirefoxKey4 = "key4.db"
|
||||
fileFirefoxCookie = "cookies.sqlite"
|
||||
fileFirefoxPassword = "logins.json"
|
||||
fileFirefoxData = "places.sqlite"
|
||||
fileFirefoxLocalStorage = "webappsstore.sqlite"
|
||||
fileFirefoxExtension = "extensions.json"
|
||||
|
||||
UnsupportedItem = "unsupported item"
|
||||
)
|
||||
@@ -0,0 +1,129 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDataType_FileName(t *testing.T) {
|
||||
for _, item := range DefaultChromiumTypes {
|
||||
assert.Equal(t, item.Filename(), item.filename())
|
||||
}
|
||||
for _, item := range DefaultFirefoxTypes {
|
||||
assert.Equal(t, item.Filename(), item.filename())
|
||||
}
|
||||
for _, item := range DefaultYandexTypes {
|
||||
assert.Equal(t, item.Filename(), item.filename())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataType_TempFilename(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
testCases := []struct {
|
||||
item DataType
|
||||
expected string
|
||||
}{
|
||||
{ChromiumKey, "Local State"},
|
||||
{ChromiumPassword, "Login Data"},
|
||||
{ChromiumLocalStorage, "Local Storage/leveldb"},
|
||||
{FirefoxSessionStorage, "unsupported item"},
|
||||
{FirefoxLocalStorage, "webappsstore.sqlite"},
|
||||
{YandexPassword, "Ya Passman Data"},
|
||||
{YandexCreditCard, "Ya Credit Cards"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
expectedPrefix := tc.expected + "_" + strconv.Itoa(int(tc.item)) + ".temp"
|
||||
actualPath := tc.item.TempFilename()
|
||||
asserts.Contains(actualPath, expectedPrefix, "TempFilename should contain the correct prefix for "+tc.expected)
|
||||
asserts.Contains(actualPath, os.TempDir(), "TempFilename should be in the system temp directory for "+tc.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataType_IsSensitive(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testCases := []struct {
|
||||
item DataType
|
||||
expected bool
|
||||
}{
|
||||
{ChromiumKey, true},
|
||||
{ChromiumPassword, true},
|
||||
{ChromiumBookmark, false},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
asserts.Equal(tc.expected, tc.item.IsSensitive(), fmt.Sprintf("IsSensitive for %v should be %v", tc.item, tc.expected))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterSensitiveItems(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testCases := []struct {
|
||||
items []DataType
|
||||
expected int
|
||||
}{
|
||||
{[]DataType{ChromiumKey, ChromiumBookmark, ChromiumPassword}, 2},
|
||||
{[]DataType{ChromiumBookmark, ChromiumHistory}, 0},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
filteredItems := FilterSensitiveItems(tc.items)
|
||||
asserts.Len(filteredItems, tc.expected, "FilterSensitiveItems should return the correct number of sensitive items")
|
||||
for _, item := range filteredItems {
|
||||
asserts.True(item.IsSensitive(), "Filtered items should be sensitive")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (i DataType) filename() string {
|
||||
switch i {
|
||||
case ChromiumKey:
|
||||
return fileChromiumKey
|
||||
case ChromiumPassword:
|
||||
return fileChromiumPassword
|
||||
case ChromiumCookie:
|
||||
return fileChromiumCookie
|
||||
case ChromiumBookmark:
|
||||
return fileChromiumBookmark
|
||||
case ChromiumDownload:
|
||||
return fileChromiumDownload
|
||||
case ChromiumLocalStorage:
|
||||
return fileChromiumLocalStorage
|
||||
case ChromiumSessionStorage:
|
||||
return fileChromiumSessionStorage
|
||||
case ChromiumCreditCard:
|
||||
return fileChromiumCredit
|
||||
case ChromiumExtension:
|
||||
return fileChromiumExtension
|
||||
case ChromiumHistory:
|
||||
return fileChromiumHistory
|
||||
case YandexPassword:
|
||||
return fileYandexPassword
|
||||
case YandexCreditCard:
|
||||
return fileYandexCredit
|
||||
case FirefoxKey4:
|
||||
return fileFirefoxKey4
|
||||
case FirefoxPassword:
|
||||
return fileFirefoxPassword
|
||||
case FirefoxCookie:
|
||||
return fileFirefoxCookie
|
||||
case FirefoxBookmark:
|
||||
return fileFirefoxData
|
||||
case FirefoxDownload:
|
||||
return fileFirefoxData
|
||||
case FirefoxLocalStorage:
|
||||
return fileFirefoxLocalStorage
|
||||
case FirefoxHistory:
|
||||
return fileFirefoxData
|
||||
case FirefoxExtension:
|
||||
return fileFirefoxExtension
|
||||
case FirefoxCreditCard:
|
||||
return UnsupportedItem
|
||||
default:
|
||||
return UnsupportedItem
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user