SlideShare a Scribd company logo
LIGHTWEIGHT STATIC
CODE ANALYSIS WITH
SEMGREP
Nikhil Sahoo
WHOAMI
SECURITY SOFTWARE
ENGINEER
@MICROSOFT
EX- SECURITY
CONSULTANT,
DELOITTE
CERTS: OSCP,
EWPTX, CRTP
ACKNOWLEDGED
AND RECEIVED
HOFS FROM
MULTIPLE ORGS:
ORACLE, DELL,
MICROSOFT, SAP,
SONY ETC.
LINKEDIN:
HTTPS://WWW.LINKE
DIN.COM/IN/NIKHIL-
SAHOO-87204B106/
NIKHILSAHOO.COM
AGENDA
• History
• What is Semgrep?
• SAST Tools Slider
• Rule Syntax
• Writing Rules | Demo
• Usage
• Limitations
• QnA
HISTORY: SGREP (SYNTACTIC GREP)
• Initially called Sgrep/Pfff
• Written By Yoann Padioleau at Facebook for analyzing PHP code
• Was used to Enforce Best Practices
• Easy for developers to organize and understand the rules
• Joined R2C and renamed Sgrep to Semgrep
• Goal was to match based on semantics of the code
Reference: https://semgrep.dev/blog/2021/semgrep-a-static-analysis-journey
WHAT IS SEMGREP?
• Fast and lightweight static analysis tool to find bugs and enforce code
standards.
Reference: https://semgrep.dev/
PROS
• Opensource
• Rules look like the code you already write; no abstract syntax trees, regex
wrestling
• Scans in minutes
• Does not require a build
• Hunt Bugs at scale
• Supports many languages
• Very easy to write custom rules
SAST SLIDER
https://instagram-engineering.com/static-analysis-at-scale-an-instagram-story-8f498ab71a0c
Devskim
Semgrep
CodeQl
REGEX PROBLEMS
https://www.explainxkcd.com/wiki/images/1/10/perl_problems.png
False
Positives
REGEX PROBLEMS
CODE IS NOT A STRING, IT’S A TREE
🧶 String != 🌲Tree
INTERNALS
https://r2c.dev/static/00125f77fba64f5350b367c373c4e849/1132d/semgrep-flow.png
RULE SYNTAX
https://semgrep.dev/docs/writing-rules/rule-syntax/
DEMO WRITING SEMGREP RULES
HTTPS://SEMGREP.DEV/PLAYGROUND/NEW
ELLIPSIS
• Ellipsis Operator: “…”
• Find all PHP Command Injection Functions
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-php-
command-inj
• Reference: https://www.stackhawk.com/blog/php-command-injection/
METAVARIABLE
• Metavariable Operator: “$X”
• Find all user exploitable PHP Preg_Replace Functions
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-
metavariable-pregreplace
• Reference:
https://bitquark.co.uk/blog/2013/07/23/the_unexpected_dangers_of_preg
_replace
CATCHING ATTRIBUTES
• Scenario 1: Find all functions vulnerable to CSRF
• Solution: https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-
csrf-mvc
• Scenario 2: Broken Access Control
• Solution: https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-
missing-or-broken-authorization
• Reference: https://www.stackhawk.com/blog/net-csrf-protection-guide-
examples-and-how-to-enable/, https://github.com/returntocorp/semgrep-
rules/blob/develop/csharp/dotnet/security/audit/missing-or-broken-
authorization.cs
DEEP EXPRESSION
• Deep Expression Operator: “<… [your pattern]…>”
• Scenario: XML Parser allows resolving external resources
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-
xmltextreader
• Reference: https://rules.sonarsource.com/csharp/RSPEC-2755,
https://pvs-studio.com/en/blog/posts/csharp/0918/
METAVARIABLE REGEX
• Scenario: App not validating the expiration time of a JWT
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-jwt-no-
expiry
• Reference: https://github.com/returntocorp/semgrep-
rules/blob/develop/csharp/lang/security/ad/jwt-
tokenvalidationparameters-no-expiry-validation.cs
METAVARIABLE COMPARISON
• Scenario: Improper Regex Timeout
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-regex-
dos-infinite-timeout
• Reference: https://github.com/returntocorp/semgrep-
rules/blob/develop/csharp/lang/security/regular-expression-dos/regular-
expression-dos-infinite-timeout.cs,
https://rules.sonarsource.com/csharp/RSPEC-6444
TAINT ANALYSIS
• Scenario: PHP Reflected XSS
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-php-xss-
taint
• Reference: https://rules.sonarsource.com/php/RSPEC-5131
PATTERN-SANITIZERS
• Scenario: Path.Combine() Path Traversal
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-path-
combine
• Reference: https://www.praetorian.com/blog/pathcombine-security-
issues-in-aspnet-applications/
GENERIC PATTERN MATCHING
• Scenario: Debugging Enabled(Web.Config)
• Solution:
https://semgrep.dev/orgs/nikhil1232/editor/s/nikhil1232:owasp-net-
webconfig-debug
• Reference: https://portswigger.net/kb/issues/00100800_asp-net-
debugging-enabled
LIMITATION
Multi File Analysis:
DeepSemgrep
USAGE
Semgrep in Editors
• IntelliJ IDEA: semgrep-idea-plugin
• Microsoft Visual Studio Code: semgrep-vscode
• Vim: semgrep.vim
CLI
Docker
SEMGREP ON PUSH
https://semgrep.dev/docs/semgrep-ci/overview/
TOOLS
• Semgrep: https://github.com/returntocorp/semgrep
• Semgrep External Rules: https://github.com/returntocorp/semgrep-rules
• Semgrep Playground: https://semgrep.dev/playground
• CodeQL: https://codeql.github.com/
• Devskim: https://github.com/microsoft/DevSkim
REFERENCES
• https://semgrep.dev/docs/
• https://www.youtube.com/watch?v=kb8oo7Wyk84
• https://youtube.com/watch?v=O5mh8j7-An8
• https://semgrep.dev/playground/
• https://rules.sonarsource.com/
• https://semgrep.dev/blog/2021/semgrep-a-static-analysis-journey
• https://github.com/returntocorp/semgrep-rules
THANK YOU

