44import uuid
55from colorama import Fore , Back , Style
66
7- from modules .redwarden_parser import MalleableParser
8- from modules .logger import logger
7+ from lib . modules .redwarden_parser import MalleableParser
8+ from lib . modules .logger import logger
99
1010class caddy_Proxy :
11- def __init__ (self , profile , local , chains , filename , geo_country , xf ):
11+ def __init__ (self , profile , local , chains , geo_country , xf ):
1212 self .__profile = profile
1313 # Redirect destination, only https
1414 self .__local = local
1515 self .__chains = chains
16- self .__outfile = filename
16+ self .__outfile = "Caddyfile"
1717 self .__country = geo_country
1818 self .__xf_switch = xf
1919
@@ -71,7 +71,7 @@ def wrapper(self):
7171(GEOFILTER) {
7272 @geofilter {
7373 not maxmind_geolocation {
74- db_path "./data/GeoLite2-Country.mmdb"
74+ db_path "./lib/ data/GeoLite2-Country.mmdb"
7575 allow_countries REPLEACE_ME
7676 }
7777 not remote_ip 10.0.0.0/8
@@ -135,7 +135,7 @@ def generate_HanldeBlock(self, chain, proxyBlock):
135135 tag = "(caddy-guard-%s)" % str (chain .strip ("\n " ).split (":" )[3 ])
136136 template_Header = r''' {
137137 # For old windows version support, like: win7
138- tls ./localhost.crt ./localhost.key {
138+ tls ./cert-out/ localhost.crt ./cert-out /localhost.key {
139139 ciphers TLS_RSA_WITH_AES_128_CBC_SHA TLS_RSA_WITH_AES_256_CBC_SHA TLS_RSA_WITH_AES_128_GCM_SHA256 TLS_RSA_WITH_AES_256_GCM_SHA384 TLS_AES_128_GCM_SHA256 TLS_AES_256_GCM_SHA384 TLS_CHACHA20_POLY1305_SHA256 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
140140 }
141141 import basic-security
@@ -167,45 +167,33 @@ def generate_HanldeBlock(self, chain, proxyBlock):
167167 header "user-agent" "SecurityString"
168168 header "Accept-SecurityString" "REPLEACE_B"
169169}
170- cgi @matcher_TeamserverGuard REPLEACE_C /iptables.sh'''
171- teamserver_WardenBlock = teamserver_WardenBlock .replace ("REPLEACE_A" ,warden_Path ).replace ("REPLEACE_B" ,securityString ). replace ( "REPLEACE_C" , os . getcwd ())
170+ cgi @matcher_TeamserverGuard ./lib/run /iptables.sh'''
171+ teamserver_WardenBlock = teamserver_WardenBlock .replace ("REPLEACE_A" ,warden_Path ).replace ("REPLEACE_B" ,securityString )
172172 proxyBlock = proxyBlock + teamserver_WardenBlock
173173
174174 # Generate new iptables.sh
175- print (Fore .YELLOW + "\r \n [+] Don't foget grant execute permission to iptables.sh" )
176- print (Fore .RED + "chmod 777 ./iptables.sh\r \n " )
177- with open ("iptables-template.sh" ,'r' ) as f :
178- file = f .read ()
179- with open ("iptables.sh" ,'w' ) as f2 :
180- f2 .write (file .replace ("REPLEACE_HERE" ,chain .strip ("\n " ).split (":" )[5 ]))
175+ with open ("./lib/template/iptables-template.sh" ,'r' ) as f : file = f .read ()
176+
177+ if os .path .exists ('./lib/run' ) == False : os .makedirs ('./lib/run' , exist_ok = True )
178+
179+ with open ("./lib/run/iptables.sh" ,'w' ) as f2 : f2 .write (file .replace ("REPLEACE_HERE" ,chain .strip ("\n " ).split (":" )[5 ]))
181180
182181 # Teamserver warden tips
183- windows_CurlTips = r'''
184- add-type @"
185- using System.Net;
186- using System.Security.Cryptography.X509Certificates;
187- public class TrustAllCertsPolicy : ICertificatePolicy {
188- public bool CheckValidationResult(
189- ServicePoint srvPoint, X509Certificate certificate,
190- WebRequest request, int certificateProblem) {
191- return true;
192- }
193- }
194- "@
195- [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
196182
197- $headers = @{
198- "user-agent" = 'SecurityString'
199- "Accept-SecurityString" = 'REPLEACE_B'
200- }
201- Invoke-WebRequest https://[REPLEACE_TO_YOUR_VPS_IP]:REPLEACE_A/REPLEACE_C -Headers $headers
202- '''
203- print (Fore .RED + "[!] Execute this command on your vps server" )
204- print (Fore .YELLOW + "sudo iptables -I INPUT -p tcp --dport %s -j DROP" % chain .strip ("\n " ).split (":" )[5 ])
205- print (Fore .GREEN + "\r \n [1] Add whitelist ip into teamserver port warden (for linux client -- bash)" )
206- print (Fore .YELLOW + 'curl https://[REPLEACE_TO_YOUR_VPS_IP]:%s/%s -H "user-agent: SecurityString" -H "Accept-SecurityString: %s" -k -vvv ' % (chain .strip ("\n " ).split (":" )[0 ], warden_Path , securityString ))
207- print (Fore .GREEN + "\r \n [2] Add whitelist ip into teamserver port warden (for windows client -- powershell)" )
208- print ("-" * 120 + Fore .YELLOW + windows_CurlTips .replace ("REPLEACE_A" ,chain .strip ("\n " ).split (":" )[0 ]).replace ("REPLEACE_B" ,securityString ).replace ("REPLEACE_C" ,warden_Path ) + Fore .GREEN + "-" * 120 + Style .RESET_ALL )
183+ if os .path .exists ('teamserver-guard' ) == False : os .makedirs ('teamserver-guard' , exist_ok = True )
184+
185+ with open ('./lib/template/teamserver-guard-Win.ps1' , 'r' ) as f : windows_CurlTips = f .read ()
186+
187+ with open ('./teamserver-guard/teamserver-guard-Win.ps1' , 'w' ) as f :
188+ f .write (windows_CurlTips .replace ("REPLEACE_A" ,chain .strip ("\n " ).split (":" )[0 ]).replace ("REPLEACE_B" ,securityString ).replace ("REPLEACE_C" ,warden_Path ))
189+
190+ linux_Script = 'curl https://[REPLEACE_TO_YOUR_VPS_IP]:%s/%s -H "user-agent: SecurityString" -H "Accept-SecurityString: %s" -k' % (chain .strip ("\n " ).split (":" )[0 ], warden_Path , securityString )
191+
192+ with open ('./teamserver-guard/teamserver-guard-Linux.sh' , 'w' ) as f : f .write (linux_Script )
193+
194+ with open ('run.sh' , 'w' ) as run_Script : run_Script .write ("chmod 777 ./lib/run/iptables.sh\n sudo iptables -I INPUT -p tcp --dport %s -j DROP" % chain .strip ("\n " ).split (":" )[5 ])
195+ #print(Fore.YELLOW + "sudo iptables -I INPUT -p tcp --dport %s -j DROP"%chain.strip("\n").split(":")[5])
196+ print (Fore .GREEN + "\r \n [+] Add whitelist ip into teamserver port warden scripts in: ./teamserver-guard" )
209197
210198 except :
211199 pass
@@ -214,67 +202,21 @@ def generate_HanldeBlock(self, chain, proxyBlock):
214202 return final
215203
216204 def generate_Caddyfile (self , full_Block ):
217- caddyguard_Header = r'''{
218- debug
219- log
220- order tls last
221- auto_https off
222- order cgi last
223- }
224-
225- (basic-security) {
226- header {
227- Server "Apache/2.4.50 (Unix) OpenSSL/1.1.1d"
228- X-Robots-Tag "noindex, nofollow, nosnippet, noarchive"
229- X-Content-Type-Options "nosniff"
230- Permissions-Policy interest-cohort=()
231- Strict-Transport-Security max-age=31536000;
232- X-Content-Type-Options nosniff
233- X-Frame-Options DENY
234- Referrer-Policy no-referrer-when-downgrade
235- Cache-Control no-cache
236- X-Powered-By
237- X-Page-Speed
238- X-Varnish
239- }
240- }
241-
242- (basic-blacklist) {
243- @ua_denylist {
244- import ./data/bad-user-agents.caddy
245- }
246-
247- @ip_denylist {
248- import ./data/bad-ips.caddy
249- }
250-
251- # UA blacklist
252- route @ua_denylist {
253- abort
254- }
255-
256- # IP blacklist
257- route @ip_denylist {
258- abort
259- }
260- }
261-
262- '''
205+ with open ('./lib/template/caddyfile-header' , 'r' ) as f : caddyguard_Header = f .read ()
263206
264- with open (self .__outfile ,'w+' ) as f :
265- f .write (caddyguard_Header + full_Block )
207+ with open (self .__outfile ,'w+' ) as f : f .write (caddyguard_Header + full_Block )
266208
267- def tips (self ,chains ):
209+ def tips (self , chains ):
268210 print (Fore .GREEN + "[+] Formating caddyfile" )
269211 os .system ("cat %s | ./caddy fmt --overwrite" % self .__outfile )
270- print ( Fore . RED + "[!] Use iptables rules to drop C2 backend port traffic and make sure it only allow incomming traffic with upstream address" )
212+ iptables = open ( "run.sh" , 'a' )
271213 for i in chains :
272- print ( Fore . YELLOW + "sudo iptables -I INPUT -p tcp --dport %s -j DROP"% (i .strip ("\n " ).split (":" )[3 ]))
273- print ( "sudo iptables -I INPUT -s %s -p tcp --dport %s -j ACCEPT"% (self .__local , i .strip ("\n " ).split (":" )[3 ]))
274- print ( Fore . RED + " \r \n [!] Run caddy with profile" )
275- print ( Fore . YELLOW + "sudo ./caddy run --config %s --adapter caddyfile" % self . __outfile )
276- print ( " \r \n [!] Reload caddy with profile" )
277- print ("sudo ./caddy reload --config %s --adapter caddyfile" % self . __outfile + Style . RESET_ALL )
214+ iptables . write ( " \n sudo iptables -I INPUT -p tcp --dport %s -j DROP"% (i .strip ("\n " ).split (":" )[3 ]))
215+ iptables . write ( " \n sudo iptables -I INPUT -s %s -p tcp --dport %s -j ACCEPT"% (self .__local , i .strip ("\n " ).split (":" )[3 ]))
216+ iptables . write ( " \n sudo ./ caddy run --config %s --adapter caddyfile" % self . __outfile )
217+ iptables . close ( )
218+ os . chmod ( './run.sh' , 0o0777 )
219+ print (Fore . RED + " \r \n [!] Run redcaddy with: ./run.sh" )
278220
279221if __name__ == '__main__' :
280222 parser = argparse .ArgumentParser (add_help = True , description = "Generate C2 redirection config file which is base on caddy." )
@@ -286,7 +228,6 @@ def tips(self,chains):
286228 'For multiple country please separated by a single space, Like: CN US' )
287229 parser .add_argument ('-xf' , action = 'store_true' , help = 'Using x-forwarded-for header ip address as remote ip'
288230 ',the source request must include x-forwarded-for header' )
289- parser .add_argument ('-o' , '-out' , metavar = "filename" ,action = 'store' , help = 'Filename you want to save as' )
290231
291232 if len (sys .argv )== 1 :
292233 parser .print_help ()
@@ -303,9 +244,6 @@ def tips(self,chains):
303244 if options .r == None :
304245 logger .err ("[-] Please specify port" )
305246 sys .exit (1 )
306- if options .o == None :
307- logger .err ("[-] Please output output destination" )
308- sys .exit (1 )
309247
310- executer = caddy_Proxy (options .f , options .l , options .r , options .o , options . c , options .xf )
248+ executer = caddy_Proxy (options .f , options .l , options .r , options .c , options .xf )
311249 executer .wrapper ()
0 commit comments