|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a check for use of wildcard injection. |
|
8 """ |
|
9 |
|
10 # |
|
11 # This is a modified version of the one found in the bandit package. |
|
12 # |
|
13 # Original Copyright 2014 Hewlett-Packard Development Company, L.P. |
|
14 # |
|
15 # SPDX-License-Identifier: Apache-2.0 |
|
16 # |
|
17 |
|
18 from Security.SecurityDefaults import SecurityDefaults |
|
19 |
|
20 |
|
21 def getChecks(): |
|
22 """ |
|
23 Public method to get a dictionary with checks handled by this module. |
|
24 |
|
25 @return dictionary containing checker lists containing checker function and |
|
26 list of codes |
|
27 @rtype dict |
|
28 """ |
|
29 return { |
|
30 "Call": [ |
|
31 (checkLinuxCommandsWildcardInjection, ("S609",)), |
|
32 ], |
|
33 } |
|
34 |
|
35 |
|
36 def checkLinuxCommandsWildcardInjection(reportError, context, config): |
|
37 """ |
|
38 Function to check for use of wildcard injection. |
|
39 |
|
40 @param reportError function to be used to report errors |
|
41 @type func |
|
42 @param context security context object |
|
43 @type SecurityContext |
|
44 @param config dictionary with configuration data |
|
45 @type dict |
|
46 """ |
|
47 if config and "shell_injection_subprocess" in config: |
|
48 subProcessFunctionNames = config["shell_injection_subprocess"] |
|
49 else: |
|
50 subProcessFunctionNames = SecurityDefaults[ |
|
51 "shell_injection_subprocess"] |
|
52 |
|
53 if config and "shell_injection_shell" in config: |
|
54 shellFunctionNames = config["shell_injection_shell"] |
|
55 else: |
|
56 shellFunctionNames = SecurityDefaults["shell_injection_shell"] |
|
57 |
|
58 vulnerableFunctions = ['chown', 'chmod', 'tar', 'rsync'] |
|
59 if ( |
|
60 context.callFunctionNameQual in shellFunctionNames or |
|
61 (context.callFunctionNameQual in subProcessFunctionNames and |
|
62 context.checkCallArgValue('shell', 'True')) |
|
63 ): |
|
64 if context.callArgsCount >= 1: |
|
65 callArgument = context.getCallArgAtPosition(0) |
|
66 argumentString = '' |
|
67 if isinstance(callArgument, list): |
|
68 for li in callArgument: |
|
69 argumentString = argumentString + ' {0}'.format(li) |
|
70 elif isinstance(callArgument, str): |
|
71 argumentString = callArgument |
|
72 |
|
73 if argumentString != '': |
|
74 for vulnerableFunction in vulnerableFunctions: |
|
75 if ( |
|
76 vulnerableFunction in argumentString and |
|
77 '*' in argumentString |
|
78 ): |
|
79 lineNo = context.getLinenoForCallArg('shell') |
|
80 if lineNo < 1: |
|
81 lineNo = context.node.lineno |
|
82 offset = context.getOffsetForCallArg('shell') |
|
83 if offset < 0: |
|
84 offset = context.node.col_offset |
|
85 reportError( |
|
86 lineNo - 1, |
|
87 offset, |
|
88 "S609", |
|
89 "H", |
|
90 "M", |
|
91 context.callFunctionNameQual |
|
92 ) |