| name | Code Quality Metrics |
| description | Analyze code complexity, maintainability, and security issues |
| tags | ["quality","complexity","security","metrics","technical-debt"] |
| allowed_tools | ["analyze_code_metrics","get_function_security_score","decompile_function","list_imports"] |
Task: Code Quality Analysis. Evaluate binary code for complexity, maintainability, and security.
Metrics Analyzed
Complexity Metrics
- Cyclomatic Complexity: Decision points in function
- Instruction Count: Total instructions per function
- Basic Block Count: Control flow graph nodes
- Nesting Depth: Maximum nesting level
Complexity Ratings
- Very High: 50+ (needs refactoring)
- High: 20-49 (consider simplification)
- Medium: 10-19 (acceptable)
- Low: 5-9 (good)
- Simple: 1-4 (ideal)
Code Smells Detected
- God Function: >500 instructions (does too much)
- Long Parameter List: >8 parameters
- Deep Nesting: >4 nesting levels
- Magic Numbers: Hardcoded numeric values
- Large Switch: >10 case statements
Security Issues Detected
- Hardcoded Credentials: passwords, API keys, secrets
- Weak Crypto: MD5, SHA1, DES, RC4
- Unsafe String Ops: strcpy, strcat, sprintf, gets
- Random Without Seed: rand() without srand()
- Null Pointer Dereference: Potential crashes
Security Score Calculation
Grade Scale:
- A (90-100): Excellent, no critical issues
- B (80-89): Good, minor issues
- C (70-79): Fair, some concerns
- D (60-69): Poor, significant issues
- F (0-59): Critical issues present
Point Deductions:
- Critical: -20 points each
- High: -10 points each
- Medium: -5 points each
- Low: -1 point each
Analysis Workflow
-
Full Binary Analysis
Use analyze_code_metrics() for comprehensive report
- All functions analyzed
- Aggregate statistics
- Security score with grade
- Most complex/largest functions
-
Function-Level Analysis
Use get_function_security_score(address) for specific function
- Detailed complexity breakdown
- Security issues found
- Code smells detected
- Specific recommendations
-
Issue Remediation
Priority Order:
1. Critical severity (hardcoded credentials, unsafe ops)
2. High severity (weak crypto, null derefs)
3. Medium severity (random issues)
4. Code smells (complexity, size)
Recommendations
High Complexity Functions
- Break into smaller, focused functions
- Reduce nesting levels
- Extract common patterns
- Improve testability
Large Functions
- Decompose into logical units
- Extract helper functions
- Consider strategy pattern
Security Issues
- Replace unsafe string operations
- Remove hardcoded credentials
- Update weak cryptographic algorithms
- Add input validation
Report Format
[Code Quality Report]
Binary: target.exe
Date: 2024-01-15
[Summary]
Functions: 1250
Average Complexity: 8.5
High Complexity Functions: 45 (3.6%)
Large Functions: 18 (1.4%)
[Security Score]
Grade: C (72/100)
Critical: 2 issues
High: 8 issues
Medium: 15 issues
[Most Complex Functions]
1. ProcessRequest (Complexity: 87, Size: 1245 instr)
- Issues: god_function, magic_numbers
- Recommends: Split into 5-6 functions
2. ParseInput (Complexity: 76, Size: 892 instr)
- Issues: deep_nesting, unsafe_string_ops
- Recommends: Reduce nesting, use safe functions
[Security Issues]
CRITICAL: Hardcoded credentials
- Location: 0x140001200 (verify_password)
- Pattern: password = "admin123"
HIGH: Unsafe string operations
- Location: 0x140001500 (copy_data)
- Functions: strcpy, sprintf