-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogAnalyzer.java
More file actions
333 lines (283 loc) · 12.8 KB
/
Copy pathLogAnalyzer.java
File metadata and controls
333 lines (283 loc) · 12.8 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LogAnalyzerApp extends JFrame {
private JTextPane logTextPane;
private JTextPane resultTextPane;
private File currentFile;
private PatternInfo[] patternInfos;
private JPanel legendPanel;
public LogAnalyzerApp() {
setTitle("Android Log Analyzer");
setSize(1000, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(5, 5));
getContentPane().setBackground(new Color(45, 45, 48));
// Initialize pattern information with colors
initializePatterns();
// Create components
createUI();
// Center on screen
setLocationRelativeTo(null);
}
private void initializePatterns() {
patternInfos = new PatternInfo[]{
new PatternInfo("Permission Issues",
Pattern.compile("(?i)permission denied|avc:.*denied"),
new Color(220, 20, 60)), // Crimson
new PatternInfo("Security Exceptions",
Pattern.compile("(?i)security exception|invalid credential"),
new Color(255, 140, 0)), // Dark Orange
new PatternInfo("Root Access",
Pattern.compile("(?i)root access|su command"),
new Color(50, 205, 50)), // Lime Green
new PatternInfo("Malware/Trojans",
Pattern.compile("(?i)malware|trojan|virus|backdoor"),
new Color(138, 43, 226)), // Violet
new PatternInfo("Unauthorized Access",
Pattern.compile("(?i)unauthorized (access|attempt)|bruteforce"),
new Color(0, 191, 255)), // Deep Sky Blue
new PatternInfo("Kernel Issues",
Pattern.compile("(?i)kernel panic|segfault|Oops\\[#\\d+\\]|Call Trace:"),
new Color(255, 215, 0)), // Gold
new PatternInfo("SELinux Denials",
Pattern.compile("(?i)avc: denied"),
new Color(255, 105, 180)), // Hot Pink
new PatternInfo("Debugging Issues",
Pattern.compile("(?i)debuggerd.*signal 11"),
new Color(64, 224, 208)), // Turquoise
new PatternInfo("Package Issues",
Pattern.compile("(?i)package .* does not belong to|invalid package"),
new Color(147, 112, 219)) // Medium Purple
};
}
private void createUI() {
// Create a dark-themed panel for buttons
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
topPanel.setBackground(new Color(30, 30, 32));
// Create buttons with modern styling
JButton browseButton = createStyledButton("Browse Log File", new Color(70, 130, 180));
JButton analyzeButton = createStyledButton("Analyze for Suspicious Activity", new Color(46, 139, 87));
JButton clearButton = createStyledButton("Clear Results", new Color(205, 92, 92));
browseButton.addActionListener(this::browseLogFile);
analyzeButton.addActionListener(this::analyzeLog);
clearButton.addActionListener(e -> {
resultTextPane.setText("");
logTextPane.setText("");
currentFile = null;
});
topPanel.add(browseButton);
topPanel.add(analyzeButton);
topPanel.add(clearButton);
add(topPanel, BorderLayout.NORTH);
// Create split pane for log display and results
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setResizeWeight(0.6);
splitPane.setDividerSize(3);
splitPane.setBorder(BorderFactory.createEmptyBorder());
// Create log display area
JPanel logPanel = new JPanel(new BorderLayout());
logPanel.setBorder(BorderFactory.createTitledBorder("Android Log Content"));
logTextPane = new JTextPane();
logTextPane.setEditable(false);
logTextPane.setBackground(new Color(25, 25, 28));
logTextPane.setForeground(Color.LIGHT_GRAY);
logTextPane.setFont(new Font("Monospaced", Font.PLAIN, 13));
JScrollPane logScroll = new JScrollPane(logTextPane);
logScroll.setBorder(BorderFactory.createEmptyBorder());
logPanel.add(logScroll, BorderLayout.CENTER);
// Create results display area
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.setBorder(BorderFactory.createTitledBorder("Analysis Results"));
resultTextPane = new JTextPane();
resultTextPane.setEditable(false);
resultTextPane.setBackground(new Color(25, 25, 28));
resultTextPane.setForeground(Color.WHITE);
resultTextPane.setFont(new Font("Monospaced", Font.PLAIN, 13));
JScrollPane resultScroll = new JScrollPane(resultTextPane);
resultScroll.setBorder(BorderFactory.createEmptyBorder());
resultPanel.add(resultScroll, BorderLayout.CENTER);
splitPane.setTopComponent(logPanel);
splitPane.setBottomComponent(resultPanel);
add(splitPane, BorderLayout.CENTER);
// Create legend panel
legendPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
legendPanel.setBorder(BorderFactory.createTitledBorder("Pattern Legend"));
legendPanel.setBackground(new Color(30, 30, 32));
legendPanel.setForeground(Color.WHITE);
updateLegend();
add(legendPanel, BorderLayout.SOUTH);
}
private JButton createStyledButton(String text, Color bgColor) {
JButton button = new JButton(text);
button.setBackground(bgColor);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setFont(new Font("Segoe UI", Font.BOLD, 12));
// Fixed border creation: Create a compound border with line border and empty border
Border lineBorder = BorderFactory.createLineBorder(new Color(100, 100, 100));
Border paddingBorder = new EmptyBorder(8, 15, 8, 15);
button.setBorder(BorderFactory.createCompoundBorder(lineBorder, paddingBorder));
return button;
}
private void updateLegend() {
legendPanel.removeAll();
for (PatternInfo info : patternInfos) {
JLabel label = new JLabel(info.description);
label.setOpaque(true);
label.setBackground(info.color);
label.setForeground(Color.BLACK);
label.setFont(new Font("Segoe UI", Font.BOLD, 11));
// Create a compound border for legend items
Border lineBorder = BorderFactory.createLineBorder(Color.DARK_GRAY);
Border paddingBorder = new EmptyBorder(3, 8, 3, 8);
label.setBorder(BorderFactory.createCompoundBorder(lineBorder, paddingBorder));
legendPanel.add(label);
}
legendPanel.revalidate();
legendPanel.repaint();
}
private void browseLogFile(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("Text Files", "txt"));
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
currentFile = fileChooser.getSelectedFile();
loadLogFile();
}
}
private void loadLogFile() {
if (currentFile == null) return;
try (BufferedReader reader = new BufferedReader(new FileReader(currentFile))) {
// Clear previous content
logTextPane.setText("");
resultTextPane.setText("");
// Create a document for styled text
StyledDocument doc = logTextPane.getStyledDocument();
StyleContext sc = StyleContext.getDefaultStyleContext();
Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
String line;
while ((line = reader.readLine()) != null) {
boolean matched = false;
// Check each pattern
for (PatternInfo info : patternInfos) {
Matcher m = info.pattern.matcher(line);
if (m.find()) {
// Apply style for this pattern
Style style = sc.addStyle(info.description, defaultStyle);
StyleConstants.setForeground(style, info.color);
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), line + "\n", style);
matched = true;
break;
}
}
// If no pattern matched, use default style
if (!matched) {
doc.insertString(doc.getLength(), line + "\n", defaultStyle);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error loading file: " + ex.getMessage(),
"File Error", JOptionPane.ERROR_MESSAGE);
}
}
private void analyzeLog(ActionEvent e) {
if (currentFile == null) {
JOptionPane.showMessageDialog(this, "No log file loaded!",
"Analysis Error", JOptionPane.WARNING_MESSAGE);
return;
}
// Clear previous results
resultTextPane.setText("");
List<LogEntry> suspiciousEntries = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(currentFile))) {
String line;
int lineNum = 1;
while ((line = reader.readLine()) != null) {
for (PatternInfo info : patternInfos) {
Matcher m = info.pattern.matcher(line);
if (m.find()) {
suspiciousEntries.add(new LogEntry(lineNum, line, info));
break; // Avoid duplicate matches
}
}
lineNum++;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error analyzing log: " + ex.getMessage(),
"Analysis Error", JOptionPane.ERROR_MESSAGE);
}
// Display results with color coding
if (suspiciousEntries.isEmpty()) {
appendToResult("No suspicious activity found!", null);
} else {
appendToResult("Found " + suspiciousEntries.size() + " suspicious entries:\n\n", null);
for (LogEntry entry : suspiciousEntries) {
appendToResult(String.format("[Line %d] ", entry.lineNum), null);
appendToResult(entry.content + "\n", entry.patternInfo);
}
}
}
private void appendToResult(String text, PatternInfo info) {
try {
StyledDocument doc = resultTextPane.getStyledDocument();
StyleContext sc = StyleContext.getDefaultStyleContext();
if (info != null) {
// Style for matched pattern
Style style = sc.addStyle(info.description, null);
StyleConstants.setForeground(style, info.color);
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), text, style);
} else {
// Default style
Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setForeground(defaultStyle, Color.LIGHT_GRAY);
doc.insertString(doc.getLength(), text, defaultStyle);
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
// Set system look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
LogAnalyzerApp app = new LogAnalyzerApp();
app.setVisible(true);
});
}
// Helper classes
static class PatternInfo {
String description;
Pattern pattern;
Color color;
PatternInfo(String description, Pattern pattern, Color color) {
this.description = description;
this.pattern = pattern;
this.color = color;
}
}
static class LogEntry {
int lineNum;
String content;
PatternInfo patternInfo;
LogEntry(int lineNum, String content, PatternInfo patternInfo) {
this.lineNum = lineNum;
this.content = content;
this.patternInfo = patternInfo;
}
}
}