Skip to content

Commit 9286fa6

Browse files
committed
async gunicorn
1 parent 6f154d6 commit 9286fa6

14 files changed

+46
-21
lines changed

core/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ RUN pip install --no-cache-dir -r requirements.txt
1313
COPY ./ /app/
1414
RUN python manage.py collectstatic --noinput
1515

16-
ENTRYPOINT ["gunicorn", "plt.wsgi", "-b", "0.0.0.0:8000"]
16+
ENTRYPOINT ["gunicorn", "plt.wsgi", "--worker-class gevent", "--workers=8", "--threads=2", "-b", "0.0.0.0:8000"]

core/base/apps.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class BaseConfig(AppConfig):
1212
def get_latest_attack(team, category):
1313
if category == ITEM_CATEGORY_SQLI:
1414
try:
15-
latest = SqliLog.objects.filter(from_team=team).latest()
15+
latest = SqliLog.objects.filter(from_team=team).last()
1616
except SqliLog.DoesNotExist:
1717
latest = ''
1818
elif category == ITEM_CATEGORY_XSS:
1919
try:
20-
latest = XssLog.objects.filter(from_team=team).latest()
20+
latest = XssLog.objects.filter(from_team=team).last()
2121
except XssLog.DoesNotExist:
2222
latest = ''
2323
else:

core/plt/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
'django.contrib.sessions',
5757
'django.contrib.messages',
5858
'django.contrib.staticfiles',
59+
'corsheaders',
5960
'base',
6061
'sqli',
6162
'xss',

core/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ cffi==1.14.4
33
cryptography==3.3.1
44
Django==3.1.5
55
django-cors-headers==3.6.0
6+
gevent==21.1.1
7+
greenlet==1.0.0
68
gunicorn==20.0.4
79
pycparser==2.20
810
PyMySQL==1.0.2
911
pytz==2020.5
1012
six==1.15.0
1113
sqlparse==0.4.1
14+
zope.event==4.5.0
15+
zope.interface==5.2.0

core/shop/models.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.db.models import F
33
from django.contrib.auth import get_user_model
44

5-
from base.models import RegexRule, LenRule, CspRule
5+
from base.models import RegexRule, LenRule, CspRule, SqliFilter, XssFilter
66
from env.environ import CATEGORY, ITEM_CATEGORY_SQLI, ITEM_CATEGORY_XSS
77
Team = get_user_model()
88

@@ -24,9 +24,9 @@ def already_bought(self, request):
2424

2525
def get_filter(self, team: Team):
2626
if self.category == ITEM_CATEGORY_SQLI:
27-
return team.sqli_filter
27+
return SqliFilter.objects.get(owner=team)
2828
elif self.category == ITEM_CATEGORY_XSS:
29-
return team.xss_filter
29+
return XssFilter.objects.get(owner=team)
3030

3131
def check_balance(self, team: Team):
3232
return team.balance >= self.price

core/shop/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717

1818
urlpatterns = [
1919
path('', views.ShopView.as_view(), name='shop'),
20-
path('<int:item_id>', views.ItemView.as_view(), name='item'),
20+
path('<int:item_id>/', views.ItemView.as_view(), name='item'),
2121
]

core/sqli/apps.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pymysql
55
import random
6+
import re
67

78
from utils.generator import random_string, random_flag
89
from utils.mysql import sqli_db

core/sqli/models.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from django.db import models
2+
from django.conf import settings
3+
from pytz import timezone
4+
25

36

47
class SqliLog(models.Model):
@@ -9,6 +12,11 @@ class SqliLog(models.Model):
912
succeed = models.BooleanField(default=False)
1013
return_value = models.CharField(max_length=1000)
1114

15+
@property
16+
def created_at_korean_time(self):
17+
korean_timezone = timezone(settings.TIME_ZONE)
18+
return self.created_at.astimezone(korean_timezone)
19+
1220
def __str__(self):
1321
return f"SQLi query from {self.from_team} to {self.to_team}"
1422

