joshua_jebaraj
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: "How to find Bugs in Android application Using Semgrep" date: 2019-09-03 pubtype: "Blog" featured: true description: "In this Blog I will tell explain you how to find the Security Flaws in Android Application " tags: ["Semgrep","Mobile","Automation"] weight: 400 sitemap: priority : 0.8 --- Hello, Friends Hope you are all staying safe and Happy. Its been a long time I wrote the blog Finally I am able to kick my lazy ass and sit down to write the blog So I am a big fan of Android Security In my free time I used to read and do Some research around Android Security Even my first Blog is about [Android-Security](https://www.joshuajebaraj.com/publications/how-do-i-automate-the-enviroment-setup-for-android-pentesting-using-simple-bash-scripts/) But I want to automate the Security Scanner that able to pick up the most common Security Flaws So I ended up having two choices - Use the Pre-made Tool like [Mobsf](https://github.com/MobSF/Mobile-Security-Framework-MobSF) which is a great tool which I suggest must checkout - Create your own tool So I ended Selecting the Second option So the biggest problem is writing a custom Scanner for Black Box Testing in Android is You will not get the same source code as written due to obfuscation Let's see the example What I meant So you wrote the pattern to match the code ``` java WebView.setWebContentsDebuggingEnabled(true); ``` You write the rule in any other existing tool or use the grep to find the above code it will able to find the code but the problem is due to obfuscation the code will not be the same as above this could be changed like ``` java ew.setWebContentsDebuggingEnabled(true); ``` Now When we try to run the tool again or grep again the tool couldn't able to find the code due to obfuscation ![m1](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/meme.jpg) Here Comes the [Semgrep](https://github.com/returntocorp/semgrep) into play Before gets into the detail lets see a quick glance about the `Semgrep` According to the Official Docs ``` Lightweight static analysis for many languages. Find and block bug variants with rules that look like source code. ``` Here are the few ways that Semgrep more has an advantage over another too - Easy to Install All you need is `pip3` installed in your System You can Install Semgrep by typing the below command ``` bash pip3 install semgrep ``` - Supports Multiple Language like Python, JavaScript, Java,Go - Can be easily Integrate into CI/CD pipeline - Has Registry with Predefined Rules So Without Getting the time wasted let's get started For the hands-on part, I am going to use the [Semgrep-Live-editor](https://semgrep.dev/editor) which allow me to write the rules on the fly You can either test your rules here like I do or you could test locally with the **semgrep-cli** tool that Installed using pip Let's try to understand the various components in creating the rules Below is the rules look like in the semgrep This rule looks for the `System.out.println("Hello")` in the hello world-program ``` yaml rules: - id: Hello-world metadata: author: Joshua message: | System.out.println Found patterns: - pattern: System.out.println("Hello"); severity: WARNING languages: - java ``` lets try to understand the above yaml - id - Here we specify the id of the rule - metadata - Here we can specify the additionalInformation - message - Here we specify the message to be shown on pattern match - pattern - Here we specify the pattern to be matched - severity - Here we specify the severity - languages - Here we specify the language Copy and paste the above YAML in the `semgrep-rule` field and put the hello-world code in the `Test-code` Field and click **RUN** ![he](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/hell.png) Ya we have successfully wrote our first rule So let's get started To give a quick heads up this blog is more focused on the tool rather than the vulnerabilities So I will not be covering explaining the vulnerability The First vulnerability that we are going to find is Debug mode was enabled in the web view of the Android application This is What the Code look like in the plain java for the above vulnerability ``` java WebView.setWebContentsDebuggingEnabled(true); ``` The problem here is on reversing the android application the the `Webview` can become obfuscated Let's see how we can solve with semgrep We are going to use one of the feature of Semgrep called **Metavarialble** . Metavariable is the variable which is replaced by any variable in the runtime Below is the semgrep pattern that we are going to use ``` $RUNTIME.setWebContentsDebuggingEnabled(true); ``` ![img](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/image-1.png) You could See the Semgrep could able to find the matching pattern Lets try to replace the Webview with another value ![img-2](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/iamge-2.png) You could See Its successfully able to find out that right Lets See What are the other thing that Semgrep Got us to offer The next vulnerability that we are going to cover was Insecure Logging This is What the Code look like in the plain java ``` java Log.e("Hello") ``` ``` java Log.e("Tag","Hello From Tag") ``` The Problem with this code is the Tag parameter is optional so there may be code with or without tag This is where ellipsis operator comes into play ellipsis operator allow us to match 0 or more argument Below is the semgrep pattern that we are going to use ``` yaml Log.e(...) ``` Lets try to match the code without tag ![log1](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/log1.png) You could see its successfully able to find the pattern Lets try with log function with tag flag ![log2](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/log2.png) By default, Semgrep uses **and** operator but in a certain situation, we want to perform **or** operation that matches either one of the pattern To achieve that semgrep provide `pattern-either` option where the semgrep will perform or operation let's take the example you want to find the hardcoded-secret the variable name could be pass or password The Semgrep should match any one of the variables > Note to follow the further exercises you have to switch to the `Advanced` tab in the website ![pat-1](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/pattern-1.png) It could able to find the pattern Lets try to replace the variable name with pass ![pat-2](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/pattern-2.png) Sometimes you want to filter out the patterns that you are not looking in that situation you can use **pattern-not** Let's take the first example Instead of matching all Log message you want to match the log message which is not info log (Note: we are assuming here the Log.info doesn't have any sensitive info In real-world It may vary) ``` java Log.e("Hello"); // this should be matched Log.i("Hello Info); // this shouldn't be matched ``` Let's try to create the pattern with less **pattern-not** flag ``` yaml rules: - id: Log Message patterns: - pattern: | Log.$RUNTIME(...); - pattern-not: | Log.i(...); message: | Semgrep found a match severity: WARNING ``` ![log-i](https://raw.githubusercontent.com/JOSHUAJEBARAJ/blog-images/master/semgrep/log-i.png) You could see its matching all other patterns than `Log.i` .This way you could write the rules to find the bugs in the Android application There are many features that Semgrep offers In my blog I cover few features If you are interested more I high Recommend you to check out the resources in the reference section and I highly recommend you to join the Official Slack Channel [here](http://r2c.dev/slack) If you have any questions or feedback feel free to reach out to me on [Twitter](https://twitter.com/joshva_jebaraj?lang=en) Thanks for reading Have a nice day ## References - [Semgrep-Docs](https://semgrep.dev/) - [Semgrep A Practical Introduction](https://notsosecure.com/semgrep-a-practical-introduction/) - [Semgrep presentation by r2c at Bay Area OWASP Meetup](https://www.youtube.com/watch?v=pul1bRIOYc8) - [Mobsf-rules](https://github.com/MobSF/Mobile-Security-Framework-MobSF/blob/master/StaticAnalyzer/views/android/rules/android_rules.yaml) - [Semgrep-Learning](https://semgrep.dev/learn)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully