|
| 1 | +/** |
| 2 | + * @name Potential Timebomb |
| 3 | + * @description If there is data flow from a file's last modification date and an offset to a condition statement, this could trigger a "time bomb". |
| 4 | + * @kind path-problem |
| 5 | + * @precision Low |
| 6 | + * @problem.severity warning |
| 7 | + * @id cs/backdoor/potential-time-bomb |
| 8 | + * @tags security |
| 9 | + * solorigate |
| 10 | + */ |
| 11 | + |
| 12 | +import csharp |
| 13 | +import DataFlow |
| 14 | + |
| 15 | +query predicate nodes = PathGraph::nodes/3; |
| 16 | + |
| 17 | +query predicate edges(DataFlow::PathNode a, DataFlow::PathNode b) { |
| 18 | + PathGraph::edges(a, b) |
| 19 | + or |
| 20 | + exists( |
| 21 | + FlowsFromGetLastWriteTimeConfigToTimeSpanArithmeticCallable conf1, |
| 22 | + FlowsFromTimeSpanArithmeticToTimeComparisonCallable conf2 |
| 23 | + | |
| 24 | + conf1 = a.getConfiguration() and |
| 25 | + conf1.isSink(a.getNode()) and |
| 26 | + conf2 = b.getConfiguration() and |
| 27 | + b.isSource() |
| 28 | + ) |
| 29 | + or |
| 30 | + exists( |
| 31 | + FlowsFromTimeSpanArithmeticToTimeComparisonCallable conf1, |
| 32 | + FlowsFromTimeComparisonCallableToSelectionStatementCondition conf2 |
| 33 | + | |
| 34 | + conf1 = a.getConfiguration() and |
| 35 | + conf1.isSink(a.getNode()) and |
| 36 | + conf2 = b.getConfiguration() and |
| 37 | + b.isSource() |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Class that will help to find the source for the trigger file-modification date. |
| 43 | + * |
| 44 | + * May be extended as new patterns for similar time bombs are found. |
| 45 | + */ |
| 46 | +class GetLastWriteTimeMethod extends Method { |
| 47 | + GetLastWriteTimeMethod() { |
| 48 | + this.getQualifiedName() in [ |
| 49 | + "System.IO.File.GetLastWriteTime", "System.IO.File.GetFileCreationTime", |
| 50 | + "System.IO.File.GetCreationTimeUtc", "System.IO.File.GetLastAccessTimeUtc" |
| 51 | + ] |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Abstracts `System.DateTime` structure |
| 57 | + */ |
| 58 | +class DateTimeStruct extends Struct { |
| 59 | + DateTimeStruct() { this.getQualifiedName() = "System.DateTime" } |
| 60 | + |
| 61 | + /** |
| 62 | + * holds if the Callable is used for DateTime arithmetic operations |
| 63 | + */ |
| 64 | + Callable getATimeSpanArtithmeticCallable() { |
| 65 | + (result = this.getAnOperator() or result = this.getAMethod()) and |
| 66 | + result.getName() in [ |
| 67 | + "Add", "AddDays", "AddHours", "AddMilliseconds", "AddMinutes", "AddMonths", "AddSeconds", |
| 68 | + "AddTicks", "AddYears", "+", "-" |
| 69 | + ] |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Holds if the Callable is used for DateTime comparision |
| 74 | + */ |
| 75 | + Callable getAComparisonCallable() { |
| 76 | + (result = this.getAnOperator() or result = this.getAMethod()) and |
| 77 | + result.getName() in ["Compare", "CompareTo", "Equals", "==", "!=", "<", ">", "<=", ">="] |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Dataflow configuration to find flow from a GetLastWriteTime source to a DateTime arithmetic operation |
| 83 | + */ |
| 84 | +private class FlowsFromGetLastWriteTimeConfigToTimeSpanArithmeticCallable extends TaintTracking::Configuration { |
| 85 | + FlowsFromGetLastWriteTimeConfigToTimeSpanArithmeticCallable() { |
| 86 | + this = "FlowsFromGetLastWriteTimeConfigToTimeSpanArithmeticCallable" |
| 87 | + } |
| 88 | + |
| 89 | + override predicate isSource(DataFlow::Node source) { |
| 90 | + exists(Call call, GetLastWriteTimeMethod m | |
| 91 | + m.getACall() = call and |
| 92 | + source.asExpr() = call |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + override predicate isSink(DataFlow::Node sink) { |
| 97 | + exists(Call call, DateTimeStruct dateTime | |
| 98 | + call.getAChild*() = sink.asExpr() and |
| 99 | + call = dateTime.getATimeSpanArtithmeticCallable().getACall() |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Dataflow configuration to find flow from a DateTime arithmetic operation to a DateTime comparison operation |
| 106 | + */ |
| 107 | +private class FlowsFromTimeSpanArithmeticToTimeComparisonCallable extends TaintTracking::Configuration { |
| 108 | + FlowsFromTimeSpanArithmeticToTimeComparisonCallable() { |
| 109 | + this = "FlowsFromTimeSpanArithmeticToTimeComparisonCallable" |
| 110 | + } |
| 111 | + |
| 112 | + override predicate isSource(DataFlow::Node source) { |
| 113 | + exists(DateTimeStruct dateTime, Call call | source.asExpr() = call | |
| 114 | + call = dateTime.getATimeSpanArtithmeticCallable().getACall() |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + override predicate isSink(DataFlow::Node sink) { |
| 119 | + exists(Call call, DateTimeStruct dateTime | |
| 120 | + call.getAnArgument().getAChild*() = sink.asExpr() and |
| 121 | + call = dateTime.getAComparisonCallable().getACall() |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Dataflow configuration to find flow from a DateTime comparison operation to a Selection Statement (such as an If) |
| 128 | + */ |
| 129 | +private class FlowsFromTimeComparisonCallableToSelectionStatementCondition extends TaintTracking::Configuration { |
| 130 | + FlowsFromTimeComparisonCallableToSelectionStatementCondition() { |
| 131 | + this = "FlowsFromTimeComparisonCallableToSelectionStatementCondition" |
| 132 | + } |
| 133 | + |
| 134 | + override predicate isSource(DataFlow::Node source) { |
| 135 | + exists(DateTimeStruct dateTime, Call call | source.asExpr() = call | |
| 136 | + call = dateTime.getAComparisonCallable().getACall() |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + override predicate isSink(DataFlow::Node sink) { |
| 141 | + exists(SelectionStmt sel | sel.getCondition().getAChild*() = sink.asExpr()) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Holds if the last file modification date from the call to getLastWriteTimeMethodCall will be used in a DateTime arithmetic operation timeArithmeticCall, |
| 147 | + * which is then used for a DateTime comparison timeComparisonCall and the result flows to a Selection statement which is likely a TimeBomb trigger |
| 148 | + */ |
| 149 | +predicate isPotentialTimeBomb( |
| 150 | + DataFlow::PathNode pathSource, DataFlow::PathNode pathSink, Call getLastWriteTimeMethodCall, |
| 151 | + Call timeArithmeticCall, Call timeComparisonCall, SelectionStmt selStatement |
| 152 | +) { |
| 153 | + exists( |
| 154 | + FlowsFromGetLastWriteTimeConfigToTimeSpanArithmeticCallable config1, Node sink, |
| 155 | + DateTimeStruct dateTime, FlowsFromTimeSpanArithmeticToTimeComparisonCallable config2, |
| 156 | + Node sink2, FlowsFromTimeComparisonCallableToSelectionStatementCondition config3, Node sink3 |
| 157 | + | |
| 158 | + pathSource.getNode() = exprNode(getLastWriteTimeMethodCall) and |
| 159 | + config1.hasFlow(exprNode(getLastWriteTimeMethodCall), sink) and |
| 160 | + timeArithmeticCall = dateTime.getATimeSpanArtithmeticCallable().getACall() and |
| 161 | + timeArithmeticCall.getAChild*() = sink.asExpr() and |
| 162 | + config2.hasFlow(exprNode(timeArithmeticCall), sink2) and |
| 163 | + timeComparisonCall = dateTime.getAComparisonCallable().getACall() and |
| 164 | + timeComparisonCall.getAnArgument().getAChild*() = sink2.asExpr() and |
| 165 | + config3.hasFlow(exprNode(timeComparisonCall), sink3) and |
| 166 | + selStatement.getCondition().getAChild*() = sink3.asExpr() and |
| 167 | + pathSink.getNode() = sink3 |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +from |
| 172 | + DataFlow::PathNode source, DataFlow::PathNode sink, Call getLastWriteTimeMethodCall, |
| 173 | + Call timeArithmeticCall, Call timeComparisonCall, SelectionStmt selStatement |
| 174 | +where |
| 175 | + isPotentialTimeBomb(source, sink, getLastWriteTimeMethodCall, timeArithmeticCall, |
| 176 | + timeComparisonCall, selStatement) |
| 177 | +select selStatement, source, sink, |
| 178 | + "Possible TimeBomb logic triggered by $@ that takes into account $@ from the $@ as part of the potential trigger.", |
| 179 | + timeComparisonCall, timeComparisonCall.toString(), timeArithmeticCall, "an offset", |
| 180 | + getLastWriteTimeMethodCall, "last modification time of a file" |
0 commit comments