More Related Content

PDF
Web Application Security and Awareness
PPTX
Deep understanding on Cross-Site Scripting and SQL Injection
PPTX
Sql injections - with example
PDF
SSRF workshop
PPTX
Ppt on sql injection
PDF
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
PPTX
Xss attack
PDF
BugBounty Roadmap with Mohammed Adam
Web Application Security and Awareness
Deep understanding on Cross-Site Scripting and SQL Injection
Sql injections - with example
SSRF workshop
Ppt on sql injection
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
Xss attack
BugBounty Roadmap with Mohammed Adam

What's hot (20)

PPT
Top 10 Web Security Vulnerabilities (OWASP Top 10)
PPT
Introduction to Web Application Penetration Testing
PDF
XSS Magic tricks
PDF
BugBounty Tips.pdf
PPT
Security Exploit of Business Logic Flaws, Business Logic Attacks
PDF
HTTP Security Headers
PPTX
Web application security
PPTX
SSRF For Bug Bounties
PPTX
Cross-Site Scripting (XSS)
PPT
Cross Site Request Forgery
PDF
Web Application Penetration Testing
PPTX
Secure Coding 101 - OWASP University of Ottawa Workshop
PDF
Sql injection with sqlmap
PPTX
Introduction XSS
PDF
Polyglot payloads in practice by avlidienbrunn at HackPra
PPT
SQL Injection
PPTX
Cyber kill chain
PPTX
Vulnerabilities in modern web applications
PDF
The Cross Site Scripting Guide
PPTX
Introduction to Dynamic Malware Analysis ...Or am I "Cuckoo for Malware?"
Top 10 Web Security Vulnerabilities (OWASP Top 10)
Introduction to Web Application Penetration Testing
XSS Magic tricks
BugBounty Tips.pdf
Security Exploit of Business Logic Flaws, Business Logic Attacks
HTTP Security Headers
Web application security
SSRF For Bug Bounties
Cross-Site Scripting (XSS)
Cross Site Request Forgery
Web Application Penetration Testing
Secure Coding 101 - OWASP University of Ottawa Workshop
Sql injection with sqlmap
Introduction XSS
Polyglot payloads in practice by avlidienbrunn at HackPra
SQL Injection
Cyber kill chain
Vulnerabilities in modern web applications
The Cross Site Scripting Guide
Introduction to Dynamic Malware Analysis ...Or am I "Cuckoo for Malware?"
Ad

