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