|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "AttackSec/backend/passwordAnalysis" |
| 5 | + "AttackSec/backend/passwordModifier" |
| 6 | + "AttackSec/backend/passwordValidator" |
| 7 | + "AttackSec/backend/smartFilter" |
| 8 | + "AttackSec/backend/utilities" |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "github.com/sqweek/dialog" |
| 12 | + "github.com/wailsapp/wails/v2/pkg/runtime" |
| 13 | + "log" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | + "sync" |
| 17 | +) |
| 18 | + |
| 19 | +type App struct { |
| 20 | + ctx context.Context |
| 21 | +} |
| 22 | + |
| 23 | +func NewApp() *App { |
| 24 | + return &App{} |
| 25 | +} |
| 26 | + |
| 27 | +func (a *App) OnDomReady(ctx context.Context) {} |
| 28 | + |
| 29 | +func (a *App) Startup(ctx context.Context) { |
| 30 | + a.ctx = ctx |
| 31 | +} |
| 32 | + |
| 33 | +func (a *App) BeforeClose(ctx context.Context) (prevent bool) { |
| 34 | + messageDialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ |
| 35 | + Type: runtime.QuestionDialog, |
| 36 | + Title: "AttackSecurity", |
| 37 | + Message: "Are you sure you want to quit?", |
| 38 | + }) |
| 39 | + |
| 40 | + if err != nil { |
| 41 | + return false |
| 42 | + } |
| 43 | + return messageDialog != "Yes" |
| 44 | +} |
| 45 | + |
| 46 | +func (a *App) ExitProgram() { |
| 47 | + os.Exit(0) |
| 48 | +} |
| 49 | + |
| 50 | +func (a *App) GetServiceNames() []string { |
| 51 | + var serviceNames []string |
| 52 | + for _, policy := range passwordValidator.PasswordPolicies { |
| 53 | + serviceNames = append(serviceNames, policy.ServiceName) |
| 54 | + } |
| 55 | + return serviceNames |
| 56 | +} |
| 57 | + |
| 58 | +func (a *App) GetCategories() []string { |
| 59 | + var categories []string |
| 60 | + for _, policy := range passwordModifier.PasswordPolicies { |
| 61 | + categories = append(categories, policy.Category) |
| 62 | + } |
| 63 | + return categories |
| 64 | +} |
| 65 | + |
| 66 | +func (a *App) FileDialog(fileType string) string { |
| 67 | + inputFilePath, _ := dialog.File().Filter(fmt.Sprintf(".%v files", fileType), fileType).Title(fmt.Sprintf("Select .%v File", fileType)).Load() |
| 68 | + return inputFilePath |
| 69 | +} |
| 70 | + |
| 71 | +func (a *App) InitializeConfig() utilities.Settings { |
| 72 | + loadedSettings, err := utilities.LoadSettings() |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("Failed to load utilities: %v", err) |
| 75 | + } |
| 76 | + return *loadedSettings |
| 77 | +} |
| 78 | + |
| 79 | +func (a *App) CheckCustomPolicy(filePath string) (bool, error) { |
| 80 | + isValid, err := passwordValidator.CheckCustomPolicy(filePath) |
| 81 | + return isValid, err |
| 82 | +} |
| 83 | + |
| 84 | +func (a *App) CheckCustomPolicyModifier(filePath string) (bool, error) { |
| 85 | + isValid, err := passwordModifier.CheckCustomPolicy(filePath) |
| 86 | + return isValid, err |
| 87 | +} |
| 88 | + |
| 89 | +func (a *App) RunPasswordValidator(filePath, preset, presetPath string) passwordValidator.Output { |
| 90 | + var passed, failed int |
| 91 | + var mu sync.Mutex |
| 92 | + |
| 93 | + var passedLines, failedLines []string |
| 94 | + |
| 95 | + lines, err := utilities.ReadLines(filePath) |
| 96 | + if err != nil { |
| 97 | + return passwordValidator.Output{Error: err, Success: false} |
| 98 | + } |
| 99 | + |
| 100 | + settings, err := utilities.LoadSettings() |
| 101 | + if err != nil { |
| 102 | + return passwordValidator.Output{Error: err, Success: false} |
| 103 | + } |
| 104 | + lineFormat := settings.LineFormat |
| 105 | + |
| 106 | + worker := func(task interface{}) error { |
| 107 | + line := task.(string) |
| 108 | + pass := "" |
| 109 | + switch lineFormat { |
| 110 | + case "user:pass": |
| 111 | + pass = strings.Split(line, ":")[1] |
| 112 | + case "pass:user": |
| 113 | + pass = strings.Split(line, ":")[0] |
| 114 | + case "pass": |
| 115 | + pass = line |
| 116 | + } |
| 117 | + |
| 118 | + valid, reason, _ := passwordValidator.ValidatePassword(preset, presetPath, pass) |
| 119 | + mu.Lock() |
| 120 | + if valid { |
| 121 | + passed++ |
| 122 | + passedLines = append(passedLines, line) |
| 123 | + } else { |
| 124 | + failed++ |
| 125 | + failedLines = append(failedLines, fmt.Sprintf("%v [%v]", reason, line)) |
| 126 | + } |
| 127 | + mu.Unlock() |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + tasks := make([]interface{}, len(lines)) |
| 132 | + for i, line := range lines { |
| 133 | + tasks[i] = line |
| 134 | + } |
| 135 | + |
| 136 | + if err := utilities.ConcurrentProcessor(tasks, worker, settings.MaxWorkers); err != nil { |
| 137 | + return passwordValidator.Output{Error: err, Success: false} |
| 138 | + } |
| 139 | + |
| 140 | + data := map[string][]string{ |
| 141 | + "passed": passedLines, |
| 142 | + "failed": failedLines, |
| 143 | + } |
| 144 | + |
| 145 | + if err := utilities.ExportResults(data, "Password Validator"); err != nil { |
| 146 | + log.Fatalf("Export failed: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + return passwordValidator.Output{ |
| 150 | + Passed: passed, |
| 151 | + Failed: failed, |
| 152 | + Lines: len(lines), |
| 153 | + Error: nil, |
| 154 | + Success: true, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func (a *App) RunPasswordModifier(filePath, preset, presetPath string) passwordModifier.Output { |
| 159 | + var passed, failed int |
| 160 | + var mu sync.Mutex |
| 161 | + |
| 162 | + var modifiedLines, failedLines []string |
| 163 | + |
| 164 | + lines, err := utilities.ReadLines(filePath) |
| 165 | + if err != nil { |
| 166 | + return passwordModifier.Output{Error: err, Success: false} |
| 167 | + } |
| 168 | + |
| 169 | + settings, err := utilities.LoadSettings() |
| 170 | + if err != nil { |
| 171 | + return passwordModifier.Output{Error: err, Success: false} |
| 172 | + } |
| 173 | + lineFormat := settings.LineFormat |
| 174 | + |
| 175 | + worker := func(task interface{}) error { |
| 176 | + line := task.(string) |
| 177 | + pass, user := "", "" |
| 178 | + switch lineFormat { |
| 179 | + case "user:pass": |
| 180 | + parts := strings.Split(line, ":") |
| 181 | + if len(parts) == 2 { |
| 182 | + user = parts[0] |
| 183 | + pass = parts[1] |
| 184 | + } |
| 185 | + case "pass:user": |
| 186 | + parts := strings.Split(line, ":") |
| 187 | + if len(parts) == 2 { |
| 188 | + pass = parts[0] |
| 189 | + user = parts[1] |
| 190 | + } |
| 191 | + case "pass": |
| 192 | + pass = line |
| 193 | + } |
| 194 | + |
| 195 | + valid, modifiedPass, reason, _ := passwordModifier.ModifyPassword(preset, presetPath, user, pass) |
| 196 | + mu.Lock() |
| 197 | + if valid { |
| 198 | + passed++ |
| 199 | + modifiedLines = append(modifiedLines, modifiedPass) |
| 200 | + } else { |
| 201 | + failed++ |
| 202 | + failedLines = append(failedLines, fmt.Sprintf("%v [%v]", reason, line)) |
| 203 | + } |
| 204 | + mu.Unlock() |
| 205 | + return nil |
| 206 | + } |
| 207 | + |
| 208 | + tasks := make([]interface{}, len(lines)) |
| 209 | + for i, line := range lines { |
| 210 | + tasks[i] = line |
| 211 | + } |
| 212 | + |
| 213 | + if err := utilities.ConcurrentProcessor(tasks, worker, settings.MaxWorkers); err != nil { |
| 214 | + return passwordModifier.Output{Error: err, Success: false} |
| 215 | + } |
| 216 | + |
| 217 | + data := map[string][]string{ |
| 218 | + "modified": modifiedLines, |
| 219 | + "failed": failedLines, |
| 220 | + } |
| 221 | + |
| 222 | + if err := utilities.ExportResults(data, "Password Modifier"); err != nil { |
| 223 | + log.Fatalf("Export failed: %v", err) |
| 224 | + } |
| 225 | + |
| 226 | + return passwordModifier.Output{ |
| 227 | + Modified: passed, |
| 228 | + Failed: failed, |
| 229 | + Lines: len(lines), |
| 230 | + Error: nil, |
| 231 | + Success: true, |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +func (a *App) RunSmartFilter(mode, filePath, patterns, format string, blockSize int) []string { |
| 236 | + lines, err := utilities.ReadLines(filePath) |
| 237 | + if err != nil { |
| 238 | + return nil |
| 239 | + } |
| 240 | + |
| 241 | + config := smartFilter.Config{ |
| 242 | + Patterns: strings.Split(patterns, "\n"), |
| 243 | + BlockSize: blockSize, |
| 244 | + Format: format, |
| 245 | + } |
| 246 | + |
| 247 | + result := smartFilter.SmartFilter(mode, lines, config) |
| 248 | + |
| 249 | + data := map[string][]string{ |
| 250 | + "extracted": result, |
| 251 | + } |
| 252 | + |
| 253 | + if err := utilities.ExportResults(data, "Smart Filter"); err != nil { |
| 254 | + log.Fatalf("Export failed: %v", err) |
| 255 | + } |
| 256 | + |
| 257 | + return result |
| 258 | +} |
| 259 | + |
| 260 | +func (a *App) RunPasswordAnalysis(filePath string) passwordAnalysis.Result { |
| 261 | + lines, err := utilities.ReadLines(filePath) |
| 262 | + if err != nil { |
| 263 | + return passwordAnalysis.Result{Error: err, Success: false} |
| 264 | + } |
| 265 | + |
| 266 | + result := passwordAnalysis.AnalysisPassword(lines) |
| 267 | + |
| 268 | + data := map[string][]string{ |
| 269 | + "analysed": {result.String()}, |
| 270 | + } |
| 271 | + |
| 272 | + if err := utilities.ExportResults(data, "Password Analysis"); err != nil { |
| 273 | + log.Fatalf("Export failed: %v", err) |
| 274 | + } |
| 275 | + |
| 276 | + return result |
| 277 | +} |
| 278 | + |
| 279 | +func (a *App) ShowTutorial() bool { |
| 280 | + return utilities.ShowTutorial() |
| 281 | +} |
| 282 | + |
| 283 | +func (a *App) ChangeSettings(newSettings *utilities.Settings) error { |
| 284 | + return utilities.ChangeSettings(newSettings) |
| 285 | +} |
| 286 | + |
| 287 | +func (a *App) RestartApplication() { |
| 288 | + utilities.RestartSelf() |
| 289 | +} |
| 290 | + |
| 291 | +func (a *App) CheckCrash() bool { |
| 292 | + var isCrashed bool |
| 293 | + |
| 294 | + utilities.CrashReporter(func() { |
| 295 | + err := os.Remove(utilities.GetFilePath("crash_reports.json")) |
| 296 | + if err != nil { |
| 297 | + fmt.Errorf(err.Error()) |
| 298 | + return |
| 299 | + } |
| 300 | + isCrashed = true |
| 301 | + }) |
| 302 | + return isCrashed |
| 303 | +} |
| 304 | + |
| 305 | +func (a *App) TutorialCompleted() { |
| 306 | + utilities.TutorialCompleted() |
| 307 | +} |
0 commit comments