Skip to content

Commit

Permalink
Merge pull request #56 from firmianay/dev3
Browse files Browse the repository at this point in the history
add targetSdk in rules
  • Loading branch information
nkbai authored Nov 3, 2023
2 parents e63250c + ad7c336 commit ee56540
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ import kotlin.io.path.pathString
import kotlin.streams.toList

class AnalyzeStepByStep {
suspend fun loadRules(
ruleList: String,
): Rules {
suspend fun loadRules(ruleList: String, targetSdk: Int): Rules {
val rulePathList = if (ruleList.isNotEmpty())
ruleList.split(",").map { "${getConfig().rulePath}/$it" }.toList()
ruleList.split(",").map { "${getConfig().rulePath}/${it.trim()}" }.toList()
else
withContext(Dispatchers.IO) {
Files.walk(Paths.get(getConfig().rulePath), 1)
}.filter { it.pathString.endsWith(".json") }.map { it.pathString }
.toList()
val rules = Rules(rulePathList, RuleFactory())
rules.loadRules()
rules.loadRules(targetSdk)
return rules
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/net/bytedance/security/app/RuleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ data class RuleData(

val ConstNumberMode: Boolean? = null,
val targetNumberArr: List<Int>? = null,

val targetSdk: String = "",
)

val defaultSourceReturn = SourceReturn()

@Serializable
data class SourceReturn(

val EntryInvoke: Boolean = false, val LibraryOnly: Boolean? = false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object StaticAnalyzeMain {
profiler.parseApk.end()

profiler.preProcessor.start()
val rules = v3.loadRules(argumentConfig.rules)
val rules = v3.loadRules(argumentConfig.rules, AndroidUtils.TargetSdk)
logInfo("rules loaded")
val ctx = v3.createContext(rules)
profiler.preProcessor.end()
Expand Down
45 changes: 41 additions & 4 deletions src/main/kotlin/net/bytedance/security/app/rules/Rules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import java.nio.file.Paths
class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForContext {
val allRules: MutableList<IRule> = ArrayList()

suspend fun loadRules() {
suspend fun loadRules(targetSdk: Int? = null) {
rulePaths.forEach {
val jsonStr = loadConfigOrQuit(it)
val rules = Json.parseToJsonElement(jsonStr)
Expand All @@ -40,8 +40,12 @@ class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForC
ruleData.sanitize = ruleData.sanitizer
ruleData.sanitizer = null
}
val rule = factory.create(ruleName, ruleData)
allRules.add(rule)
if (targetSdk == null || targetSdk in parseSdkVersion(ruleData.targetSdk)) {
val rule = factory.create(ruleName, ruleData)
allRules.add(rule)
} else {
Log.logDebug("ignore rule: $ruleName")
}
}
}
}
Expand Down Expand Up @@ -90,4 +94,37 @@ class Rules(val rulePaths: List<String>, val factory: IRuleFactory) : IRulesForC
return jsonStr
}
}
}

fun parseSdkVersion(input: String): List<Int> {
val MIN_SDK_VERSION = 9 // Android 2.3
val MAX_SDK_VERSION = 50 // for future

if (input.isBlank() || input.trim() == ":") {
return (MIN_SDK_VERSION..MAX_SDK_VERSION).toList()
}
return input.split(Regex("[,\\s]+")).flatMap { part ->
when {
part.contains(":") -> {
val splitPart = part.split(":")
val hasStart = splitPart[0].isNotEmpty()
val hasEnd = splitPart[1].isNotEmpty()
when {
!hasStart && !hasEnd -> listOf()
!hasEnd -> {
(splitPart[0].toIntOrNull() ?: return@flatMap listOf())..MAX_SDK_VERSION
}
!hasStart -> {
(MIN_SDK_VERSION..(splitPart[1].toIntOrNull() ?: return@flatMap listOf())).toList()
}
else -> {
val start = splitPart[0].toIntOrNull() ?: return@flatMap listOf()
val end = splitPart[1].toIntOrNull() ?: return@flatMap listOf()
(start..end).toList()
}
}
}
else -> listOf(part.toIntOrNull() ?: return@flatMap listOf())
}
}
}
}

0 comments on commit ee56540

Please sign in to comment.