CVE-2015-10026: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: A vulnerability was found in tiredtyrant flairbot. It has been declared as critical. This vulnerability affects unknown code of the file flair.py. The manipulation leads to sql injection. The patch is identified as 5e112b68c6faad1d4699d02c1ebbb7daf48ef8fb. It is recommended to apply a patch to fix this issue. VDB-217618 is the identifier assigned to this vulnerability.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 9.8
Published: 2023-01-07T13:15Z
Repository: https://github.com/tiredtyrant/flairbot
Language: Python
Files changed: flair.py
Commit date: 2015-04-27 20:40:31 -0400
Commit message: Alteracoes nas queries para evitar sql injection

Signed-off-by: tiredtyrant <tiredtyrant@gmail.com>

Diff: diff --git a/flair.py b/flair.py
index 3720af4..6842246 100644
--- a/flair.py
+++ b/flair.py
@@ -7,22 +7,22 @@ con = sqlite3.connect('estados_municipios.db')
 cursor = con.cursor()
 
 def dbLookup(msg):
     if len(msg.split(',')) != 2:
         #procura na lista de paises
-        query = 'SELECT id FROM paises WHERE nome == "%s"' % (msg)
-        cursor.execute(query)
+        query = 'SELECT id FROM paises WHERE nome == ?;'
+        cursor.execute(query,(msg,))
         if cursor.fetchone():
             return True
         else:
             return False
     else:
         cidade = msg.split(',')[0].strip()
         estado = msg.split(',')[1].strip()
         #check cidade pertence ao estado
-        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == "%s" AND estados.uf == "%s";' % (cidade, estado)
-        cursor.execute(query)
+        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == ? AND estados.uf == ?;'
+        cursor.execute(query,(cidade,estado))
         if not cursor.fetchone():
             return False
             
     return True
     
@@ -35,30 +35,33 @@ def main():
     else:
         print 'failed to log in'
         return
     while True:
         time.sleep(0.5)
-        for msg in r.get_unread(limit=None):
-            try:
-                print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
-            except UnicodeEncodeError:
-                print 'AUTHOR: %s - unprintable chars' % (msg.author)
-            sub = r.get_subreddit('brasil')
-            if msg.subject == 'flair':
-                if dbLookup(msg.body):
-                    estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
-                    sub.set_flair(msg.author,msg.body,estado)
-                    r.send_message(msg.author,'flair','Flair configurado.')
-                    print('flair ok')
-                else:
-                    r.send_message(msg.author,'flair','Configuração de flair falhou.')
-                    print('flair fail')
-            if msg.subject == 'remover flair':
-                sub.set_flair(msg.author,'','')
-                r.send_message(msg.author,'flair','Flair removido.')
-                print('remove flair ok')
+        try:
+            for msg in r.get_unread(limit=None):
+                try:
+                    print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
+                except UnicodeEncodeError:
+                    print 'AUTHOR: %s - unprintable chars' % (msg.author)
+                sub = r.get_subreddit('brasil')
+                if msg.subject == 'flair':
+                    if dbLookup(msg.body):
+                        estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
+                        sub.set_flair(msg.author,msg.body,estado)
+                        r.send_message(msg.author,'flair','Flair configurado.')
+                        print('flair ok')
+                    else:
+                        r.send_message(msg.author,'flair','Configuração de flair falhou.')
+                        print('flair fail')
+                if msg.subject == 'remover flair':
+                    sub.set_flair(msg.author,'','')
+                    r.send_message(msg.author,'flair','Flair removido.')
+                    print('remove flair ok')
                 
-            msg.mark_as_read()
+                msg.mark_as_read()
+        except:
+            pass
 
 if __name__ == '__main__':
     main()
-    
\ No newline at end of file
+    

Vulnerable code:         query = 'SELECT id FROM paises WHERE nome == "%s"' % (msg)
        cursor.execute(query)
        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == "%s" AND estados.uf == "%s";' % (cidade, estado)
        cursor.execute(query)
        for msg in r.get_unread(limit=None):
            try:
                print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
            except UnicodeEncodeError:
                print 'AUTHOR: %s - unprintable chars' % (msg.author)
            sub = r.get_subreddit('brasil')
            if msg.subject == 'flair':
                if dbLookup(msg.body):
                    estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
                    sub.set_flair(msg.author,msg.body,estado)
                    r.send_message(msg.author,'flair','Flair configurado.')
                    print('flair ok')
                else:
                    r.send_message(msg.author,'flair','Configuração de flair falhou.')
                    print('flair fail')
            if msg.subject == 'remover flair':
                sub.set_flair(msg.author,'','')
                r.send_message(msg.author,'flair','Flair removido.')
                print('remove flair ok')
            msg.mark_as_read()
    
Fixed code:         query = 'SELECT id FROM paises WHERE nome == ?;'
        cursor.execute(query,(msg,))
        query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == ? AND estados.uf == ?;'
        cursor.execute(query,(cidade,estado))
        try:
            for msg in r.get_unread(limit=None):
                try:
                    print 'AUTHOR: %s - SUBJECT: %s - BODY: %s' % (msg.author, msg.subject, msg.body)
                except UnicodeEncodeError:
                    print 'AUTHOR: %s - unprintable chars' % (msg.author)
                sub = r.get_subreddit('brasil')
                if msg.subject == 'flair':
                    if dbLookup(msg.body):
                        estado = 'world' if len(msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
                        sub.set_flair(msg.author,msg.body,estado)
                        r.send_message(msg.author,'flair','Flair configurado.')
                        print('flair ok')
                    else:
                        r.send_message(msg.author,'flair','Configuração de flair falhou.')
                        print('flair fail')
                if msg.subject == 'remover flair':
                    sub.set_flair(msg.author,'','')
                    r.send_message(msg.author,'flair','Flair removido.')
                    print('remove flair ok')
                msg.mark_as_read()
        except:
            pass