File size: 3,082 Bytes
c276eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad247f35-1a9d-4f3a-9788-ecdbc43b6976",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9051f612-d935-4b90-a623-0c5504b51d9c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Database registered!'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%set_database codeql-db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a460e252-30f5-4cad-9a32-253e9d789025",
   "metadata": {},
   "outputs": [],
   "source": [
    "import python\n",
    "import semmle.python.ApiGraphs\n",
    "import semmle.python.dataflow.new.RemoteFlowSources\n",
    "import semmle.python.dataflow.new.DataFlow"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee080ea9-375f-432b-8fc8-a93fa0e60910",
   "metadata": {},
   "source": [
    "## Code exploration"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c305ec3-40f7-4a7e-bef6-845ae0ef0c4f",
   "metadata": {},
   "source": [
    "### Find all calls to `subprocess.call`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "32c13716-39a0-43d9-8e05-25a11fcd557f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>n</th>\n",
       "      <th>URL for n</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ControlFlowNode for Attribute()</td>\n",
       "      <td>file:///Users/pwntester/src/github.com/github/codeql-jupyter-kernel/example/src/main.py:3:1:3:38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query predicate test(API::CallNode n) {\n",
    "    n = API::moduleImport(\"subprocess\").getMember(\"call\").getACall()\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e37dbb8-32b5-45a0-8aa2-90a8b415c905",
   "metadata": {},
   "source": [
    "## Attack surface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9383863b-0f6e-4fc3-afad-b653cae62bdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "query predicate attackSurface(DataFlow::Node n) { n instanceof RemoteFlowSource }"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "CodeQL",
   "language": "codeql",
   "name": "codeql"
  },
  "language_info": {
   "file_extension": ".ql",
   "help_links": [
    {
     "text": "MetaKernel Magics",
     "url": "https://metakernel.readthedocs.io/en/latest/source/README.html"
    }
   ],
   "mimetype": "text/x-codeql",
   "name": "codeql"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}