Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add targetSdk in rules #56

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}
}
}
Loading