Similar to Lightweight static code analysis with semgrep (20)

ODP
Drupal Efficiency - Coding, Deployment, Scaling
PPTX
Splunk: Forward me the REST of those shells
PDF
Introduction to Chef
PPTX
Building a REST API Microservice for the DevNet API Scavenger Hunt
PDF
BYOP: Custom Processor Development with Apache NiFi
PDF
[CB20] Vulnerabilities of Machine Learning Infrastructure by Sergey Gordeychik
PDF
Tool Up Your LAMP Stack
PDF
Tool up your lamp stack
PDF
Developing Brilliant and Powerful APIs in Ruby & Python
PDF
Coders Workshop: API First Mobile Development Featuring Angular and Node
PPTX
Introduction to Laravel Framework (5.2)
PDF
Analysis of-quality-of-pkgs-in-packagist-univ-20171024
PPTX
Aleksei Dremin - Application Security Pipeline - phdays9
ODP
Drupal Efficiency using open source technologies from Sun
PPTX
Microservices & Serverless Architecture Principles Applied - Cisco Live Orlan...
PDF
Apache Deep Learning 101 - ApacheCon Montreal 2018 v0.31
PDF
A crushing blow at the heart of SAP’s J2EE Engine.
PPTX
introduction to node.js
PPTX
OWASP ZAP Workshop for QA Testers
PDF
Headless approach for offloading heavy tasks in Magento
Drupal Efficiency - Coding, Deployment, Scaling
Splunk: Forward me the REST of those shells
Introduction to Chef
Building a REST API Microservice for the DevNet API Scavenger Hunt
BYOP: Custom Processor Development with Apache NiFi
[CB20] Vulnerabilities of Machine Learning Infrastructure by Sergey Gordeychik
Tool Up Your LAMP Stack
Tool up your lamp stack
Developing Brilliant and Powerful APIs in Ruby & Python
Coders Workshop: API First Mobile Development Featuring Angular and Node
Introduction to Laravel Framework (5.2)
Analysis of-quality-of-pkgs-in-packagist-univ-20171024
Aleksei Dremin - Application Security Pipeline - phdays9
Drupal Efficiency using open source technologies from Sun
Microservices & Serverless Architecture Principles Applied - Cisco Live Orlan...
Apache Deep Learning 101 - ApacheCon Montreal 2018 v0.31
A crushing blow at the heart of SAP’s J2EE Engine.
introduction to node.js
OWASP ZAP Workshop for QA Testers
Headless approach for offloading heavy tasks in Magento
Ad

More from Null Bhubaneswar (16)

PDF
PPTX
Online_financial_fraud3
PPTX
Web App Pen Test
PDF
BurpSuiteOverview
PPTX
PPTX
OWASP TOP 10 VULNERABILITIS
PPTX
Linux Basic Commands
PPTX
Intro to Reverse Engineering
PPTX
Saying Hello to Bug Bounty
PPTX
Information Security 201
PPTX
Online_financial_fraud Episode 2
PPTX
Information Security 101
PPTX
PPTX
Online Financial Fraud
PPTX
Introduction_to_Cloud
PPTX
how_to_get_into_infosec
Online_financial_fraud3
Web App Pen Test
BurpSuiteOverview
OWASP TOP 10 VULNERABILITIS
Linux Basic Commands
Intro to Reverse Engineering
Saying Hello to Bug Bounty
Information Security 201
Online_financial_fraud Episode 2
Information Security 101
Online Financial Fraud
Introduction_to_Cloud
how_to_get_into_infosec

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
A Presentation on Artificial Intelligence
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Encapsulation theory and applications.pdf
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Insight: SEC Turns Pro Crypto
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
A Presentation on Artificial Intelligence
Big Data Technologies - Introduction.pptx
NewMind AI Monthly Chronicles - July 2025
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Encapsulation theory and applications.pdf

Lightweight static code analysis with semgrep