YouTube2Text

SQL Injection | Complete Guide — Transcript

by Rana Khalil · 12,141 words · 2,145 segments · language en · Watch on YouTube

Full transcript

  1. 0:01hi everyone welcome back to another
  2. 0:03video in the web security academy series
  3. 0:06in today's video we'll be discussing the
  4. 0:08theory behind sql injection
  5. 0:10vulnerabilities
  6. 0:12this video is pretty much a brain dump
  7. 0:13of everything that i consider to be
  8. 0:15fundamental knowledge when it comes to
  9. 0:17sequel injection attacks and so it's
  10. 0:19going to be a long one
  11. 0:20so if you're only interested in a
  12. 0:22specific topic related to sql injection
  13. 0:24then i would recommend
  14. 0:26going to the description and checking
  15. 0:28out the timestamps that i have mapped
  16. 0:29out to the different topics that i cover
  17. 0:31in this video
  18. 0:33okay let's get started we'll start off
  19. 0:36with describing what a sequel injection
  20. 0:38vulnerability is
  21. 0:39and the technical details behind this
  22. 0:41category of vulnerabilities
  23. 0:43so in this section we'll discuss topics
  24. 0:46like
  25. 0:47what are sql injection vulnerabilities
  26. 0:49what are the different types of sql
  27. 0:50injection vulnerabilities
  28. 0:52how common are sql injection
  29. 0:54vulnerabilities and so on
  30. 0:56next we'll cover how to find sql
  31. 0:58injection vulnerabilities from both the
  32. 1:00black box
  33. 1:01and the white box perspective so if
  34. 1:03you're given
  35. 1:04an application and possibly even the
  36. 1:06source code of the application
  37. 1:08how would you approach testing the
  38. 1:10application in order to determine if
  39. 1:12it's vulnerable to sql injection
  40. 1:16in the third section we'll cover how to
  41. 1:18exploit sql injection vulnerabilities
  42. 1:22so imagine you're testing an application
  43. 1:24and you discover
  44. 1:25that one of the input vectors or one of
  45. 1:28the
  46. 1:28parameters is potentially vulnerable to
  47. 1:30sql injection
  48. 1:31in that case how do you go about
  49. 1:33exploiting that vulnerability
  50. 1:35in order to gain access to sensitive
  51. 1:37information or even potentially gain
  52. 1:38remote code execution
  53. 1:40on that server
  54. 1:44last but not least now that you have
  55. 1:46successfully found and exploited a sql
  56. 1:48injection vulnerability
  57. 1:50what recommendations can you give to the
  58. 1:52client in order to remediate the
  59. 1:54vulnerabilities that you found
  60. 1:57so in this section we'll cover the
  61. 1:58different techniques that you can use to
  62. 2:00prevent sql injection vulnerabilities
  63. 2:03okay let's start off with the first
  64. 2:05section what is sql injection
  65. 2:08sql injection is a vulnerability that
  66. 2:10consists of an
  67. 2:11attacker interfering with the sql
  68. 2:13queries that an application makes to the
  69. 2:15backend database
  70. 2:17so if you as an attacker can manage to
  71. 2:19interact and change the sql query
  72. 2:21by adding sql characters or sql code
  73. 2:24in an input vector of an application
  74. 2:27then the application is definitely
  75. 2:28vulnerable to sql injection
  76. 2:30now to better understand this let's take
  77. 2:33an example
  78. 2:34imagine you have an attacker a web
  79. 2:37server
  80. 2:37and a database the application that the
  81. 2:40web server hosts has an authentication
  82. 2:42functionality where the user has to put
  83. 2:44in their
  84. 2:46username and password and once the
  85. 2:49username and password
  86. 2:50are put in equity is made to the
  87. 2:52database to see if this username and
  88. 2:54password
  89. 2:54are in the database and if they are then
  90. 2:57the user is presented with
  91. 2:58uh his or her profile page if the user
  92. 3:02enters an incorrect username or password
  93. 3:04then the user is presented with an error
  94. 3:06message that says
  95. 3:07you have entered an incorrect username
  96. 3:09or password
  97. 3:11so in this case we clearly have two
  98. 3:13input vectors the username field and the
  99. 3:15password field a talk to the backend
  100. 3:17database so they get included in a query
  101. 3:19in the backend database
  102. 3:21and so we definitely need to test these
  103. 3:23input vectors
  104. 3:24for a sql injection vulnerability to see
  105. 3:26if that input
  106. 3:27is validated in any way
  107. 3:31so let's see what an attacker can do in
  108. 3:33this case
  109. 3:34what an attacker is doing is in the
  110. 3:36username field instead of just adding a
  111. 3:38username
  112. 3:39the attacker adds the username admin the
  113. 3:42account that the attacker wants to
  114. 3:44compromise but then the attacker also
  115. 3:47adds some sql characters
  116. 3:49after the username so add single quote
  117. 3:53and then add double dash which stands
  118. 3:55for a comment
  119. 3:57so what happens is that
  120. 4:00this username gets included in a query
  121. 4:03so what the query does
  122. 4:04at the back end is it says select star
  123. 4:06from users so select all the entries
  124. 4:08from the users table
  125. 4:10where username is equal to whatever
  126. 4:12username is put in here
  127. 4:14and password is equal to whatever
  128. 4:15password is put in here
  129. 4:18now if this input is properly validated
  130. 4:21and we'll talk about that
  131. 4:22in the next couple of sections if this
  132. 4:25input is properly validated then the
  133. 4:27database will literally look for a
  134. 4:29username
  135. 4:30that is equal to admin dash admin code
  136. 4:34dash dash in the database
  137. 4:35and it won't find one because no one has
  138. 4:37that username and so the attacker would
  139. 4:39be presented with
  140. 4:40an incorrect username or password
  141. 4:44um however if this application is
  142. 4:47vulnerable to sql injection so it
  143. 4:49doesn't use parameterized queries it
  144. 4:51doesn't validate
  145. 4:52user input then what will happen is that
  146. 4:55any sql characters or sql code that the
  147. 4:58attacker adds as part of the payload
  148. 5:00will become part of the query and so in
  149. 5:02this case what's happening
  150. 5:03is select star from users so select the
  151. 5:06entries from the users table
  152. 5:07where username is equal to admin the
  153. 5:10single quote over here that the
  154. 5:12attacker included closes the string or
  155. 5:14the username
  156. 5:16and then the double dash stands for a
  157. 5:18comment and so it says comment out the
  158. 5:20rest of the query
  159. 5:22and so what happens is the query becomes
  160. 5:25this so select
  161. 5:26all the entries from the users table
  162. 5:27where username is equal to
  163. 5:30admin now admin is an existing username
  164. 5:33in the database
  165. 5:34and so what happens is that the database
  166. 5:37will return the admin user profile
  167. 5:39and the attacker gets logged in as the
  168. 5:41admin user
  169. 5:42so essentially what happened over here
  170. 5:45is that the attacker
  171. 5:47exploited a sql injection vulnerability
  172. 5:49in the login functionality of the
  173. 5:51application
  174. 5:52in order to bypass authentication so in
  175. 5:54this case the attacker no longer needed
  176. 5:56to know the admins password
  177. 5:58because the attacker was able to change
  178. 6:00the query to no longer
  179. 6:02ask for the admins password so that's
  180. 6:04one
  181. 6:05example of how you could use uh sql
  182. 6:07injection vulnerabilities
  183. 6:09in order to um in order to bypass
  184. 6:12authentication
  185. 6:13on login functionality now let's talk
  186. 6:16about
  187. 6:16what are other impacts of sql injection
  188. 6:20attacks
  189. 6:20and the real answer to this is that it
  190. 6:23really depends on the context
  191. 6:24of the sql injection that you're
  192. 6:26exploiting so some sql injection
  193. 6:28vulnerabilities will only allow you to
  194. 6:30view data
  195. 6:31whereas others will allow you to
  196. 6:32actually modify data
  197. 6:34so when i was coming up with this slide
  198. 6:36instead of listing the infinite
  199. 6:38number of things that you could do to
  200. 6:40applications with sql injection
  201. 6:42vulnerabilities
  202. 6:43i instead decided to just compare it to
  203. 6:45the asea triad
  204. 6:46so how it affects confidentiality
  205. 6:49integrity
  206. 6:50and availability so in terms of
  207. 6:52confidentiality
  208. 6:53some sql injection vulnerabilities will
  209. 6:55allow you to view sensitive information
  210. 6:57such as usernames
  211. 6:58and passwords so the application's
  212. 7:00username and password
  213. 7:02and that's an example of how you impact
  214. 7:05the confidentiality
  215. 7:06of an application by accessing
  216. 7:08information that you shouldn't
  217. 7:10have access to in terms of integrity
  218. 7:13some sql injection vulnerabilities will
  219. 7:15allow you to alter
  220. 7:16data in a database and an example of
  221. 7:19that
  222. 7:19that i exploited not too long ago is an
  223. 7:22application that allows you to
  224. 7:24update an email address and so because
  225. 7:27this was vulnerable to sql injection
  226. 7:29i was able to update another user's
  227. 7:31email address
  228. 7:33to one that i control and so uh what i
  229. 7:36did
  230. 7:36is because i control this email address
  231. 7:38i reset the password
  232. 7:40of the user and the reset token was sent
  233. 7:42to the email address that
  234. 7:43i own and so i hijacked the user's
  235. 7:46account and that's an example of how to
  236. 7:49effect the integrity of the application
  237. 7:51because you were able to alter data that
  238. 7:53you shouldn't
  239. 7:54have otherwise been able to alter it
  240. 7:56also affected the availability
  241. 7:58of the application because i denied when
  242. 8:00i reset the user's password i denied
  243. 8:03access to the user's account
  244. 8:07and so i affected the availability of
  245. 8:09the application
  246. 8:12and of course in certain scenarios sql
  247. 8:15injections could actually lead to remote
  248. 8:17code execution
  249. 8:18on the operating system and that affects
  250. 8:20confidentiality integrity
  251. 8:22and availability all at once and you
  252. 8:25would gain
  253. 8:26access to the operating system with the
  254. 8:28same privilege that the database is
  255. 8:30running with
  256. 8:30and so you got to be very careful in
  257. 8:32terms of
  258. 8:33making sure that the database runs with
  259. 8:36the least privilege
  260. 8:36possible and we'll discuss that in the
  261. 8:39last section
  262. 8:40when we're talking about things like
  263. 8:42defense in depth
  264. 8:43and how to prevent sql injection
  265. 8:45vulnerabilities
  266. 8:47okay so um we talked about the impact
  267. 8:50of sql injection vulnerabilities now the
  268. 8:53question
  269. 8:54is how common and how critical are sigma
  270. 8:57injection vulnerabilities
  271. 8:59and one way to measure that and it's not
  272. 9:01bulletproof is the
  273. 9:02awasp top 10 list so for those of you
  274. 9:06that have never heard of the oas top 10
  275. 9:08project
  276. 9:09it's essentially the list of top 10 most
  277. 9:12critical
  278. 9:12security risks to web applications it's
  279. 9:15updated every couple of years so you
  280. 9:16could see over here i've got the 2010
  281. 9:18list
  282. 9:19the 2013 list and then the 2017 uh
  283. 9:22list i believe 2020 is in the works so i
  284. 9:26think it was submitted but has not been
  285. 9:28approved yet but i could be wrong so i
  286. 9:30think
  287. 9:302017 is the latest one that we have
  288. 9:32right now and you'll notice over here it
  289. 9:34does change across the years so you've
  290. 9:35got cross-site scripturing
  291. 9:37was the second most critical risk to web
  292. 9:40applications in 2010
  293. 9:43and then it it became the third most
  294. 9:46critical risk in 2013
  295. 9:49and then it dropped all the way to uh
  296. 9:51the seventh most critical risk
  297. 9:53in 2017 and you'll see some of them
  298. 9:56completely disappear
  299. 9:57so insufficient transport layer
  300. 9:59protection was number nine
  301. 10:01of most critical risks in 2010 but that
  302. 10:04but it completely disappeared in 2013
  303. 10:06and 2017
  304. 10:07um and that's probably because it's so
  305. 10:09easy to now get certificates
  306. 10:10and use https on your side even
  307. 10:13malicious sites use https right now so
  308. 10:15it's definitely
  309. 10:16um not a risk anymore but then you see
  310. 10:19also new ones get added so
  311. 10:21for example insecurity serialization
  312. 10:23over here
  313. 10:24was never a risk in 2010 or 2013 was not
  314. 10:27part of the list
  315. 10:28and in 2017 it became part of the list
  316. 10:32now for sql injection vulnerabilities
  317. 10:35they fall
  318. 10:36under the first most critical risk
  319. 10:39to web applications so they fall under
  320. 10:42the injection category however it's
  321. 10:45worth
  322. 10:46mentioning that this covers all of
  323. 10:48injection vulnerabilities not just sql
  324. 10:50injection vulnerabilities
  325. 10:52in fact uh the likelihood of sql
  326. 10:54injection vulnerabilities
  327. 10:55existing in applications has decreased
  328. 10:58across the years for many reasons and
  329. 11:00we'll discuss that in the last section
  330. 11:02when we discuss how to prevent sql
  331. 11:03injection vulnerabilities
  332. 11:05nevertheless um it is still a critical
  333. 11:08risk
  334. 11:08because as we saw in the
  335. 11:11past couple of slides if you do have
  336. 11:14even just one parameter that is
  337. 11:16vulnerable to sql injection it could
  338. 11:18potentially lead to
  339. 11:20remote code execution on the server so a
  340. 11:22full compromise of the underlying server
  341. 11:25all right so in the past couple of
  342. 11:27slides we got a high level overview of
  343. 11:29what sql injection vulnerabilities are
  344. 11:32now we're going to go at a more granular
  345. 11:34level and talk about the different types
  346. 11:36of sql injection
  347. 11:38sql injection vulnerabilities can be
  348. 11:40classified into three
  349. 11:42major categories the first is in bend
  350. 11:45or classic sql injection the second
  351. 11:48is inferential or blind sql injection
  352. 11:51and the third is out-of-band sql
  353. 11:54injection
  354. 11:55now in-band or also known as classic
  355. 11:58sql injection vulnerabilities are ones
  356. 12:01where the attacker
  357. 12:02uses the same communication channel to
  358. 12:04both launch
  359. 12:05the attack and gather the results of the
  360. 12:07attack if that doesn't make much sense
  361. 12:09to you that's okay
  362. 12:10don't worry i actually have a couple of
  363. 12:12slides explaining each type in detail
  364. 12:15right now i'm simply giving you a high
  365. 12:17level overview of what each type is
  366. 12:20now inbend or classic sql injection is
  367. 12:23divided into two types
  368. 12:25error-based sql injection and
  369. 12:26union-based sql injection
  370. 12:28error-based sql injection is a technique
  371. 12:30where you force the database
  372. 12:32to generate an error giving you more
  373. 12:34information about how things
  374. 12:36operate at the back end so an example
  375. 12:39would be if you have a parameter that is
  376. 12:41vulnerable to sql injection
  377. 12:43and then you put into that parameter a
  378. 12:45single quote and that breaks the backend
  379. 12:47query which generates
  380. 12:48um an error at the application level and
  381. 12:51so
  382. 12:52what happens is that the error tells you
  383. 12:54things like
  384. 12:55uh the version of the database so the
  385. 12:57type of database that they're using at
  386. 12:59the backend
  387. 13:00the version of the database and
  388. 13:02sometimes the
  389. 13:03error will also tell you the exact sql
  390. 13:05query that is being used at the backend
  391. 13:07and this would make your life so much
  392. 13:08easier because if you know the exact
  393. 13:10query you don't have to guess it using
  394. 13:12trial and error
  395. 13:13and this way you could just build your
  396. 13:15sql payload so that's an example of
  397. 13:17error-based sql injection for union
  398. 13:20based sql injection it's a technique
  399. 13:21that leverages the union operator to
  400. 13:24combine the results of two queries into
  401. 13:26a single result set
  402. 13:27so in this technique you don't just
  403. 13:29output the result of the original query
  404. 13:31that the application
  405. 13:32makes but you also output the result of
  406. 13:34a query of your choosing
  407. 13:36and of course something that you would
  408. 13:38be interested in is
  409. 13:39uh for example the usernames and
  410. 13:42passwords of the users of the
  411. 13:43application
  412. 13:44so you would use the union operator to
  413. 13:46output
  414. 13:48the credentials of the users of the
  415. 13:50application and again we'll see examples
  416. 13:52of that in the next coming
  417. 13:54slides the next category which is
  418. 13:57inferential
  419. 13:58or also known as blind sql injection in
  420. 14:01this type of vulnerability there's no
  421. 14:03actual transfer of data
  422. 14:05via the web application so you don't see
  423. 14:07the results
  424. 14:09in the application itself instead you're
  425. 14:11stuck asking the application true or
  426. 14:12false questions
  427. 14:14which is what boolean base sql injection
  428. 14:16is
  429. 14:17or causing the database uh to pause for
  430. 14:20a specified period of time
  431. 14:22which is time-based sql injection in
  432. 14:24order to determine if what you're asking
  433. 14:26the application is correct or not
  434. 14:28obviously the way you exploit to blind
  435. 14:31sql injections is more difficult
  436. 14:33than classic sql injections however uh
  437. 14:36the
  438. 14:36impact even if you're not seeing the
  439. 14:38results right away in your application
  440. 14:40the impact of exploiting the
  441. 14:41vulnerability is still as bad as
  442. 14:44in bend or classic sql injection and
  443. 14:46we'll see examples of that in a bit
  444. 14:48okay the third and last category
  445. 14:51is out-of-band sql injection this occurs
  446. 14:54when the attacker is unable to use the
  447. 14:56same channel to launch the attack and
  448. 14:58gather the results of the attack
  449. 15:00it usually relies on the ability of an
  450. 15:02application
  451. 15:03to make a network connection so for
  452. 15:05example a dns
  453. 15:06or http request to deliver data to an
  454. 15:09attacker
  455. 15:11i've seen people loop this type of sql
  456. 15:14injection
  457. 15:15under inferential or blind sql injection
  458. 15:19which is a fair thing to do however
  459. 15:22if you look at resources online most of
  460. 15:24the resources out there will separate
  461. 15:26them into uh three different categories
  462. 15:28and that's why i decided to also
  463. 15:30separate them into
  464. 15:31three different categories in my video
  465. 15:34okay let's go more in depth about each
  466. 15:37type starting with
  467. 15:38in-band or classic sql injection
  468. 15:42we talked about in-band sql injection
  469. 15:45vulnerabilities
  470. 15:46occurring when the attacker is able to
  471. 15:48use the same communication channel
  472. 15:50to both launch an attack and gather the
  473. 15:52results of an attack
  474. 15:54so to put this in plain simple english
  475. 15:57if you exploit a sql injection
  476. 15:58vulnerability by sending a sql payload
  477. 16:01in one of the fields of an application
  478. 16:03and then you see the result
  479. 16:04of your sql payload presented directly
  480. 16:07to you
  481. 16:07in the application then this falls under
  482. 16:09the
  483. 16:11category of in-band sql injection now
  484. 16:14uh just by the nature of how this
  485. 16:17specific type of vulnerability works
  486. 16:19it's much easier to exploit than other
  487. 16:21categories of vulnerabilities
  488. 16:23and the reason behind that is because
  489. 16:25you actually see the result of your
  490. 16:27attack in the response of the
  491. 16:28application versus having to infer the
  492. 16:31result like you would have to
  493. 16:32in a blind sql injection again we said
  494. 16:35that there's two types of
  495. 16:37in-band sql injection error-based sql
  496. 16:39injection
  497. 16:40and union-based sql injection and we'll
  498. 16:43start off with error-based sql injection
  499. 16:46error-based sql injection is an in-band
  500. 16:49sql
  501. 16:50injection technique that forces the
  502. 16:52database to generate an
  503. 16:54error giving the attacker information
  504. 16:56that could potentially
  505. 16:57aid in developing the injection payload
  506. 17:00so
  507. 17:00let's take an example imagine you have
  508. 17:03an
  509. 17:04application www.random.com that has an
  510. 17:07id parameter that is vulnerable
  511. 17:09to sql injection in this case we're
  512. 17:12putting a single quote in the id field
  513. 17:14and because that is a sequel character
  514. 17:16and this is vulnerable to sql injection
  515. 17:19that single quote will become a part of
  516. 17:21the query and so
  517. 17:22it'll break the backend query and it'll
  518. 17:25output an error and an example error
  519. 17:27is you have an error in your sql syntax
  520. 17:29check the manual that corresponds to
  521. 17:31your mysql server version
  522. 17:33blah blah blah so in this case it tells
  523. 17:35you that you're using mysql
  524. 17:37that's good information to know it might
  525. 17:39also tell you the version
  526. 17:40and again in certain scenarios the error
  527. 17:42might also give you the query that
  528. 17:44happens when you
  529. 17:45put an id parameter so what's happening
  530. 17:47when um
  531. 17:49you put for example an integer in the id
  532. 17:51parameter
  533. 17:52uh the exact query that happens uh at
  534. 17:54the back end and this way if you have
  535. 17:56the exact query it's so much easier for
  536. 17:58you to develop your payload versus
  537. 18:00sending a bunch of requests to the
  538. 18:01um the application in order to determine
  539. 18:04what the payload is
  540. 18:06and so this is in-band sql injection or
  541. 18:09classic sql injection because you see
  542. 18:11the output of your sql injection right
  543. 18:13there in the application
  544. 18:15and it's error-based because the idea is
  545. 18:18that you
  546. 18:19put an error in the application
  547. 18:22next we have union based sql injection
  548. 18:26union based sql injection is a type of
  549. 18:29in-band sql injection technique
  550. 18:31that uses the union sql operator to
  551. 18:33combine the results of two queries into
  552. 18:36a single result
  553. 18:37set again i mentioned in the previous
  554. 18:39couple of slides
  555. 18:40that i don't want to output just the
  556. 18:43result of the original query
  557. 18:45i want to output a query of my choosing
  558. 18:48and if an application is vulnerable to
  559. 18:51union based sql injection then i get the
  560. 18:53ability to
  561. 18:54output the result of a query of my
  562. 18:56choosing and combine it with the
  563. 18:58original
  564. 18:58query that the application is with the
  565. 19:01result of the original query that the
  566. 19:02application is making
  567. 19:04let's take an example you have your
  568. 19:06application www.random.com it has an id
  569. 19:10field that is vulnerable to a union
  570. 19:13based sql injection
  571. 19:15and if you put in a valid id then the
  572. 19:17page will display all the products that
  573. 19:19are associated to that id
  574. 19:22now instead of just displaying the
  575. 19:24products associated to a specific id
  576. 19:26i want to output the results of another
  577. 19:28query and in this case the query is
  578. 19:31select the username
  579. 19:32and the password from the users table so
  580. 19:35output all the usernames and passwords
  581. 19:37from the users table and then comment
  582. 19:39out the rest of the query
  583. 19:41so in this case it will
  584. 19:44output the results of this query over
  585. 19:46here we didn't put an id
  586. 19:47and so that would be empty and then
  587. 19:49it'll combine it with the results of
  588. 19:50this query over here which is the
  589. 19:52username
  590. 19:52so carlos an administrator and the
  591. 19:54passwords of the
  592. 19:56users in the users table now
  593. 19:59of course the union operator has certain
  594. 20:01conditions for it to work and so it's
  595. 20:03not as simple as hey i'm going to output
  596. 20:05whatever i want
  597. 20:06in as many fields as i want um it's
  598. 20:09it's not the case instead there are
  599. 20:11certain conditions that you have to
  600. 20:12follow and we'll discuss that in
  601. 20:14the exploitation section of this video
  602. 20:18okay so that covers in-bend or classic
  603. 20:21sql injection
  604. 20:22next let's talk about inferential or
  605. 20:24blind sql injection
  606. 20:27inferential or blind sql injection is
  607. 20:30a sql injection vulnerability where
  608. 20:32there's no actual transfer of data via
  609. 20:34the web application
  610. 20:36so unlike in-band sql injection where we
  611. 20:39directly saw the output of the sql
  612. 20:41injection displayed for us in the
  613. 20:43application
  614. 20:44in the case of blind sql injection the
  615. 20:46database does not
  616. 20:47output data to the web page and so this
  617. 20:50forces the attacker to steal
  618. 20:52data by asking the database a series of
  619. 20:54true and false questions
  620. 20:55and then determining based on how the
  621. 20:58application responds
  622. 21:00whether uh whether the application
  623. 21:02responded with yes this is a true
  624. 21:04statement versus no this is not a true
  625. 21:06statement
  626. 21:07um and again uh this will become more
  627. 21:09clear once we discuss the different
  628. 21:11types of
  629. 21:11blind sql injection and we take uh
  630. 21:14examples
  631. 21:15uh now just because an attacker can't
  632. 21:17directly display data from the database
  633. 21:18that doesn't mean that this is a
  634. 21:20harmless vulnerability
  635. 21:21like i said in the previous slides in
  636. 21:23fact it's just as dangerous as embedded
  637. 21:25sql injection
  638. 21:26because you can extract information from
  639. 21:28the database by ex
  640. 21:30by asking true and false questions it's
  641. 21:32just that this type of vulnerability
  642. 21:33takes a little bit longer and a little
  643. 21:36bit more skill set
  644. 21:37in order to exploit
  645. 21:42okay and we said there are two types of
  646. 21:44blind sql injection boolean based sql
  647. 21:46injection
  648. 21:47and time based sql injection let's talk
  649. 21:50about
  650. 21:51boolean based sql injection
  651. 21:54boolean based sql injection is a blind
  652. 21:57sql injection technique that uses
  653. 21:59boolean conditions to return a different
  654. 22:01result set
  655. 22:01depending on whether the query returns
  656. 22:04true or
  657. 22:05a false result so in simple english
  658. 22:08again the attacker sends different sql
  659. 22:10payloads that ask the database
  660. 22:12true and false questions and depending
  661. 22:14on the how the database responds then
  662. 22:16you could infer whether the data
  663. 22:17responded
  664. 22:18the application responded with true or
  665. 22:21false
  666. 22:22this is sometimes referred to as content
  667. 22:24based blind sql
  668. 22:25injection so you'll see those two terms
  669. 22:28used uh
  670. 22:29interchangeably now let's take an
  671. 22:32example so that this becomes
  672. 22:34a little bit more clearer imagine again
  673. 22:36you have an application www.random.com
  674. 22:40and it has an id parameter that is
  675. 22:43vulnerable to a blind sql injection
  676. 22:45and imagine in this case you can't
  677. 22:47simply use the union operator to output
  678. 22:50data in the database like we did
  679. 22:52with union union-based sql injection so
  680. 22:55in this case what you can try
  681. 22:56uh to test if the field is vulnerable to
  682. 22:59sql injection
  683. 23:00is first inject it with a false payload
  684. 23:02so force a payload that you know is
  685. 23:04false
  686. 23:04and then see how the application
  687. 23:06responds and then force a payload that
  688. 23:08you know is true
  689. 23:09and see how the application responds and
  690. 23:11if the application responds
  691. 23:13differently in the false payload versus
  692. 23:15the true payload then this is
  693. 23:17uh vulnerable to boolean based blind sql
  694. 23:20injection
  695. 23:22so let's take the backend query so
  696. 23:24imagine over here
  697. 23:26when you enter an id number
  698. 23:29the query this is the query that happens
  699. 23:31at the backend so select the title
  700. 23:34from the product table where id is equal
  701. 23:36to one and because id is equal to one is
  702. 23:38a valid id in the table
  703. 23:40the application will display the title
  704. 23:44of the products that are associated to
  705. 23:46id1
  706. 23:48now if we want to force a false
  707. 23:51payload what we can do is we could use a
  708. 23:54conditional statement
  709. 23:55and one is equal to two so the back end
  710. 23:58query would be select the title
  711. 24:00from the product table where id is equal
  712. 24:02to one and one is equal to
  713. 24:04two now we said id is equal to one well
  714. 24:06is a valid id
  715. 24:07and so this will evaluate to true and
  716. 24:09one is equal to two
  717. 24:11um we'll evaluate to false because one
  718. 24:13will never be equal to two and so
  719. 24:15because this is an
  720. 24:16and statement over here you're using the
  721. 24:19and operator
  722. 24:20they both have to be true in order for
  723. 24:22the entire where clause to evaluate to
  724. 24:24true so this evaluates to true this
  725. 24:26evaluates to false
  726. 24:28true and false is equal to false
  727. 24:31and so the entire where condition
  728. 24:32evaluates to false
  729. 24:34and so it won't display a title uh from
  730. 24:37the product table
  731. 24:38so in this case if you force the false
  732. 24:40statement you won't get the title of the
  733. 24:42products that are associated to id is
  734. 24:44equal to 1.
  735. 24:46now let's see what happens when you
  736. 24:47force a true statement
  737. 24:49so we'll use conditional statements and
  738. 24:52we'll say id is equal to 1
  739. 24:53and 1 is equal to 1. now we said id is
  740. 24:56equal to 1 evaluates to true because
  741. 24:58this is a valid id in the table
  742. 25:00and one is equal to one one will always
  743. 25:02be equal to one
  744. 25:03and so this will always evaluate to true
  745. 25:06true and true
  746. 25:07evaluates to true and so the where
  747. 25:08clause evaluates to true and so the
  748. 25:10title
  749. 25:12of the products that are associated to
  750. 25:13id one
  751. 25:15will be outputted on the page and so
  752. 25:18right now
  753. 25:19i have two different responses depending
  754. 25:21on whether i force a false statement
  755. 25:23versus a true statement and so i'm able
  756. 25:25to tell when the application is telling
  757. 25:27me yes this is a true statement versus
  758. 25:29no this is not a true statement
  759. 25:32and so now you might be wondering well
  760. 25:34it's nice that i can play around with
  761. 25:35the application and get it to output
  762. 25:37true or false but how does that
  763. 25:39actually matter and how does that help
  764. 25:41me in
  765. 25:42exploiting a sql injection vulnerability
  766. 25:44to get
  767. 25:45sensitive information from the database
  768. 25:48to answer that let's take another use
  769. 25:51case so imagine the same application
  770. 25:53the application has a login
  771. 25:55functionality and so it has a users
  772. 25:56table in the database
  773. 25:57and the users table has the usernames
  774. 26:00of the users of the application so in
  775. 26:02this case administrator
  776. 26:04and the hashed passwords of the users in
  777. 26:07the application
  778. 26:09and so i want to extract this hash
  779. 26:11password for the administrator
  780. 26:13but i can't simply output it on the page
  781. 26:16like i would for union-based sql
  782. 26:18injection
  783. 26:20and so i have to use my boolean based
  784. 26:22sql injection um
  785. 26:24in order to output it and that's only
  786. 26:26done using by
  787. 26:27asking the application true and false
  788. 26:29questions and so
  789. 26:30the way i'm going to do that is by using
  790. 26:33this payload over here which generates
  791. 26:35this query over here
  792. 26:37so you've got i'll use the substring
  793. 26:40function and what that function does it
  794. 26:42extracts
  795. 26:44some characters from a string so the
  796. 26:46first method the first parameter
  797. 26:48that it takes is the string that you
  798. 26:50want to extract from
  799. 26:52the second parameter that it takes is
  800. 26:53the start position
  801. 26:55or the extraction and the third
  802. 26:57parameter that it takes
  803. 26:59is the number of characters to extract
  804. 27:02so in this case
  805. 27:03i'm saying that string to extract from
  806. 27:07is the output of this query so select
  807. 27:09the password from the users table where
  808. 27:11username is equal to
  809. 27:12administrator this query will output
  810. 27:14this hashed
  811. 27:15string over here and then i'm asking it
  812. 27:18extract the first character in the
  813. 27:21string and
  814. 27:22only one character and so it will um
  815. 27:25extract the first character over here of
  816. 27:28the hashed password and then i'm asking
  817. 27:30it is this first character
  818. 27:31equal to s in this case the first
  819. 27:34character is not equal to
  820. 27:36s because the first character is e and
  821. 27:39so
  822. 27:39this this statement over here evaluates
  823. 27:43to false
  824. 27:44true and false evaluates to false and so
  825. 27:48the where clause evaluates to false and
  826. 27:50you shouldn't see a title
  827. 27:52from the products page and so nothing is
  828. 27:55returned on the page
  829. 27:57that means it returned false that means
  830. 28:00i know that s
  831. 28:01is not a first character in the hashed
  832. 28:02password
  833. 28:04now imagine i keep looping through the
  834. 28:07characters and i get to e
  835. 28:10and i ask it again is the first
  836. 28:13character of the hashed password equal
  837. 28:16to e
  838. 28:18that evaluates to true right so the
  839. 28:20backend query would be
  840. 28:22true and true so the entire where clause
  841. 28:24evaluates to true and so the title
  842. 28:27of the products that are associated to
  843. 28:29id1 are returned on the page
  844. 28:32and this gives me an indication that it
  845. 28:34returned true
  846. 28:36which again tells me that e is the first
  847. 28:39character of the hashed password
  848. 28:41and so right now you should get an
  849. 28:43indication of how you're going to
  850. 28:45extract information from the database
  851. 28:47and why i said it takes more skill set
  852. 28:49and more time than
  853. 28:50union-based injection and that's because
  854. 28:53you're
  855. 28:54asking the application true and false
  856. 28:56questions and you're going to have to do
  857. 28:57that for each
  858. 28:58character so you start off with the
  859. 28:59first character you loop through
  860. 29:02all the alphanumeric characters until
  861. 29:04you get a true result once you get a
  862. 29:06true result that means you know you
  863. 29:08discovered what the character is
  864. 29:09which is e over here and then you move
  865. 29:12on to the next one
  866. 29:13and then you loop through the
  867. 29:14alphanumeric characters until you get
  868. 29:16another true statement and then that
  869. 29:18means you discovered what this character
  870. 29:19is
  871. 29:20and then again you loop through all the
  872. 29:22alphanumeric characters
  873. 29:23until you discover what this character
  874. 29:25is and so on until
  875. 29:27you output the entire uh string of the
  876. 29:29hashed password and this could take
  877. 29:31thousands of requests
  878. 29:32obviously you can do it manually and so
  879. 29:35you have to automate
  880. 29:36the tasks and we'll do that in one of
  881. 29:38the videos where we
  882. 29:40use burp intruder to automate it we'll
  883. 29:42also write a python script
  884. 29:44that does it for you and it might seem
  885. 29:46like it's something that is very
  886. 29:47difficult but it's not
  887. 29:49um writing the script in python takes
  888. 29:51about five minutes five to ten minutes
  889. 29:53max
  890. 29:54and then outputting the um the string is
  891. 29:57going to take less than one minute
  892. 29:58putting the entire hashed uh password so
  893. 30:01that's boolean based
  894. 30:03uh blind sql injection next
  895. 30:06let's talk about time-based blind sql
  896. 30:09injection
  897. 30:11time-based sql injection is a
  898. 30:13blind-based sql injection technique
  899. 30:15that relies on the database pausing for
  900. 30:18a specified amount of time
  901. 30:20then returning the results indicating a
  902. 30:21successful sql
  903. 30:23query execution so just like
  904. 30:26uh just like we did for boolean based
  905. 30:28sql injection what we do for time-based
  906. 30:30sql injection
  907. 30:32is we inject a payload in the parameter
  908. 30:35that is vulnerable and what that payload
  909. 30:37does
  910. 30:37it pauses the application the response
  911. 30:40of the application for a certain period
  912. 30:41of time so in postgres sql you would use
  913. 30:44the pg
  914. 30:44sleep command and and put a certain
  915. 30:47number of seconds so if i use pg sleep
  916. 30:4910 that means
  917. 30:50i ask the application to sleep for 10
  918. 30:52seconds
  919. 30:53and so if the response time takes 10
  920. 30:55seconds to get back to me then i know
  921. 30:57it's vulnerable to
  922. 30:58time based sequel injection if it
  923. 31:00doesn't take 10 seconds
  924. 31:01so it takes the general one second to
  925. 31:03get back to me then it's not vulnerable
  926. 31:05to
  927. 31:05time-based sql injection and
  928. 31:09the example is the same for
  929. 31:10boolean-based sql injection the way you
  930. 31:11would extract
  931. 31:13information from the database is
  932. 31:16by asking the application true and false
  933. 31:18questions except the condition over here
  934. 31:20is not a response in the application
  935. 31:22the indication of whether it's true or
  936. 31:24false is going to be based on
  937. 31:26the response time of the application and
  938. 31:28so i'm going to say if the first
  939. 31:30character of the administrators hashed
  940. 31:32password is an a then wait for 10
  941. 31:34seconds
  942. 31:35and if the response time takes 10
  943. 31:37seconds to respond to me
  944. 31:39then the first letter is a if it doesn't
  945. 31:41take 10 seconds then the first letter
  946. 31:43is not a and again this can't be done
  947. 31:47manually
  948. 31:48you're going to have to script it and
  949. 31:50you can do that in python where you
  950. 31:52measure the response time
  951. 31:53and then depending on what the response
  952. 31:54time is you determine whether it's true
  953. 31:57or false okay
  954. 32:00last but not least uh let's talk about
  955. 32:03out-of-band
  956. 32:04sql injection out-of-band sql injection
  957. 32:09is a vulnerability that consists of
  958. 32:10triggering an
  959. 32:11out-of-band network connection to a
  960. 32:13system that you control
  961. 32:15so this technique is usually used when
  962. 32:17you can't apply any of the other
  963. 32:18techniques that we learned about so far
  964. 32:21and it's not a common technique because
  965. 32:23it does rely
  966. 32:24on certain features being enabled in the
  967. 32:26database for it to actually make that
  968. 32:29network connection now to exploit this
  969. 32:31vulnerability you could use a variety of
  970. 32:32protocols
  971. 32:34but you'll see that the most commonly
  972. 32:35used ones are dns
  973. 32:37and http and one that we will look at in
  974. 32:40the videos it's in the web security
  975. 32:42academy so in the upcoming videos we
  976. 32:44where we gain hands-on experience doing
  977. 32:46this is this example over here
  978. 32:49where you exploit the vulnerability by
  979. 32:52using the
  980. 32:52xp dir-3 stored procedure in ms
  981. 32:56sql to perform a lookup of
  982. 32:59this domain over here so this domain was
  983. 33:02generated using burp collaborator
  984. 33:04and so you own it um since this is the
  985. 33:06domain that you control
  986. 33:08you can confirm if there were any dns
  987. 33:11lookups
  988. 33:11if there was then this is vulnerable to
  989. 33:13add a banned sql injection
  990. 33:15if it's not vulnerable then you won't
  991. 33:16see the dns lookup we'll see how to do
  992. 33:18this
  993. 33:19in the lab videos now once you've
  994. 33:21confirmed that you have an out-of-band
  995. 33:22sql injection
  996. 33:24in some cases you might be able to
  997. 33:25directly exfiltrate data
  998. 33:27whereas in other cases you might have to
  999. 33:30use conditional statements like we did
  1000. 33:32with
  1001. 33:32boolean and time-based sql injection
  1002. 33:35where if you do get a dns lookup then
  1003. 33:37yes
  1004. 33:38this is a true scenario if you don't get
  1005. 33:40a dns lookup then no it's not a true
  1006. 33:42scenario
  1007. 33:45all right in the past couple of slides
  1008. 33:47we discussed the different types of sql
  1009. 33:49injection vulnerabilities
  1010. 33:51now in this section we'll discuss how to
  1011. 33:53find sql injection vulnerabilities
  1012. 33:55so imagine you've been given an
  1013. 33:57application and asked to test it how
  1014. 33:59would you go about testing the
  1015. 34:00application
  1016. 34:01to see if it's vulnerable to any sql
  1017. 34:04injection vulnerabilities
  1018. 34:08before we cover that it's worth
  1019. 34:11mentioning that the methodology used for
  1020. 34:13finding sql injection vulnerabilities
  1021. 34:15differs from one person to another
  1022. 34:17person
  1023. 34:18and it's usually developed by experience
  1024. 34:21so just because i give you my
  1025. 34:23methodology doesn't mean that you have
  1026. 34:25to follow it to the letter
  1027. 34:26instead i would recommend that you take
  1028. 34:28what is useful for you from it
  1029. 34:30and then build and add on to your own
  1030. 34:32methodology as you gain more and more
  1031. 34:34experience
  1032. 34:35in pen testing web applications and
  1033. 34:38that statement applies for finding all
  1034. 34:41the vulnerabilities not just
  1035. 34:43sql injection vulnerabilities okay
  1036. 34:46so finding sql injection vulnerabilities
  1037. 34:48i've decided to split
  1038. 34:50uh this into two categories depending on
  1039. 34:53the perspective
  1040. 34:54of uh testing and the two categories
  1041. 34:57are black box testing and white box
  1042. 35:00testing for those of you that have
  1043. 35:02never heard of these terms before
  1044. 35:03blackbox web application patent testing
  1045. 35:06is when the tester is given little to no
  1046. 35:08information about the system
  1047. 35:10usually the only information that the
  1048. 35:12tester has access to
  1049. 35:14is the url of the application and the
  1050. 35:16scope of the engagement so this is
  1051. 35:18uh similar to mimicking an external
  1052. 35:21attacker that
  1053. 35:22is trying to attack your system the
  1054. 35:24attacker usually doesn't know anything
  1055. 35:25about the application
  1056. 35:27other than the main url of the
  1057. 35:28application
  1058. 35:30now for whitebox web application pen
  1059. 35:32testing it's the complete opposite the
  1060. 35:34tester would be given complete access to
  1061. 35:36the system including access to the
  1062. 35:38source code
  1063. 35:39of the application now there's a third
  1064. 35:41category that i haven't
  1065. 35:43included on the slide and it's called
  1066. 35:45grey box
  1067. 35:46web application pen testing this is a
  1068. 35:48combination of white box and black box
  1069. 35:50pen testing where the tester is given
  1070. 35:52limited information
  1071. 35:53and access to the system for example
  1072. 35:56instead
  1073. 35:57of just giving the tester a url of the
  1074. 36:00application like you would with blackbox
  1075. 36:02pen testing the tester is also given
  1076. 36:04accounts to the application
  1077. 36:06so when it comes to my methodology of
  1078. 36:08finding sql injection vulnerabilities i
  1079. 36:10loop both grey box and black box pen
  1080. 36:12testing into one category and that's
  1081. 36:14because my methodology is the same
  1082. 36:16regardless of the category the way i see
  1083. 36:18it
  1084. 36:18is if i'm approaching it from a black
  1085. 36:20box perspective
  1086. 36:21my scope will be much more limited than
  1087. 36:23the gray box perspective unless i find
  1088. 36:26authentication bypass vulnerability
  1089. 36:28nevertheless the methodology is still
  1090. 36:30the same for
  1091. 36:31finding sql injection vulnerabilities
  1092. 36:32whether i'm doing it
  1093. 36:34in a public or an authenticated page
  1094. 36:37and that's why they're looped in
  1095. 36:39together now when it comes to white box
  1096. 36:41pen testing that's a whole different
  1097. 36:43methodology because you actually gain
  1098. 36:44access to the source code of the
  1099. 36:46application and reviewing source code is
  1100. 36:48a whole different realm
  1101. 36:50than approaching an application from the
  1102. 36:52outside
  1103. 36:55all right let's start off by talking
  1104. 36:57about how to test applications from a
  1105. 36:59black box perspective the first thing
  1106. 37:01that i do when testing an application
  1107. 37:03is map the application and what that
  1108. 37:05means is i literally visit the url of
  1109. 37:07the application
  1110. 37:08i walk through all the pages that are
  1111. 37:10accessible to me within the user context
  1112. 37:12that i'm running as
  1113. 37:13i make note of all the input vectors
  1114. 37:15that potentially talk to the back end
  1115. 37:17i understand how the application
  1116. 37:19functions i try to figure out
  1117. 37:21the logic of the application i try to
  1118. 37:24find subdomains in the application
  1119. 37:26i enumerate directories and
  1120. 37:29pages that might not be directly visible
  1121. 37:32to me through the application
  1122. 37:34and so on and while i do all of that i
  1123. 37:37have my
  1124. 37:38proxy listening silently and
  1125. 37:40intercepting all the requests that i'm
  1126. 37:42making to the application
  1127. 37:43so mapping the application is such an
  1128. 37:46underrated step when it comes to pen
  1129. 37:48testing applications
  1130. 37:49although i would argue it's probably the
  1131. 37:51most important
  1132. 37:52step and the reason i say that is
  1133. 37:55because i usually see beginners in this
  1134. 37:56field
  1135. 37:57get way too excited about the fact that
  1136. 38:00they're testing an
  1137. 38:00application and then the first thing
  1138. 38:02that they do is start throwing
  1139. 38:03potentially malicious input vectors
  1140. 38:06into the application to see if the
  1141. 38:08application responds in an unusual way
  1142. 38:10and although this is a valid way of
  1143. 38:12testing an application
  1144. 38:13it doesn't make you any different from
  1145. 38:15an automated tool
  1146. 38:16so this is exactly what web application
  1147. 38:18vulnerability scanners do they crawl the
  1148. 38:20application
  1149. 38:21they locate all the input vectors and
  1150. 38:23then they throw a ton of malicious
  1151. 38:24payloads at these input vectors
  1152. 38:26and depending on how the application
  1153. 38:28responds the scanner logs the behavior
  1154. 38:30as a potential vulnerability
  1155. 38:32and this way of testing usually only uh
  1156. 38:35discovers the
  1157. 38:36low hanging vulnerabilities so if you're
  1158. 38:39doing exactly what a scanner can already
  1159. 38:41do then your testing is really not
  1160. 38:43adding any value
  1161. 38:44instead of doing that what i would
  1162. 38:46recommend is you leave that up to the
  1163. 38:48scanners
  1164. 38:49and instead spend quite a bit of time
  1165. 38:53understanding how the application works
  1166. 38:55because most of the critical
  1167. 38:56vulnerabilities found in applications
  1168. 38:58are logic flaws that a scanner is not
  1169. 39:01capable of finding on its own
  1170. 39:02or um the vulnerabilities are embedded
  1171. 39:05in pages that the scanner can't crawl so
  1172. 39:09technologies that the scanner can crawl
  1173. 39:11okay so once you've mapped the
  1174. 39:14application
  1175. 39:15and you've listed all the input vectors
  1176. 39:17that are potentially included in queries
  1177. 39:20that are interacting with the sql
  1178. 39:21database then it's a matter of fuzzing
  1179. 39:23the application
  1180. 39:25and that's because we're looking for sql
  1181. 39:26injection vulnerabilities you'd be
  1182. 39:28fuzzing the application with sql
  1183. 39:30specific characters so what that means
  1184. 39:32is you add special characters in the
  1185. 39:34input vectors
  1186. 39:35and see if the application responds in
  1187. 39:38an unusual way
  1188. 39:39now depending on how the application
  1189. 39:42responds you start refining your query
  1190. 39:44until you achieve your end goal and
  1191. 39:46that's what makes you different from a
  1192. 39:47scanner
  1193. 39:48because a scanner just simply throws a
  1194. 39:50bunch of input
  1195. 39:51at the application you as a human being
  1196. 39:55is capable you're capable of
  1197. 39:57understanding the output
  1198. 39:59of the sql injection and so you're able
  1199. 40:02to refine your sql injection using
  1200. 40:04multiple requests
  1201. 40:05until you actually achieve your goal and
  1202. 40:07we'll see how to do that in the
  1203. 40:08exploitation section
  1204. 40:10of this video okay again fuzzing the
  1205. 40:13application means
  1206. 40:14adding special characters and input
  1207. 40:16vectors and seeing if the application
  1208. 40:17responds
  1209. 40:18in an unusual way now that can be done
  1210. 40:21in many ways
  1211. 40:21the first thing that i try to do is
  1212. 40:24submit sql specific characters such as
  1213. 40:26the quote or the double quote or the
  1214. 40:29hash or the
  1215. 40:30dashes that represent comments and see
  1216. 40:32if the application outputs an
  1217. 40:34error now errors are really important
  1218. 40:38and really useful because they give you
  1219. 40:40information about how the backend
  1220. 40:42operates so sometimes they give you the
  1221. 40:45database that you're using they give you
  1222. 40:47the version of the database we discussed
  1223. 40:48that sometimes they actually give you
  1224. 40:50the exact query
  1225. 40:52if they gave you the query then you hit
  1226. 40:53gold because this way you don't have to
  1227. 40:56develop your payload across multiple
  1228. 40:57requests instead you already know what
  1229. 40:59the backend query is and so you just
  1230. 41:01develop your payload based on that query
  1231. 41:04and so the first thing that i do is try
  1232. 41:06to output an error once i have an
  1233. 41:08error i try to build my query um
  1234. 41:11using multiple requests so i refine my
  1235. 41:13query based on the errors that i get and
  1236. 41:15i build my query now in certain
  1237. 41:16scenarios you don't actually get an
  1238. 41:18error like for a blind sql injection and
  1239. 41:21so you also have to submit boolean
  1240. 41:22conditions such as
  1241. 41:24you know and or one equal to one
  1242. 41:27and one is equal to two and look for
  1243. 41:30differences in the applications
  1244. 41:31responses
  1245. 41:32so if you can get the application to
  1246. 41:34respond differently when you force a
  1247. 41:36true use case versus when you force
  1248. 41:38a false use case that means it's
  1249. 41:39vulnerable to boolean
  1250. 41:41injection uh next i
  1251. 41:45i do the same thing as boolean injection
  1252. 41:47instead it would be
  1253. 41:48to trigger time delays in the
  1254. 41:51application and then see
  1255. 41:52if there's a difference in the time it
  1256. 41:55takes to respond
  1257. 41:56so if you input a function that causes a
  1258. 42:00time delay and you
  1259. 42:01actually observe that time delay that
  1260. 42:02means that the application is vulnerable
  1261. 42:04to
  1262. 42:05time-based sql injection last but not
  1263. 42:08least
  1264. 42:09i also submit out-of-band payloads
  1265. 42:12that are designed to trigger a network
  1266. 42:15interaction with a server that i control
  1267. 42:18and if i get that network interaction
  1268. 42:20whether it's a dns lookup or it's done
  1269. 42:22through http
  1270. 42:23that means that the application is
  1271. 42:26vulnerable to out a band sql injection
  1272. 42:29and if i don't get them that means it's
  1273. 42:31not vulnerable to add a band sql
  1274. 42:32injection
  1275. 42:34so that's a high level overview of my
  1276. 42:38methodology when it comes to
  1277. 42:39testing from a black box perspective
  1278. 42:42there's really
  1279. 42:43two steps first mapping the application
  1280. 42:45so
  1281. 42:46listing all the input vectors in the
  1282. 42:48application
  1283. 42:49and then fuzzing the input vectors to
  1284. 42:52see
  1285. 42:52if i can if i can find a sql injection
  1286. 42:55and again when it comes to fuzzing the
  1287. 42:57input vectors
  1288. 42:58you can refine your requests based on
  1289. 43:00the responses that the application
  1290. 43:03gives you okay next is
  1291. 43:06whitebox testing perspective so where
  1292. 43:08you're given the source code of the
  1293. 43:10application
  1294. 43:11in this case the first thing that i do
  1295. 43:13is i enable
  1296. 43:14web server logging and i enable database
  1297. 43:17logging enabling
  1298. 43:19logging at the web server level will
  1299. 43:21help because
  1300. 43:23when you're fuzzing the application like
  1301. 43:24we did with blackbox perspective
  1302. 43:26it will generate errors on all the
  1303. 43:29different
  1304. 43:30um invalid characters that you input
  1305. 43:33into the application and then that helps
  1306. 43:35you
  1307. 43:36to detect that a sql injection uh
  1308. 43:40exists and then also to refine your
  1309. 43:42payload
  1310. 43:43later on similarly for database logging
  1311. 43:45i enable that because when i
  1312. 43:47think that there's a sql injection
  1313. 43:49vulnerability and i enter a sql
  1314. 43:51injection payload in the parameter i'd
  1315. 43:53like to see how it was logged at the
  1316. 43:54back end
  1317. 43:55and then depending on how it was logged
  1318. 43:57at the backend i can see what characters
  1319. 43:59made it through and in what format they
  1320. 44:00made it through and then i could say
  1321. 44:02um with maybe a 90 accuracy that yes
  1322. 44:05this is vulnerable to sql injection
  1323. 44:07before
  1324. 44:08i even look at the source code of the
  1325. 44:10application
  1326. 44:13so once i have my logging enabled the
  1327. 44:15next thing that i do is map the
  1328. 44:17application just like i would with
  1329. 44:19a black box perspective so i'll start
  1330. 44:22with the applications
  1331. 44:24graphical user interface i'll map all
  1332. 44:26the visible functionality in the
  1333. 44:28application all the input vectors that
  1334. 44:29potentially talk to the backend
  1335. 44:32and i'll make a list on them next
  1336. 44:34because i do have access to the source
  1337. 44:36code i'll also do a resurrect search
  1338. 44:39on all instances in the code that talk
  1339. 44:41to the database and that really
  1340. 44:42that really depends on how the
  1341. 44:44application is programmed and then
  1342. 44:46which database it uses and so on
  1343. 44:50but i would have regex strings
  1344. 44:53that i look for in the code to see
  1345. 44:56if i've missed anything that i haven't
  1346. 44:58mapped from a black box perspective and
  1347. 45:00you'll see that there are certain pages
  1348. 45:03that
  1349. 45:03might not have been directly accessible
  1350. 45:05through the main page of the application
  1351. 45:07and you didn't discover them through
  1352. 45:09directory brute forcing and so it's
  1353. 45:11useful to actually look at the code and
  1354. 45:13see if there's anything that you missed
  1355. 45:15once i have a list of everything what i
  1356. 45:18would do
  1357. 45:18is i would literally fuzz the
  1358. 45:21application so
  1359. 45:22i would submit sql characters
  1360. 45:26that could potentially break the query i
  1361. 45:28will look at how
  1362. 45:30they were logged using database logging
  1363. 45:33that i enabled in the first step
  1364. 45:34and then uh based on that i can usually
  1365. 45:37tell if it's vulnerable to
  1366. 45:38do sql code injection and if i feel like
  1367. 45:41it's vulnerable to sql code injection
  1368. 45:43then i actually do a code review
  1369. 45:45of that functionality so for example if
  1370. 45:48i think the login page is vulnerable to
  1371. 45:49sql injection what i would do
  1372. 45:51is i will look for the login
  1373. 45:53functionality so the function that
  1374. 45:55implements that in the code and then i
  1375. 45:57would do a walkthrough of the entire
  1376. 45:59functionality
  1377. 46:00from beginning to end until until i have
  1378. 46:03confirmed that it is
  1379. 46:04vulnerable to sql injection in theory
  1380. 46:06and then to confirm in practice that it
  1381. 46:08is vulnerable to sql injection
  1382. 46:10then i would test it out and depending
  1383. 46:13on the response i get from the
  1384. 46:14application that means
  1385. 46:16it's vulnerable to sql injection
  1386. 46:19so again that's my methodology doesn't
  1387. 46:21mean that it's a methodology that you
  1388. 46:22should follow
  1389. 46:23i do know people that would
  1390. 46:25automatically just go into the source
  1391. 46:27code and then do a search for
  1392. 46:29insecure functions when it comes to
  1393. 46:32parameterized queries and so on and so
  1394. 46:36if they find an insecure function
  1395. 46:37they'll start with that instead of
  1396. 46:39doing kind of a hybrid approach of
  1397. 46:42both black box and white box which is
  1398. 46:45the way that i the methodology that i
  1399. 46:47described
  1400. 46:50okay in the past two sections we talked
  1401. 46:53about the different types of sql
  1402. 46:55injection vulnerabilities
  1403. 46:56and how to test an application to see if
  1404. 46:59it's vulnerable to sql injection
  1405. 47:01now in this section we'll talk about how
  1406. 47:03to exploit sql injection vulnerabilities
  1407. 47:06in order to achieve your end goal and uh
  1408. 47:09that really depends on the sql injection
  1409. 47:11vulnerability that you're trying to
  1410. 47:13exploit
  1411. 47:13uh we so far talked about four different
  1412. 47:15uh sorry actually five different types
  1413. 47:17error-based sql injection union-based
  1414. 47:19sql injection
  1415. 47:20boolean based blind sql injection based
  1416. 47:23blind sql injection and out of band sql
  1417. 47:26injection so in this section we'll talk
  1418. 47:28about how to exploit
  1419. 47:28each one of these vulnerabilities
  1420. 47:32and we'll start off with error-based sql
  1421. 47:34injection
  1422. 47:36now as a reminder we said error-based
  1423. 47:38sql injection is an
  1424. 47:39in-band sql injection technique that
  1425. 47:41forces the database to generate an error
  1426. 47:44giving the attacker information upon
  1427. 47:46which to refine
  1428. 47:47a query so a successful exploitation of
  1429. 47:51an error-based sql injection
  1430. 47:52is the fact that you can get the
  1431. 47:54application to output an error so
  1432. 47:56there's really not much to explaining
  1433. 47:58this vulnerability all you would do is
  1434. 48:00submit sql specific characters
  1435. 48:02again such as the single quote or the
  1436. 48:04double quote and you
  1437. 48:06look for errors in the application
  1438. 48:09now different characters can give you
  1439. 48:11different errors so it's good to fuzz
  1440. 48:13the application with the commonly known
  1441. 48:15characters
  1442. 48:16to generate errors next
  1443. 48:19is a union-based sql injection
  1444. 48:22exploiting this vulnerability is a
  1445. 48:23little bit more involved
  1446. 48:24as a reminder we said union-based sql
  1447. 48:27injection is an in-band sql injection
  1448. 48:30technique
  1449. 48:31that leverages the union sql operator
  1450. 48:34to combine the results of two queries
  1451. 48:36into a single result set
  1452. 48:38now in order for you to use the union
  1453. 48:40operator you have to follow
  1454. 48:42the rules of the union operator and
  1455. 48:43there's two basic rules the first one is
  1456. 48:46that the number
  1457. 48:46and the order of the columns must be the
  1458. 48:48same in all queries
  1459. 48:50and the second one is that the data
  1460. 48:52types must be
  1461. 48:53compatible so the query that the
  1462. 48:54application is making
  1463. 48:56and the query that you want to make have
  1464. 48:58to satisfy these two rules
  1465. 49:00and so when you're exploiting union
  1466. 49:02based sql injection the first thing that
  1467. 49:04you need to do is figure out the number
  1468. 49:05of columns that the query is making
  1469. 49:08we'll talk about that in a bit and we've
  1470. 49:09got two ways of doing that
  1471. 49:11next you figure out the data types of
  1472. 49:12the columns
  1473. 49:14we're mainly interested with string data
  1474. 49:16because we want to extract things like
  1475. 49:18usernames and passwords and so on and so
  1476. 49:20that's usually saved in string format
  1477. 49:22and so we need to find a data type that
  1478. 49:25outputs
  1479. 49:26uh string data and this way we could
  1480. 49:28output the data that we want to extract
  1481. 49:31and then once you've figured out the
  1482. 49:32number of columns that the query is
  1483. 49:33making and the data types of the columns
  1484. 49:35then you could use the union operator to
  1485. 49:38output information from the database
  1486. 49:40sometimes
  1487. 49:41you don't actually get the data type
  1488. 49:43that you want and so it's a dead end
  1489. 49:46you can only extract for example
  1490. 49:48integers or so on so
  1491. 49:49it depends on the sql injection and the
  1492. 49:52context that you're running with on
  1493. 49:53whether you could actually exploit it to
  1494. 49:55extract information from the backend
  1495. 49:57database
  1496. 49:58okay so i said there's two different uh
  1497. 50:00ways
  1498. 50:01of determining the number of columns
  1499. 50:03that that are required in a sql
  1500. 50:05injection union attack and one of the
  1501. 50:07ways is using the
  1502. 50:08order by clause
  1503. 50:11now this clause orders uh the result set
  1504. 50:14of a query by a specified column list
  1505. 50:17so let's take an example if you've got
  1506. 50:19select the title
  1507. 50:20and the cost from the product table
  1508. 50:23where id is equal to one
  1509. 50:25and then you say order by 1 what that
  1510. 50:27means
  1511. 50:28is that you're ordering by the first
  1512. 50:31column which is by the title column over
  1513. 50:33here
  1514. 50:35now the way to figure out the number of
  1515. 50:37columns
  1516. 50:38is to incrementally inject a series of
  1517. 50:41order by clauses
  1518. 50:42until you get an error or observe a
  1519. 50:45different behavior in the application
  1520. 50:48so if i say order by one
  1521. 50:51it'll order by the first column if i say
  1522. 50:54order by two it'll order by the second
  1523. 50:56column
  1524. 50:56now if i say order by three that column
  1525. 50:59doesn't exist
  1526. 51:00and so i get an error saying the order
  1527. 51:02by position number three
  1528. 51:04is out of range of the number of items
  1529. 51:06in the select list
  1530. 51:08and so this way i know that 3 is not a
  1531. 51:11column and since
  1532. 51:12i incrementally increase the value of
  1533. 51:15the order by
  1534. 51:16integer over here i know then the number
  1535. 51:19of columns is this value minus 1 so 3
  1536. 51:21minus 1 which is
  1537. 51:232. so i know that number of columns that
  1538. 51:25are being used in the sql query
  1539. 51:27is 2. so that's the way to do it using
  1540. 51:31the order by clause you could also
  1541. 51:33figure out the number of columns
  1542. 51:35required in a sql injection
  1543. 51:37using null values
  1544. 51:40so again let's take example of a query
  1545. 51:43that does select the title
  1546. 51:45and the cost from the product table
  1547. 51:48where id is equal to one
  1548. 51:50and then you say union select null
  1549. 51:53and then dash dash for a comment
  1550. 51:55character
  1551. 51:57now because a union using the union
  1552. 52:00operator requires that
  1553. 52:02this query has the same number of
  1554. 52:03columns as this query
  1555. 52:05if you don't use the same number of
  1556. 52:06columns you will get an error
  1557. 52:08so if you incrementally inject a series
  1558. 52:11of union select payloads specifying a
  1559. 52:13different number of null values
  1560. 52:14until you no longer get an error then
  1561. 52:16you could figure out the number of
  1562. 52:18columns that are required by a sql
  1563. 52:20injection so
  1564. 52:22for example this when i put this payload
  1565. 52:25over here which is the one over here
  1566. 52:27it'll give me an error saying something
  1567. 52:29like all queries combined using a union
  1568. 52:32intersect or except operator
  1569. 52:35must have an equal number of expressions
  1570. 52:37in their target list
  1571. 52:38so it's telling me you're using the
  1572. 52:40union operator but your two queries
  1573. 52:42don't have the same number of columns
  1574. 52:43and so i know that the query does not
  1575. 52:45have
  1576. 52:46only one column so i increase the number
  1577. 52:49of null values again by one so now i
  1578. 52:51have two null values
  1579. 52:52and i try it out and i see if it gives
  1580. 52:55me an error that means
  1581. 52:56it has more than two columns and i try
  1582. 52:58three and so on until i no longer get an
  1583. 53:00error
  1584. 53:00now in this case we only have two
  1585. 53:02columns and so this will
  1586. 53:04um this will output uh the results
  1587. 53:08of this query over here and then the
  1588. 53:10null values over here so this won't give
  1589. 53:12me an error
  1590. 53:13and that means that the number of
  1591. 53:15columns that i have
  1592. 53:17are is equal to the number of null
  1593. 53:20values that i entered which is 2.
  1594. 53:21so you could either do it by the order
  1595. 53:23by clause or
  1596. 53:25use by adding union select payloads
  1597. 53:27specifying
  1598. 53:28different numbers of null values
  1599. 53:32okay so that's how to find out
  1600. 53:35the number of columns that the query is
  1601. 53:38using next we need to find a useful data
  1602. 53:41type
  1603. 53:42in these columns and so the way to do
  1604. 53:45that
  1605. 53:45is to probe each column to test whether
  1606. 53:48it can hold string data
  1607. 53:50by submitting a series of union select
  1608. 53:52payloads that place a string value into
  1609. 53:54each column in turn so
  1610. 53:57over here what i would do now i know
  1611. 53:59that it has two columns right
  1612. 54:01from the previous exercise that we just
  1613. 54:03did so what i would do
  1614. 54:05is i would um add a string value
  1615. 54:08so a in the first in the first column
  1616. 54:10value and see if it outputs an
  1617. 54:12error if it outputs an error saying that
  1618. 54:15i don't know it's incompatible with a
  1619. 54:16certain type so in this case we're
  1620. 54:18saying that the first
  1621. 54:19uh the first value is actually integer
  1622. 54:22and so i get an error saying that it's
  1623. 54:24incompatible which means that the first
  1624. 54:26column does not allow you to
  1625. 54:28is not a string data type so
  1626. 54:31next i try the second column over here
  1627. 54:34so i keep this as null and i try the
  1628. 54:36second column and if i get an error that
  1629. 54:38means this
  1630. 54:38also is not does not accept string
  1631. 54:41values and i keep doing that
  1632. 54:43until i find a value that allows me to
  1633. 54:46enter a string data and again in some
  1634. 54:48cases you might not actually find a
  1635. 54:50value and that's why some sql injections
  1636. 54:52allow you to do things like extract data
  1637. 54:54from a database whereas others you're
  1638. 54:56very limited in what you're
  1639. 54:58like the data type that you're allowed
  1640. 54:59to extract
  1641. 55:01okay and so going back to the slide um
  1642. 55:04explaining union based sql injection we
  1643. 55:06talked about how to figure out the
  1644. 55:08number of columns
  1645. 55:09that the query is making we also talked
  1646. 55:11about how to figure out the data types
  1647. 55:12of the columns
  1648. 55:13if you have um columns that are being
  1649. 55:16outputted on the page
  1650. 55:17and they use string data then you could
  1651. 55:20use
  1652. 55:21the union operator to output information
  1653. 55:23from the database
  1654. 55:25and that's how you exploit union based
  1655. 55:27sql injection
  1656. 55:29next let's talk about exploiting boolean
  1657. 55:31based blind sql injection
  1658. 55:33so uh the first thing to do is submit a
  1659. 55:36boolean condition that evaluates to
  1660. 55:38false
  1661. 55:39and note the response i've got an error
  1662. 55:41here it should say
  1663. 55:42note the next thing to do is submit a
  1664. 55:45boolean condition that evaluates to true
  1665. 55:48and again note the response if the
  1666. 55:51response
  1667. 55:51is different or when you force the false
  1668. 55:54statement versus a true statement then
  1669. 55:56you have a boolean baseline sql
  1670. 55:58injection and the way to exploit that we
  1671. 56:00already talked about that
  1672. 56:01is to write a program that uses
  1673. 56:02conditional statements to ask the
  1674. 56:04database a series of true and false
  1675. 56:06questions
  1676. 56:06and then monitor the responses if the
  1677. 56:09response
  1678. 56:09is the same as the response you got when
  1679. 56:12it evaluated to true that means
  1680. 56:14uh the question that you're asking the
  1681. 56:16application is true
  1682. 56:17if the response is the same as the
  1683. 56:19response that you got when you
  1684. 56:20force the false statements that means
  1685. 56:22the question that you're asking the
  1686. 56:24application
  1687. 56:25is false and that's how you extract data
  1688. 56:27from the database when it comes to
  1689. 56:29boolean-based
  1690. 56:30blind sql injection next
  1691. 56:33is time-based blind sql injection
  1692. 56:36what you do is you submit a payload that
  1693. 56:38pauses the application for a specified
  1694. 56:40period of time
  1695. 56:41if the application does actually pause
  1696. 56:44for that specified period of time that
  1697. 56:46means it's vulnerable to time-based sql
  1698. 56:48injection
  1699. 56:48and then it's a matter of doing the same
  1700. 56:50thing that you did for boolean based sql
  1701. 56:52injection you
  1702. 56:53just write a program that uses
  1703. 56:55conditional statements to ask the
  1704. 56:57a series of true and false questions and
  1705. 56:59you monitor the response time
  1706. 57:00if there's a delay in the response time
  1707. 57:02that means the question you're asking
  1708. 57:04the application is true
  1709. 57:05if there's no delay in the response time
  1710. 57:07that means the question you're asking
  1711. 57:08the application is false and then you
  1712. 57:10send a bunch of those requests until you
  1713. 57:12enumerate
  1714. 57:13or you extract the thing that you want
  1715. 57:14to extract from the database
  1716. 57:17and we'll gain experience in all of
  1717. 57:18these different types of
  1718. 57:20sql injection vulnerabilities like
  1719. 57:22hands-on experience in the next upcoming
  1720. 57:24videos
  1721. 57:26okay so explaining how to ban sql
  1722. 57:29injection
  1723. 57:29um again a reminder this is a
  1724. 57:31vulnerability that consists of
  1725. 57:33triggering
  1726. 57:33an out-of-band network connection to a
  1727. 57:35system that you control
  1728. 57:37and the way to exploit it is to submit
  1729. 57:40out-of-band payloads designed to trigger
  1730. 57:42an out-of-band network interaction when
  1731. 57:44executed within
  1732. 57:45a sql query and monitor for any
  1733. 57:48resulting
  1734. 57:48uh interactions so this is a database
  1735. 57:52specific
  1736. 57:52obviously so knowing the type of
  1737. 57:54database that you're using
  1738. 57:56will be very useful in in this scenario
  1739. 58:00and it depends on whether the database
  1740. 58:01has these functions enabled
  1741. 58:03and so if you do get that network
  1742. 58:06connection if you receive that network
  1743. 58:07connection
  1744. 58:08again whether it's a dns lookup or
  1745. 58:09something else that means that
  1746. 58:11the application is vulnerable to add a
  1747. 58:13band sql injection
  1748. 58:15and in certain scenarios you might be
  1749. 58:16able to immediately expel
  1750. 58:19data from the application so directly
  1751. 58:22excel data from the application whereas
  1752. 58:24in other cases you're again stuck asking
  1753. 58:26true and false questions just like you
  1754. 58:27would for
  1755. 58:29line based sql injection
  1756. 58:32okay i didn't want to end this section
  1757. 58:34without talking about
  1758. 58:35automated exploitation tools so one of
  1759. 58:38the famous ones and it's open source
  1760. 58:40is a sql map it's a tool that is used to
  1761. 58:43find sql injection vulnerabilities
  1762. 58:46it's very very powerful and it's very
  1763. 58:49customizable
  1764. 58:50in the sense that you select what
  1765. 58:52parameters you could
  1766. 58:53inject you also select the figurosity of
  1767. 58:56your testing you also select
  1768. 58:58the angle of your testing so if it does
  1769. 59:00find a sql injection vulnerability
  1770. 59:02sometimes you have the ability to say
  1771. 59:03try to
  1772. 59:04get a shell on the system or try to dump
  1773. 59:07the usernames and passwords of the
  1774. 59:09applications on the system and so on and
  1775. 59:11so this is a very important tool to look
  1776. 59:13into when it comes to finding sql
  1777. 59:15injection vulnerabilities
  1778. 59:16other tools that you could use which are
  1779. 59:18more for detection versus
  1780. 59:20uh actually exploiting it to to
  1781. 59:23achieve your end goal our web
  1782. 59:25application vulnerability scanners
  1783. 59:28we talked quite a bit about these in
  1784. 59:30this video
  1785. 59:31there are scanners that crawl the
  1786. 59:33application and look for vulnerabilities
  1787. 59:35so not just sql injection
  1788. 59:37vulnerabilities they look for all types
  1789. 59:38of vulnerabilities
  1790. 59:39in an application um and they usually
  1791. 59:41report it as a vulnerability they don't
  1792. 59:43actually go
  1793. 59:44all the way to explaining it to get a
  1794. 59:46shell on the system or to dump
  1795. 59:47usernames and passwords and some of
  1796. 59:50these tools
  1797. 59:51are open source whereas others are
  1798. 59:54paid so something to look into of course
  1799. 59:57these tools have limitations
  1800. 1:00:00and uh maybe i'll do a video specific on
  1801. 1:00:03that because that's what my research was
  1802. 1:00:05on but these tools have
  1803. 1:00:07limitations and they operate from a
  1804. 1:00:08black box uh perspective so there are
  1805. 1:00:11other tools that operate from a white
  1806. 1:00:12box perspective
  1807. 1:00:13they're called static analysis tools
  1808. 1:00:15whereas these are called dynamic
  1809. 1:00:17analysis tools so something to look into
  1810. 1:00:19when you want to automate a portion of
  1811. 1:00:22your
  1812. 1:00:23web application testing
  1813. 1:00:26all right now that you know what a sql
  1814. 1:00:28injection vulnerability is
  1815. 1:00:30how to find it and how to exploit it the
  1816. 1:00:32next thing to learn is how to prevent
  1817. 1:00:34sql injection vulnerabilities
  1818. 1:00:36and that will come in really handy when
  1819. 1:00:38you're writing pen testing reports and
  1820. 1:00:40you need to provide recommendations to
  1821. 1:00:41your client on how to remediate the
  1822. 1:00:43vulnerabilities that you discovered
  1823. 1:00:46so there are the primary defenses
  1824. 1:00:50and the additional defenses which use
  1825. 1:00:53the concept
  1826. 1:00:54of defense in depth we'll talk about
  1827. 1:00:57each defense in the next couple of
  1828. 1:00:59slides
  1829. 1:01:00and those are the ones that are
  1830. 1:01:01mentioned in the web application hackers
  1831. 1:01:04handbook
  1832. 1:01:05but the idea is i have this highlighted
  1833. 1:01:07over here because this is the correct
  1834. 1:01:10way of preventing sql injection
  1835. 1:01:13vulnerabilities and that is by using
  1836. 1:01:15prepared statements
  1837. 1:01:16or parameterized queries instead of
  1838. 1:01:19string concatenation within a query all
  1839. 1:01:21the other options are partial options
  1840. 1:01:24and should be used only as a last resort
  1841. 1:01:26with the knowledge
  1842. 1:01:27that they might be bypassed or they
  1843. 1:01:29might be implemented incorrectly and
  1844. 1:01:31they might cause a sql injection in the
  1845. 1:01:34in the future
  1846. 1:01:36and so let's talk about each one in
  1847. 1:01:38order starting with the correct
  1848. 1:01:40way of defending against sql injection
  1849. 1:01:43vulnerabilities and that's
  1850. 1:01:45using prepared statements
  1851. 1:01:48okay so let's take an example first so
  1852. 1:01:51you've got
  1853. 1:01:52a code block over here the first one
  1854. 1:01:54which is query over here
  1855. 1:01:56as a select the account balance from the
  1856. 1:01:59user data table
  1857. 1:02:00where username is equal to
  1858. 1:02:03request.getparameter customername
  1859. 1:02:05so what this does is it takes user input
  1860. 1:02:08so user controlled input customer name
  1861. 1:02:10and it
  1862. 1:02:11adds it directly to the query and then
  1863. 1:02:15it defines the structure so it's
  1864. 1:02:17defining the structure of the query
  1865. 1:02:19and then it executes the query do you
  1866. 1:02:22spot the issue over here
  1867. 1:02:23it's a bit obvious the user supplied
  1868. 1:02:26input customer name over here is
  1869. 1:02:28embedded directly into the sql
  1870. 1:02:31statement and so anything that the user
  1871. 1:02:34adds over here might actually be
  1872. 1:02:37interpreted as
  1873. 1:02:38a part of the query
  1874. 1:02:43okay so using uh prepared statements can
  1875. 1:02:46prevent
  1876. 1:02:48can prevent sql injection attacks and
  1877. 1:02:49that's because the construction
  1878. 1:02:51of the sql statement is performed in two
  1879. 1:02:53steps the first being the
  1880. 1:02:55application specifies the query
  1881. 1:02:57structure with placeholders for each
  1882. 1:02:59user
  1883. 1:03:00supplied input and then the application
  1884. 1:03:03specifies the content of each
  1885. 1:03:05placeholder this way there's no way that
  1886. 1:03:07a user-supplied input that is specified
  1887. 1:03:10in this step over here can ever
  1888. 1:03:12interfere with the structure of the
  1889. 1:03:14query
  1890. 1:03:14in this first step and that's because
  1891. 1:03:16the query structure had already been
  1892. 1:03:18defined
  1893. 1:03:18and now any placeholder data is
  1894. 1:03:21interpreted in a safe way
  1895. 1:03:23as literally data versus sql code
  1896. 1:03:27and so let's take an example of code
  1897. 1:03:29that is not vulnerable
  1898. 1:03:32so in this code over here you could see
  1899. 1:03:35there is uh
  1900. 1:03:36the variable uh cust name it takes and
  1901. 1:03:39user supplied input customer name
  1902. 1:03:43you've got the query over here being
  1903. 1:03:46defined
  1904. 1:03:48and then over here it prepares the
  1905. 1:03:50statement through a database connection
  1906. 1:03:52so the prepare statement method is
  1907. 1:03:54invoked
  1908. 1:03:55to interpret this query and fix the
  1909. 1:03:57structure of the query and then
  1910. 1:03:59the set string method is used to specify
  1911. 1:04:01the parameters actual value
  1912. 1:04:03now uh because the query structure is
  1913. 1:04:05already has already been fixed
  1914. 1:04:08in the prepared statement this value can
  1915. 1:04:11contain
  1916. 1:04:11any data without affecting the structure
  1917. 1:04:14of the query because it will literally
  1918. 1:04:15be interpreted as data versus as part of
  1919. 1:04:17the query so if the user over here had
  1920. 1:04:20put in sql characters like a quote or a
  1921. 1:04:22double quote or so on
  1922. 1:04:24that won't be interpreted as data and it
  1923. 1:04:26can't ever change the structure of the
  1924. 1:04:28query because the structure has already
  1925. 1:04:29been
  1926. 1:04:30determined and fixed and that's how you
  1927. 1:04:33prevent
  1928. 1:04:34sql injection vulnerabilities that's the
  1929. 1:04:36correct way of preventing sql injection
  1930. 1:04:38vulnerabilities
  1931. 1:04:40now there are partial options that the
  1932. 1:04:43book talks about
  1933. 1:04:44one is the use of stored procedures
  1934. 1:04:48so start procedures are batches of
  1935. 1:04:51statements
  1936. 1:04:52grouped as a logical unit and stored in
  1937. 1:04:54a database um
  1938. 1:04:56this is not a not always a safe way of
  1939. 1:04:59preventing sequel injection
  1940. 1:05:00uh vulnerabilities because if it's
  1941. 1:05:02implemented incorrectly it could still
  1942. 1:05:04be vulnerable to sql injections so it
  1943. 1:05:06still needs to be called in a
  1944. 1:05:07parameterized way
  1945. 1:05:08in order for it to be safe against sql
  1946. 1:05:11injection vulnerabilities
  1947. 1:05:12um and uh the reason that some people
  1948. 1:05:15would use
  1949. 1:05:16uh stored procedures or any of the
  1950. 1:05:17partial options is because
  1951. 1:05:19uh there are certain parameters that you
  1952. 1:05:22can't specify as
  1953. 1:05:23placeholders in parameterized queries
  1954. 1:05:26and for example the table name or the
  1955. 1:05:28column name
  1956. 1:05:29and so if that's user controllable you
  1957. 1:05:31have to use one of the partial options
  1958. 1:05:33in order to prevent sql injection
  1959. 1:05:35attacks now
  1960. 1:05:36the correct way of doing it is that this
  1961. 1:05:38should never be user controllable but if
  1962. 1:05:40it has to be user controllable then you
  1963. 1:05:42use one of the partial options
  1964. 1:05:44that's option two option number three is
  1965. 1:05:46whitelist input validation so define
  1966. 1:05:48which values are authorized
  1967. 1:05:50everything else is considered to be
  1968. 1:05:51unauthorized and we talked about
  1969. 1:05:53parameters that can be placeholders like
  1970. 1:05:55the table name and the column name
  1971. 1:05:56in this case a whitelist input
  1972. 1:05:58validation would be you know
  1973. 1:06:00exactly the column name should be or the
  1974. 1:06:03table name should be users and so you
  1975. 1:06:05only accept the value users and nothing
  1976. 1:06:07else
  1977. 1:06:07and this way you prevent the attacker
  1978. 1:06:10from injecting
  1979. 1:06:11other characters or other strings that
  1980. 1:06:14could potentially
  1981. 1:06:15interfere with your query option number
  1982. 1:06:19four
  1983. 1:06:19is escape all user input the validate
  1984. 1:06:22user input
  1985. 1:06:23and um that should only be used as a
  1986. 1:06:25last resort again it's a partial option
  1987. 1:06:27versus
  1988. 1:06:28something that for sure prevents sql
  1989. 1:06:30injection vulnerabilities
  1990. 1:06:33okay additional defenses uh before we
  1991. 1:06:36talk about that let's talk about
  1992. 1:06:38the concept of defense in depth
  1993. 1:06:41so the idea is to make it as difficult
  1994. 1:06:43as possible for the attacker to
  1995. 1:06:45compromise your organization
  1996. 1:06:46and that's done by providing additional
  1997. 1:06:48protection in the event
  1998. 1:06:50at the frontline defenses bill so
  1999. 1:06:53if me as an attacker i want to gain
  2000. 1:06:56access to your organization
  2001. 1:06:58and i managed to get past the first
  2002. 1:07:01block that you had put in my way
  2003. 1:07:03then i would have two other obstacles
  2004. 1:07:06that i need to get through before i get
  2005. 1:07:09to your organization
  2006. 1:07:10and so in the same sense although you
  2007. 1:07:12use parameterized queries
  2008. 1:07:14just in case you by chance forgot to
  2009. 1:07:18include it on one parameter um in one
  2010. 1:07:21page
  2011. 1:07:22you want to have defenses in place that
  2012. 1:07:24if a sql injection vulnerability was
  2013. 1:07:26exploited the attacker gets
  2014. 1:07:30gets another obstacle in their way
  2015. 1:07:32before they could achieve their goal and
  2016. 1:07:33so
  2017. 1:07:34one is least privilege so the
  2018. 1:07:36application should use the lowest
  2019. 1:07:38possible of privileges when accessing
  2020. 1:07:40the database
  2021. 1:07:41and the reason behind that is if a sql
  2022. 1:07:44injection exists in your application
  2023. 1:07:46and an attacker exploits it to gain
  2024. 1:07:49remote code execution
  2025. 1:07:51on the server that the database is
  2026. 1:07:53installed on
  2027. 1:07:54then the attacker will gain the same
  2028. 1:07:57level of privilege that the database is
  2029. 1:07:59running with and so
  2030. 1:08:00if the database is running a system or
  2031. 1:08:02route then the attacker already has
  2032. 1:08:04all the privileges that the attacker
  2033. 1:08:06needs on that server however if it was
  2034. 1:08:08running with a service account
  2035. 1:08:10that has all the privileges stripped
  2036. 1:08:11then the attacker has to find ways to
  2037. 1:08:14escalate privileges on the server before
  2038. 1:08:17they can for example move laterally
  2039. 1:08:19within the organization
  2040. 1:08:20and so the application should always use
  2041. 1:08:22the lowest possible
  2042. 1:08:24level of privilege when accessing the
  2043. 1:08:25database
  2044. 1:08:27any unnecessary default functionality in
  2045. 1:08:29the database should be removed or
  2046. 1:08:30disabled that includes things like
  2047. 1:08:35functions that allow you to execute
  2048. 1:08:37system commands from the database
  2049. 1:08:39also functions that allow you to make a
  2050. 1:08:41network connection that we talked about
  2051. 1:08:43out of band sql injection right
  2052. 1:08:45so functions that allowed that allow you
  2053. 1:08:47to do that
  2054. 1:08:48so any unnecessary default functionality
  2055. 1:08:50that you don't need
  2056. 1:08:51for the application to function that
  2057. 1:08:53should be removed because
  2058. 1:08:55it decreases the
  2059. 1:08:58amount of things that an attacker can
  2060. 1:08:59use in order to penetrate your
  2061. 1:09:01organization
  2062. 1:09:04ensure cis benchmarks for the database
  2063. 1:09:07in use is applied
  2064. 1:09:08so there are benchmarks for most of uh
  2065. 1:09:10the databases out there
  2066. 1:09:12and these are rules uh on the way to
  2067. 1:09:15configure
  2068. 1:09:16your database in order to ensure that
  2069. 1:09:19it's secure
  2070. 1:09:20and it includes things like the
  2071. 1:09:22application should use the lowest
  2072. 1:09:23possible level of privilege
  2073. 1:09:24and removing any default functionality
  2074. 1:09:26and so on
  2075. 1:09:28so look up your database version and you
  2076. 1:09:30should be able to find a cs benchmark
  2077. 1:09:33for it
  2078. 1:09:35and last but not least this one's
  2079. 1:09:36obvious all vendor issued security
  2080. 1:09:38patches should be applied in a timely
  2081. 1:09:40fashion
  2082. 1:09:40so if you get notified uh that the
  2083. 1:09:43database version that you're using
  2084. 1:09:45is vulnerable to some kind of
  2085. 1:09:46vulnerability then you should
  2086. 1:09:49patch your database as soon as possible
  2087. 1:09:52decrease the window of time that the
  2088. 1:09:53attacker has
  2089. 1:09:54to attack your database and to exploit
  2090. 1:09:57the vulnerability
  2091. 1:09:59and then this one we already discussed
  2092. 1:10:01it so whitelist
  2093. 1:10:03input validation so although you have
  2094. 1:10:05parameterized queries another
  2095. 1:10:07an additional defense so a defense and
  2096. 1:10:09depth is to still whitelist
  2097. 1:10:11um all the input values that come from
  2098. 1:10:16come from the user so come from the
  2099. 1:10:17client side
  2100. 1:10:20okay and that wraps up the last section
  2101. 1:10:24before i close this video let's talk
  2102. 1:10:26about the resources that were used to
  2103. 1:10:29create this video one is the web
  2104. 1:10:31security academy so the sql injection
  2105. 1:10:33module and that's the module that we'll
  2106. 1:10:35be following for the next couple of
  2107. 1:10:36videos
  2108. 1:10:38next is the web application hackers
  2109. 1:10:40handbook so chapter 9
  2110. 1:10:42attacking data stores if you want to
  2111. 1:10:44learn more about this topic you should
  2112. 1:10:46go and read this
  2113. 1:10:47chapter and then we've got the oasp
  2114. 1:10:51guide for sql injection
  2115. 1:10:52and sql injection prevention cheat sheet
  2116. 1:10:55i also included the pen test monkey
  2117. 1:10:57url and the reason behind that is
  2118. 1:11:00because
  2119. 1:11:00it lists all the different sql injection
  2120. 1:11:02payloads
  2121. 1:11:04that you could use depending on the
  2122. 1:11:07database that you're using and then
  2123. 1:11:08there's other ones like payload all the
  2124. 1:11:10things and so on we'll see a lot of this
  2125. 1:11:12when it comes to
  2126. 1:11:13the videos to wrap up in this video we
  2127. 1:11:16learned about sql injection
  2128. 1:11:17vulnerabilities how to find them
  2129. 1:11:19how to exploit them and how to prevent
  2130. 1:11:21them you can find a link to the slides
  2131. 1:11:23in the description
  2132. 1:11:24in the next couple of videos we will
  2133. 1:11:27gain hands-on experience
  2134. 1:11:29exploiting all the different types of
  2135. 1:11:30sql injection vulnerabilities that we
  2136. 1:11:32learned about
  2137. 1:11:33in this video
  2138. 1:11:36if you made it this far thank you for
  2139. 1:11:38watching make sure to hit the subscribe
  2140. 1:11:40button and share the video so that it
  2141. 1:11:42reaches a wider audience
  2142. 1:11:44also comment below what you liked about
  2143. 1:11:45the video and the type of videos you
  2144. 1:11:47would like to see in the future
  2145. 1:11:48thank you and see you in the next video

About this transcript

This page contains the full transcript of SQL Injection | Complete Guide by Rana Khalil, generated from the public captions YouTube serves with the video. The transcript has 12,141 words across 2,145 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.