View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    *
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   *******************************************************************************/
11  package com.jsql.model.injection.strategy;
12  
13  import com.jsql.model.InjectionModel;
14  import com.jsql.model.bean.util.Interaction;
15  import com.jsql.model.bean.util.Request;
16  import com.jsql.model.exception.StoppedByUserSlidingException;
17  import com.jsql.model.injection.strategy.blind.AbstractInjectionBit.BlindOperator;
18  import com.jsql.model.injection.strategy.blind.InjectionBlindBit;
19  import com.jsql.model.injection.vendor.model.VendorYaml;
20  import com.jsql.model.suspendable.AbstractSuspendable;
21  import com.jsql.util.I18nUtil;
22  import com.jsql.util.LogLevelUtil;
23  import com.jsql.util.StringUtil;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  
28  public class StrategyBlindBit extends AbstractStrategy {
29      
30      /**
31       * Log4j logger sent to view.
32       */
33      private static final Logger LOGGER = LogManager.getRootLogger();
34  
35      private InjectionBlindBit injectionBlindBit;
36      
37      public StrategyBlindBit(InjectionModel injectionModel) {
38          super(injectionModel);
39      }
40  
41      @Override
42      public void checkApplicability() throws StoppedByUserSlidingException {
43          if (this.injectionModel.getMediatorUtils().getPreferencesUtil().isStrategyBlindBitDisabled()) {
44              LOGGER.log(LogLevelUtil.CONSOLE_INFORM, AbstractStrategy.FORMAT_SKIP_STRATEGY_DISABLED, this.getName());
45              return;
46          } else if (StringUtils.isEmpty(
47              this.injectionModel.getMediatorVendor().getVendor().instance().getModelYaml().getStrategy().getBinary().getTest().getBit()
48          )) {
49              LOGGER.log(
50                  LogLevelUtil.CONSOLE_ERROR,
51                  AbstractStrategy.FORMAT_STRATEGY_NOT_IMPLEMENTED,
52                  this.getName(),
53                  this.injectionModel.getMediatorVendor().getVendor()
54              );
55              return;
56          }
57  
58          this.checkInjection(BlindOperator.OR);
59          this.checkInjection(BlindOperator.AND);
60          this.checkInjection(BlindOperator.STACK);
61          this.checkInjection(BlindOperator.NO_MODE);
62  
63          if (this.isApplicable) {
64              this.allow();
65              var requestMessageBinary = new Request();
66              requestMessageBinary.setMessage(Interaction.MESSAGE_BINARY);
67              requestMessageBinary.setParameters(this.injectionBlindBit.getInfoMessage());
68              this.injectionModel.sendToViews(requestMessageBinary);
69          } else {
70              this.unallow();
71          }
72      }
73  
74      private void checkInjection(BlindOperator blindOperator) throws StoppedByUserSlidingException {
75          if (this.isApplicable) {
76              return;
77          }
78  
79          LOGGER.log(
80              LogLevelUtil.CONSOLE_DEFAULT,
81              "{} [{}] with [{}]...",
82              () -> I18nUtil.valueByKey(AbstractStrategy.KEY_LOG_CHECKING_STRATEGY),
83              this::getName,
84              () -> blindOperator
85          );
86          this.injectionBlindBit = new InjectionBlindBit(this.injectionModel, blindOperator);
87          this.isApplicable = this.injectionBlindBit.isInjectable();
88  
89          if (this.isApplicable) {
90              LOGGER.log(
91                  LogLevelUtil.CONSOLE_SUCCESS,
92                  "{} [{}] injection with [{}]",
93                  () -> I18nUtil.valueByKey(AbstractStrategy.KEY_LOG_VULNERABLE),
94                  this::getName,
95                  () -> blindOperator
96              );
97          }
98      }
99  
100     @Override
101     public void allow(int... i) {
102         this.injectionModel.appendAnalysisReport(
103             StringUtil.formatReport(LogLevelUtil.COLOR_BLU, "### Strategy: " + this.getName())
104             + this.injectionModel.getReportWithoutIndex(
105                 this.injectionModel.getMediatorVendor().getVendor().instance().sqlTestBlindWithOperator(
106                     this.injectionModel.getMediatorVendor().getVendor().instance().sqlBlind(StringUtil.formatReport(LogLevelUtil.COLOR_GREEN, "<query>"), "0", true),
107                     this.injectionBlindBit.getBooleanMode()
108                 ),
109                 "metadataInjectionProcess",
110                 null
111             )
112         );
113         this.markVulnerability(Interaction.MARK_BLIND_BIT_VULNERABLE);
114     }
115 
116     @Override
117     public void unallow(int... i) {
118         this.markVulnerability(Interaction.MARK_BLIND_BIT_INVULNERABLE);
119     }
120 
121     @Override
122     public String inject(String sqlQuery, String startPosition, AbstractSuspendable stoppable, String metadataInjectionProcess) throws StoppedByUserSlidingException {
123         return this.injectionBlindBit.inject(
124             this.injectionModel.getMediatorVendor().getVendor().instance().sqlBlind(sqlQuery, startPosition, false),
125             stoppable
126         );
127     }
128 
129     @Override
130     public void activateWhenApplicable() {
131         if (this.injectionModel.getMediatorStrategy().getStrategy() == null && this.isApplicable()) {
132             LOGGER.log(
133                 LogLevelUtil.CONSOLE_INFORM,
134                 "{} [{}] with [{}]",
135                 () -> I18nUtil.valueByKey("LOG_USING_STRATEGY"),
136                 this::getName,
137                 () -> this.injectionBlindBit.getBooleanMode().name()
138             );
139             this.injectionModel.getMediatorStrategy().setStrategy(this);
140 
141             var requestMarkBlindBitStrategy = new Request();
142             requestMarkBlindBitStrategy.setMessage(Interaction.MARK_BLIND_BIT_STRATEGY);
143             this.injectionModel.sendToViews(requestMarkBlindBitStrategy);
144         }
145     }
146     
147     @Override
148     public String getPerformanceLength() {
149         return VendorYaml.DEFAULT_CAPACITY;
150     }
151     
152     @Override
153     public String getName() {
154         return "Blind bit";
155     }
156 }