D Phát hiện lỗ héng ma nguon sử dụng công cụ SonarQube: 1 Tổng quan về SonarQube: Sonarqube là một platform mã nguồn mở giúp chúng ta có thê kiểm tra chất lượng code của dự án, được viế
Trang 1
HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG
KHOA AN TOÀN THÔNG TIN
BÁO CÁO BÀI TẬP LỚN
CO SO AN TOAN THONG TIN
DE TAI NGHIEN CUU PHAT HIEN LO HONG MA NGUON Giảng viên hướng dẫn: Thầy Dé Xuân Chợ
'Tên các thành viên Mã sinh viên
Nguyễn Quang Duy B2IDCAT074
Trang 2Nội dung chính
Phát hiện lỗ héng ma nguồn sử dụng công cụ SonarCloud .«-.<- << <s<<<+ 3 Tổng quan về SonarCloud 2s x‡ 2121121111111 01121111 11.1211 ayeg 3
Cài đặt và cầu hình SonarCloud -s s s2 S521 55 1551555111155 1EE 8E reo 4
Phát hiện lỗ hỗng mã nguồn sử dung CodeQL 22
Tổng quan về CodeQ(LL . - + s2 1 EE1EE1EE121121111121111211 21 111g 22
Cài đặt CodeQL, trên VSCode Q2 1n S1 21H11 n1 H1 nàn ưệu 23
Cách viết truy vẫn trong CodeQLL, 55 SE E1 112112111121 11 011 rxe 27
Trang 3D Phát hiện lỗ héng ma nguon sử dụng công cụ SonarQube:
1) Tổng quan về SonarQube:
Sonarqube là một platform mã nguồn mở giúp chúng ta có thê kiểm tra chất lượng code của dự án, được viết bằng java nhưng nó hỗ trợ nhiều ngôn ngữ khác nhau: PHP, Ruby, Java (bao gồm ca Android), C#, JavaScript, TypeScript, C/C++, Kotlin, Go, COBOL, PL/SQL, PL/I, ABAP, VB.NET, VB6, Python, RPG, Flex, Objective-C, Swift, CSS, HTML, va XML va hé tro cac database đề lưu trữ kết qua: MySql, Postgresql
1.1) Core chính của SonarQube:
Metrics and Rules:
- Quality Profiles: Dinh nghia cac tap luat (set of rules) va cau hinh danh gia chất lượng cho từng ngôn ngữ
- _ Quality Gates: Đặt ngưỡng chất lượng mã nguồn và quyết định xem dự án có
đủ chất lượng đề triển khai hay không
Dashboard and Reporting:
- Dashboard: Hién thị tổng quan về chất lượng mã nguồn, cảnh báo, và thông kê
- Reports: Cung cap bao cao chi tiét về các vấn dé chất lượng và nợ kỹ thuật Integration and Plugins:
- _ Integration: Kết hợp với các công cụ quản lý mã nguồn và tích hợp với các quy trình CUCD
- _ Plugin System: Cho phép mở rộng khả năng của SonarQube bằng cách thêm cac plugin
User Interface:
Trang 4- Web Interface: Cung cap giao điện người dùng thân thiện đề quản lý và theo dõi chất lượng mã nguồn
e Security and Authentication:
- Authentication: H6 tro nhiều phương pháp xác thực người dùng
- _ Authorization: Quản lý quyền truy cập người dùng đối với các dự án và tính nang
® RESTful APIs:
- APIs: Cung cap cac API RESTful dé tuong tác với SonarQube từ các công cụ bên ngoài hoặc tích hợp trong các quy trỉnh tự động
® Notifications:
- _ Notifications: Gửi thông báo khi có vấn đề chất lượng mã nguồn
2) Cài đặt và cấu hình SonarCloud ( đây là một nền tảng đám mây sử dụng để
phân tích bảo mật mã nguồn tương tự SonarQube)
- Ta truy cập vao trang web hitps://www.sonarsource.com/products/sonarcloud/ -> chon
SIGN UP dé dang ki tai khoan (hoac chon LOG IN dé dang nhap - 6 day ta str dung GitHub đề đăng nhập)
sonarcloud & | Features WhafsNew Roadmap _— Documentation _—Pricing mm
clean code in your
cloud workflow with
{SonarCloud}
Enable your team to deliver clean code consistently and 0 2
efficiently with a cade review tool that easily integrates into the
1 © Code Smelis
cloud DevOps platforms and extend your CI/CD workflow
Trang 5Log in to SonarCloud
Q) | sve
Dy osvreucker Ag) cittae
Trang 6sonarcloud MyProjects Mylssues Explore a
- Chọn những project ta muốn phân tích -> chon Set Up
*Lưu ý: khi dùng phiên bản miễn phí, ta chi co thé import va phan tich cac public project Néu muon phan tich cac private project, ta can nang cap lén phiên ban Premium
1}
MyProjects Mylssues Explore a
Set up project for Clean as You Code
Which part of your cođe w
Be 00 ee 01500 1) lá 14c
Cn nh =r ( ethodolog htly ACr0SS proj
‘TheBoyWhoDev - Administration - New Code
The new code for this project will be based on
Trang 7BD rceda [Amazon if Facebook @ Ir Peers
sonarcloud My Projects Mylssues — Explore Q
POOP ty
IIENB @
Choose your Analysis Method
€)With GitHub Actions Ấ With Travis CI ©)WithCircleCt
'With Amazon CodeCatalyst 'With other Cl tools
DU oul
Administration
- Tiếp đến ta sẽ phải cầu hình các project (repository) để SonarCloud có thể phân tích
- Phan nay ta sẽ thực hiện cầu hình trên Github (mục đích là đề cấp quyền cho
SonarCloud truy cập vào các tài nguyên, source code trong repository va tao workflow dé thực hiện phân tích)
- Tai muc Choose your Analysis Method, ta chon With GitHub Actions
MyProjects Mylssues Explore Q
CỔ Ww 7 q
BS C)
Analyze a project with a GitHub Action
1 Create a GitHub Secret
Tecan Ty, go to Settings > Secrets > Actions [2 and create a new with the following details
@ initne 71 N6 s
© In the Value field, enter 4b12e851981b3cf4bb6cef4c6dđ79xcbf4lbf9ig (Œ
2 Create or update a build file
Information Pee eo eon nal và
Maven Gradle C,C+*+orObjC NET Other (for JS,TS,Go,Python,PHP, )
óc
- Giao điện sau đó sẽ như sau
- Ta copy 2 mục Name và Value
Trang 8Agoda [3 Amaon Facebook [@ Instagram Ml New folder
Code PT: ON eee Aetion: tojects — ` —
General Repository name
TU |
Go oi
non Require contributors to sign off on web-based commits
'eriiRcate of Ovigin (DCC Default branch
Pierre) oe Strom) D TT TT oe td
ty made, unle
Social Preview Upload an image to customize your reposit
% should be at least 640
- Tại GitHub ta truy cập vào Repository muốn phân tích
- Chon Settings -> Secrets and Variables -> Actions
Trang 92 Create or update a build file
Pata eas er ee ina ied
Maven Gradle C, C++ or ObjC mais Other (for JS, TS, Go, Python, PHP,
Create or update your = _github/workflows/build.ynl oa
Here is a base configuration to run a SonarCloud analysis on your master branch and Pull Requests lf you already have some GitHub Actions, you mỉght want to just add
some of these new steps to an existing one
DU Grade C, C++ or ObjC NET Other (for JS, TS, Go, Python, PHP,
Create or update your github/workflows/buttd_ymt
Here is a base configuration to run a SonarCloud analysis on your master branch and Pull Requests If you already have some GitHub Actions, you might want to just add
some of these new steps to an existing one
Trang 10- Quay lai SonarCloud Tai muc Create or Update a build file Ta chon tuy theo ngôn ngữ
hay phan mềm được st dụng trong code base Ví dụ cho Java thì ta có thé dùng Maven
hay Gradle
Chang han 6 day ta chon Other (for JS, TS, Go, Python, PHP, .)
- Một file workflow duroc build san dé ta co thé str dung (day 1a file default ta có thé chỉnh sửa thêm tùy theo mục đích sử dụng)
- Tại nhánh Main của repository Ta tạo một file có tên ðz//đym/ theo đường dẫn github/workflow/ Va copy toan bé néi dung trên vào file
3 Createa sonar-project properties file
Create a configuration file in the root directory of the project and name it sonar-pro3ect.properttes (@
[PUBLIC] Summary lssues SecurityHotspots Meas: sode Activity
Trang 11Phần 2: Thực hiện phân tích kết quả và thông số mã nguồn
- Tai dashboard phia bên trai man hinh ta chon Main Branch
- Quality Gate như một điều kiện tiêu chuẩn đã được hệ thông tính toán đề kiểm tra
source code Nêu tỉnh trạng Quality Gate được ghi là Passed, tức là proJect đã đạt tiêu chuan dé release thanh san pham va sv dung
Trang 12A few extra steps are needed for SonarCloud 12.6% Duplications @
to analyze your code co ge
Setup coverage analysis G
Division by a tainted value, possibly zero Cert CW@
Reliability
CodePTIT Intentionality issue Division by a tainted value, possibly zero oad a
OOpenv Notassigned ° Smin effort - 1 year ago
Maintainability (tinh san sang): nhung doan code có cú pháp phức tap, dai dong, không cần thiết Có thể làm giảm hiệu suất của chương trình, khiến chương trình hoạt động không 6 én định, hay khó bảo trì
12
Trang 13CodePTIT - C/
NO 9V
Consistency issue Peer cag
Define each identifier in a dedicated statement
OoOpeny @ Smin effort
Pearly
Adaptability
CodePTIT
- Software Quality Consistency issue
Define each identifier in a dedicated statement
intainability ®
Maintainability
Nhang AC —_} Consistency issue
High \ Define each identifier in a dedicated statemen
© Medium O Open~ Not igned tainability @
based-on-misra cert
based-on-misra cert J0N-0 0504 I7
Security (tính bảo mật): những đoạn code chứa lỗ hồng có thể bị khai thác bởi
hacker
Summary lssues SecurityHotspots Measures Code Activity
Filters Clear All Fiters | nh a lta
x Clean Code Attribute
Intentionality issue Add a field width specifier to this “%s" placeholder
cwe injection +
10min effort 1 year ago
eM eles effort
Security review (danh giá trực quan của nhà phat triển): nhưng đoạn code có thể chưa lỗi hoặc không, cần xem xét thủ công đề đánh giá
13
Trang 141.1% Security Hotspots Reviewed @ 7 - :
sf P Make sure use of "strcpy" is safe here @ Review priority:
High Using or “wescpy @curity-sensitive c:S5801
Buffer Overflo
Status: To Review Make sure use of "strcpy" siete dita is safe here Meals This Security Hotspot needs to be reviewedl to Review - Peer eid
assess whether the code poses a risk iN To Make sure use of "strcpy” is safe here
'Where is the risk? 'What's the risk? Assess the risk How can l fix it? Activity
ECR CRS Rs gis tre ae ee RI
Make sure use of "strcpy" is safe here
Make sure use of "strcpy" is safe here
Summary Issues SecurityHotspots Measures Code Activity
Duplicated Lines (%) 12.6% See history
[ CodePTIT - DSA/Thực Hành/SỐ BƯỚC DI CHUYỂN ÍT NHẤT copy.cpp
Trang 15Division by a tainted value, possibly zero cert cwe +
© Openy Not ( * 5min effort 1 year ago CodePTIT - C/C01006.c
_Ì _ Intentionality issue
Division by a tainted value, possibly zero cert cwe +
O Openv Not 2d ( ) Smin effort 1 year ago CodePTIT - C/C01007.c
| Intentionality issue
Division by a tainted value, possibly zero cert cwe +
O Openy Not S6 ( 5min effort 1 year ago
Severity (độ nghiệm trọng): mức độ ảnh hưởng của các lỗi trên
Rule: các quy tắc được áp dụng để phân tích mã nguôn, mỗi rule đảm nhiệm việc điều tra một nguy cơ tiêm ân trong mã nguồn
Ví dụ: Rule Multiple variables should not be declared on the same line, đề chiết xuất ra các dòng code khai báo mà có quá nhiều biên được khai báo cùng lúc
Quality Profile: đây là nơi lưu trữ tất cả các rules cho từng ngôn ngữ cụ thể mà Sonarcloud hiện đang quản lí
15
Trang 17Sonar way GUE DEFAULT 25 3 years ago 27 days ago
Sonar way GUERIN DEFAULT 5 days ago 27 days ago
Sonar way [NTRNH DEFAULT 4monthsago 27 days ago
Mission critical EYER 1 month ago Never
Sonar way GUE DEFAULT 4monthsago 27 days ago
- Mỗi ngôn ngữ sẽ có I đefault proñle(tập các rule mặc định) do SonarCloud tự tạo, các
proJect không được chỉ định một profile cụ thể khác tự động sử dụng default profile khi phân tích, nêu muốn thêm rule ta có thê tự tạo profile và thêm rule vào đó Các rule mới
cũng luôn được cập nhật và thêm vào default profile thường xuyên Tương tự các rule đã
lỗi thời cũng được loại bỏ khỏi default profile
- Sau khi chạy phân tích, nếu các rule tìm được các đoạn code tương ứng, kết quả sẽ được
trả về trong muc Issues
17
Trang 18O Openv_ Not assigned 5min effort Filters
Consistency issue
v _Clean Code Attribute Define each identifier in a dedicated statement based-on-misra
Tế he co) OOpenv Notassigned TT a 5min effort
Intentionality
The left operand of '*' is a garbage value based-on-misra cwe
O Openy Not assigned ^] 15min effort 1 year agc
- Software Quality
Security CodePTIT - C/C01046.c
Reliability
[S20 eats Maintainabilit webinars / Define each identifier 7 A Ta in a dedicated statement h based-on-misra cert
© Openy Not assigned Maintainability @ 5min effort - 1 year ago
The left operand of '*' is a garbage value 2
Variables should be initialized before use c:S836
Software qualities impacted: @
Summary lssues SecurityHotspots Measures Code Activity
25 / 3,988 issues
'Where is the issue? 'Why is this an issue? How can l fix it? Activity Le) 3
Define each iđentifier in a đedicated
Ant no The left operand of '*' is a garbage value
‡ 1 execution flow'
without an initial value
Ed Loop condition is false Execution
©ontinues on line 17
BO The left operand of ** is a garbage :
Naviga Ns The left operand of '*' is a garbage value
Trang 193 Tainted Flow (luồng đữ liệu chứa lỗi) — chủng được đánh số và giải thích dé ta hiểu rõ hơn về lỗi
4 Where is the issue — cac dong code ban sé duoc highlight dé dé dang tim kiém 5.Why is this an issue? — giải thích tai sao đây lại là một lỗi và các vẫn đề nó có thê gây nên cho nhà phát triên
Where is the issue? Why is this an issue? How can | fix it? Activity More info
variable of any built-in type (s ae float , and point 2clared without an init: e is not initialized to any alue Consequently, if no value is [ 0 such a ve Ì - ( lat uses it has no defined behavior
L +
return *p;
types, such as arrays or struct / class types without a constructor,
- Một biến cục bộ thuộc bắt kỳ kiểu dữ liệu dựng sẵn nào (chăng han nhy int, float va con
trỏ), được khai báo mà không có giá trị ban đầu sẽ không được khởi tạo thành bất kỳ gia trị cụ thê nào Do đó, nêu không có giá trị nào được gán cho biến đó trước tiên thì code