packageorg.owasp.mastestappimportandroid.content.Contextimportjava.util.Randomimportjava.lang.*importjava.security.SecureRandomclassMastgTest(privatevalcontext:Context){funmastgTest():String{// FAIL: [android-insecure-random-use] The app insecurely uses random numbers for generating authentication tokens.valrandom1=Random().nextDouble()// FAIL: [android-insecure-random-use] The title of the function indicates that it generates a random number, but it is unclear how it is actually used in the rest of the app. Review any calls to this function to ensure that the random number is not used in a security-relevant context.valrandom2=1+Math.random()vallength=16valcharacters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"valrandom=Random()valpassword=StringBuilder(length)for(iin0untillength){// FAIL: [android-insecure-random-use] The app insecurely uses random numbers for generating passwords, which is a security-relevant context.password.append(characters[random.nextInt(characters.length)])}valrandom3=password.toString()// PASS: [android-insecure-random-use] The app uses a secure random number generator.valrandom4=SecureRandom().nextInt(21)return"Generated random numbers:\n$random1 \n$random2 \n$random3 \n$random4"}}
rules:-id:mastg-android-insecure-random-useseverity:WARNINGlanguages:-javametadata:summary:Thisrulelooksforcommonpatternsincludingclassesandmethods.original_source:https://github.com/mindedsecurity/semgrep-rules-android-security/blob/main/rules/crypto/mstg-crypto-6.yamlmessage:"[MASVS-CRYPTO-1] The application makes use of an insecure random number generator."pattern-either:-patterns:-pattern-inside:$M(...){...}-pattern-either:-pattern:Math.random(...)-pattern:(java.util.Random$X).$Y(...)
The rule has identified five instances in the code file where an insecure random number generator is used. The specified line numbers can be located in the original code for further investigation and remediation.
Line 12 seems to be used to generate random numbers for security purposes, in this case for generating authentication tokens.
Line 17 is part of the function get_random. Review any calls to this function to ensure that the random number is not used in a security-relevant context.
Line 27 is part of the password generation function which is a security-critical operation.
Note that line 37 did not trigger the rule because the random number is generated using SecureRandom which is a secure random number generator.