core/xss/apps.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import binascii
33
import re
4-
from datetime import datetime
4+
from django.utils import timezone
55

66
from django.apps import AppConfig
77
from django.contrib.auth import get_user_model
@@ -27,15 +27,20 @@ def create_flag():
2727

2828

2929
def get_time_passed_after_last_attack(attack_team, target_team):
30+
now_time = int(timezone.localtime().strftime("%Y%m%d%H%M%S"))
3031
last_attack_time = 0
3132
try:
32-
last_attack = XssLog.objects.filter(from_team=attack_team,
33+
attacks = XssLog.objects.filter(from_team=attack_team,
3334
to_team=target_team,
34-
succeed=True).latest()
35-
last_attack_time = int(last_attack.created_at.strftime("%Y%m%d%H%M%S"))
35+
succeed=True)
36+
if len(attacks) == 0:
37+
return now_time
38+
last_attack = attacks.last()
39+
last_attack_time = int(last_attack.created_at_korean_time.strftime("%Y%m%d%H%M%S"))
3640
except XssLog.DoesNotExist:
3741
pass
38-
return int(datetime.now().strftime("%Y%m%d%H%M%S")) - last_attack_time
42+
print(now_time - last_attack_time)
43+
return now_time - last_attack_time
3944

4045

4146
def query_xss(attack_team_name: str, target_team_name: str, query: str):
@@ -60,7 +65,7 @@ def query_xss(attack_team_name: str, target_team_name: str, query: str):
6065
xss_log.query = query
6166
xss_log.save()
6267

63-
checked, succeed = check_alert(f'http://localhost:8000/xss/{xss_log.hash}')
68+
checked, succeed = check_alert(f'http://plus.or.kr:17354/xss/{xss_log.hash}/')
6469
xss_log.checked = checked
6570
xss_log.succeed = succeed
6671
xss_log.save()

core/xss/checkbot.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def check_alert(URI):
1818
try:
1919
driver.implicitly_wait(2)
2020
alert = driver.switch_to_alert()
21-
print(alert)
2221
alert.accept()
2322
driver.quit()
2423
return True, True

core/xss/models.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from django.db import models
2+
from django.conf import settings
3+
from pytz import timezone
24

35
from base.models import CspRule
46

@@ -13,6 +15,11 @@ class XssLog(models.Model):
1315
checked = models.BooleanField(default=False)
1416
succeed = models.BooleanField(default=False)
1517

18+
@property
19+
def created_at_korean_time(self):
20+
korean_timezone = timezone(settings.TIME_ZONE)
21+
return self.created_at.astimezone(korean_timezone)
22+
1623
def __str__(self):
1724
return f"XSS query from {self.from_team} to {self.to_team} /{self.hash}"
1825

core/xss/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717

1818
urlpatterns = [
1919
path('', views.XssView.as_view(), name='xss'),
20-
re_path(r'^(?P<hash>[0-9a-f]+)$', views.XssTestView.as_view(), name='test_xss'),
20+
re_path(r'^(?P<hash>[0-9a-f]+)\/$', views.XssTestView.as_view(), name='test_xss'),
2121
]

core/xss/views.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def post(self, request):
3737
if not succeed:
3838
return JsonResponse({
3939
'success': False,
40-
'message': message,
40+
'message': message
4141
}, status=status_code)
4242

4343
flag = create_flag()
@@ -49,10 +49,10 @@ def post(self, request):
4949

5050
class XssTestView(View):
5151
def get(self, request, hash):
52-
data = XssLog.objects.filter(hash = hash)
53-
if data:
52+
try:
53+
data = XssLog.objects.get(hash = hash)
5454
return render(request, 'xss/xss_test.html', {
55-
'data': data[0],
55+
'data': data,
5656
})
57-
else:
57+
except XssLog.DoesNotExist:
5858
return HttpResponse(status=404)

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ services:
4141
- static_volume:/app/static
4242
- "./core/data/db.sqlite3:/app/db.sqlite3"
4343
expose:
44-
- 8000
44+
- 8000

0 commit comments

Comments
 (0)