Skip to content

Commit a31f02e

Browse files
committed
complete network analyzer auto-blacklist logic and DAO queries
1 parent 05a393c commit a31f02e

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

app/src/main/java/com/droid/cybershield/data/local/dao/NetworkTrafficDao.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ interface NetworkTrafficDao {
3838
@Query("SELECT COUNT(*) FROM network_traffic WHERE blocked = 1")
3939
fun getBlockedCount(): Flow<Long>
4040

41+
/**
42+
* Count how many MALICIOUS hits exist for a specific domain since [sinceMs].
43+
* Used by the auto-blacklist rule: 3+ hits in 24 h → permanent block.
44+
*/
45+
@Query("""
46+
SELECT COUNT(*) FROM network_traffic
47+
WHERE (destination_domain = :domain OR destination_ip = :domain)
48+
AND threat_level = 'MALICIOUS'
49+
AND timestamp >= :sinceMs
50+
""")
51+
suspend fun countMaliciousForDomainSince(domain: String, sinceMs: Long): Int
52+
4153
/** Delete traffic logs older than [cutoffMs] (7-day retention). */
4254
@Query("DELETE FROM network_traffic WHERE timestamp < :cutoffMs")
4355
suspend fun deleteOlderThan(cutoffMs: Long)

app/src/main/java/com/droid/cybershield/data/local/dao/NetworkWhitelistDao.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ interface NetworkBlacklistDao {
3939
""")
4040
suspend fun deleteExpired(cutoffMs: Long)
4141

42+
/** Returns the blacklist entry for [domain], or null if not blacklisted. */
43+
@Query("SELECT * FROM network_blacklist WHERE domain = :domain LIMIT 1")
44+
suspend fun getByDomain(domain: String): NetworkBlacklistEntity?
45+
4246
@Query("SELECT COUNT(*) FROM network_blacklist")
4347
suspend fun getCount(): Long
4448
}

app/src/main/java/com/droid/cybershield/data/repository/NetworkAnalyzerRepositoryImpl.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ class NetworkAnalyzerRepositoryImpl @Inject constructor(
2727

2828
companion object {
2929
private const val TAG = "NetworkAnalyzerRepo"
30-
// 7-day and 30-day retention in milliseconds
30+
// Retention windows
3131
private const val TRAFFIC_RETENTION_MS = 7L * 24 * 60 * 60 * 1000
3232
private const val BLACKLIST_RETENTION_MS = 30L * 24 * 60 * 60 * 1000
33+
// Auto-blacklist rule: MALICIOUS_THRESHOLD hits within WINDOW_MS → permanent block
34+
private const val AUTO_BLACKLIST_THRESHOLD = 3
35+
private const val AUTO_BLACKLIST_WINDOW_MS = 24L * 60 * 60 * 1000 // 24 hours
3336
}
3437

3538
// ── Traffic Logging ────────────────────────────────────────────────────────
@@ -49,6 +52,36 @@ class NetworkAnalyzerRepositoryImpl @Inject constructor(
4952
blocked = result.shouldBlock
5053
)
5154
)
55+
56+
// Auto-blacklist: if this domain has been MALICIOUS 3+ times in 24h, block permanently
57+
if (result.threatLevel == ThreatLevel.MALICIOUS) {
58+
val target = packet.destinationDomain ?: packet.destinationIp
59+
checkAutoBlacklist(target)
60+
}
61+
}
62+
63+
/**
64+
* Checks if [domain] has been flagged MALICIOUS [AUTO_BLACKLIST_THRESHOLD]+ times
65+
* within the last [AUTO_BLACKLIST_WINDOW_MS]. If so, permanently blacklists it.
66+
* This enforces the plan's "3× MALICIOUS in 24h → auto-blacklist" rule.
67+
*/
68+
private suspend fun checkAutoBlacklist(domain: String) {
69+
val since = System.currentTimeMillis() - AUTO_BLACKLIST_WINDOW_MS
70+
val hitCount = trafficDao.countMaliciousForDomainSince(domain, since)
71+
72+
if (hitCount >= AUTO_BLACKLIST_THRESHOLD) {
73+
val alreadyBlocked = blacklistDao.getByDomain(domain) != null
74+
if (!alreadyBlocked) {
75+
blacklistDao.insert(
76+
NetworkBlacklistEntity(
77+
domain = domain,
78+
reason = "Auto-blacklisted: flagged MALICIOUS $hitCount times in 24h",
79+
blockPermanently = true
80+
)
81+
)
82+
Log.w(TAG, "🚫 AUTO-BLACKLISTED: $domain ($hitCount MALICIOUS hits in 24h)")
83+
}
84+
}
5285
}
5386

5487
override fun getRecentTraffic(limit: Int): Flow<List<NetworkTrafficEntity>> =

0 commit comments

Comments
 (0)