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

What's hot (20)

PDF
Neil Saunders (Beamly) - Securing your AWS Infrastructure with Hashicorp Vault
Outlyer
 
PPT
Source Code Analysis with SAST
Blueinfy Solutions
 
PDF
Secret Management with Hashicorp’s Vault
AWS Germany
 
PDF
Neat tricks to bypass CSRF-protection
Mikhail Egorov
 
PPTX
Docker Security Overview
Sreenivas Makam
 
PDF
[OpenStack] 공개 소프트웨어 오픈스택 입문 & 파헤치기
Ian Choi
 
PPT
Container security
Anthony Chow
 
PDF
Zephyr: Creating a Best-of-Breed, Secure RTOS for IoT
LinuxCon ContainerCon CloudOpen China
 
PDF
Container Runtime Security with Falco
Michael Ducy
 
DOCX
Requirement for creating a Penetration Testing Lab
Syed Ubaid Ali Jafri
 
PDF
Build a High Available NFS Cluster Based on CephFS - Shangzhong Zhu
Ceph Community
 
PDF
A5-Security misconfiguration-OWASP 2013
Sorina Chirilă
 
PDF
Hệ thống quản lý và phân tích log tập trung elk stack
laonap166
 
PDF
Polyglot payloads in practice by avlidienbrunn at HackPra
Mathias Karlsson
 
PDF
Docker Registry V2
Docker, Inc.
 
PPTX
Keeping a Secret with HashiCorp Vault
Mitchell Pronschinske
 
PDF
OWASP Top 10 Web Application Vulnerabilities
Software Guru
 
PPTX
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
CODE BLUE
 
PDF
DevSecOps and the CI/CD Pipeline
James Wickett
 
PDF
What is Docker Architecture | Edureka
Edureka!
 
Neil Saunders (Beamly) - Securing your AWS Infrastructure with Hashicorp Vault
Outlyer
 
Source Code Analysis with SAST
Blueinfy Solutions
 
Secret Management with Hashicorp’s Vault
AWS Germany
 
Neat tricks to bypass CSRF-protection
Mikhail Egorov
 
Docker Security Overview
Sreenivas Makam
 
[OpenStack] 공개 소프트웨어 오픈스택 입문 & 파헤치기
Ian Choi
 
Container security
Anthony Chow
 
Zephyr: Creating a Best-of-Breed, Secure RTOS for IoT
LinuxCon ContainerCon CloudOpen China
 
Container Runtime Security with Falco
Michael Ducy
 
Requirement for creating a Penetration Testing Lab
Syed Ubaid Ali Jafri
 
Build a High Available NFS Cluster Based on CephFS - Shangzhong Zhu
Ceph Community
 
A5-Security misconfiguration-OWASP 2013
Sorina Chirilă
 
Hệ thống quản lý và phân tích log tập trung elk stack
laonap166
 
Polyglot payloads in practice by avlidienbrunn at HackPra
Mathias Karlsson
 
Docker Registry V2
Docker, Inc.
 
Keeping a Secret with HashiCorp Vault
Mitchell Pronschinske
 
OWASP Top 10 Web Application Vulnerabilities
Software Guru
 
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
CODE BLUE
 
DevSecOps and the CI/CD Pipeline
James Wickett
 
What is Docker Architecture | Edureka
Edureka!
 

Similar to Lightweight static code analysis with semgrep (20)

PPTX
Developer-friendly SAST in DevOps Pipeline.pptx
SamSbp
 
PPTX
200 Days of Code, Beginner Track, Month 5
Ryne McCall
 
PDF
Modern PHP
Simon Jones
 
PPTX
Listen afup 2010
Gabriele Santini
 
PPTX
Syn504 unleashing the power of the net scaler policy and expressions engine...
Henrik Johansson
 
PDF
Sec4dev 2021 - Catch Me If You can : Continuous Delivery vs. Security Assurance
Abdessamad TEMMAR
 
PDF
Preparing for the next PHP version (5.6)
Damien Seguy
 
PPTX
Php internal architecture
Elizabeth Smith
 
PPTX
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
PDF
New PHP Exploitation Techniques
RIPS Technologies GmbH
 
PPTX
Improving your team’s source code searching capabilities
Nikos Katirtzis
 
PPTX
Improving your team's source code searching capabilities - Voxxed Thessalonik...
Nikos Katirtzis
 
PPTX
Releasing High Quality Packages - Longhorn PHP 2021
Colin O'Dell
 
PPTX
Regular Expressions in PHP
Andrew Kandels
 
PDF
When RegEx is not enough
Nati Cohen
 
PPTX
Finaal application on regular expression
Gagan019
 
PDF
Basta mastering regex power
Max Kleiner
 
ODP
What's new, what's hot in PHP 5.3
Jeremy Coates
 
ODP
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
PDF
Silex and Twig (PHP Dorset talk)
Dave Hulbert
 
Developer-friendly SAST in DevOps Pipeline.pptx
SamSbp
 
200 Days of Code, Beginner Track, Month 5
Ryne McCall
 
Modern PHP
Simon Jones
 
Listen afup 2010
Gabriele Santini
 
Syn504 unleashing the power of the net scaler policy and expressions engine...
Henrik Johansson
 
Sec4dev 2021 - Catch Me If You can : Continuous Delivery vs. Security Assurance
Abdessamad TEMMAR
 
Preparing for the next PHP version (5.6)
Damien Seguy
 
Php internal architecture
Elizabeth Smith
 
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
New PHP Exploitation Techniques
RIPS Technologies GmbH
 
Improving your team’s source code searching capabilities
Nikos Katirtzis
 
Improving your team's source code searching capabilities - Voxxed Thessalonik...
Nikos Katirtzis
 
Releasing High Quality Packages - Longhorn PHP 2021
Colin O'Dell
 
Regular Expressions in PHP
Andrew Kandels
 
When RegEx is not enough
Nati Cohen
 
Finaal application on regular expression
Gagan019
 
Basta mastering regex power
Max Kleiner
 
What's new, what's hot in PHP 5.3
Jeremy Coates
 
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
Silex and Twig (PHP Dorset talk)
Dave Hulbert
 
Ad

More from Null Bhubaneswar (16)

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

Recently uploaded (20)

PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Learn Computer Forensics, Second Edition
AnuraShantha7
 
PPTX
Q2 Leading a Tableau User Group - Onboarding
lward7
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Learn Computer Forensics, Second Edition
AnuraShantha7
 
Q2 Leading a Tableau User Group - Onboarding
lward7
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 

Lightweight static code analysis with semgrep