Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1397)

Side by Side Diff: net/tools/testserver/testserver.py

Issue 7246021: Prevent DOS attack on UDP echo servers by distinguishing between an echo request (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python2.4 1 #!/usr/bin/python2.4
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """This is a simple HTTP/FTP/SYNC/TCP ECHO/UDP ECHO/ server used for testing 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ server used for testing Chrome.
7 Chrome.
8 7
9 It supports several test URLs, as specified by the handlers in TestPageHandler. 8 It supports several test URLs, as specified by the handlers in TestPageHandler.
10 By default, it listens on an ephemeral port and sends the port number back to 9 By default, it listens on an ephemeral port and sends the port number back to
11 the originating process over a pipe. The originating process can specify an 10 the originating process over a pipe. The originating process can specify an
12 explicit port if necessary. 11 explicit port if necessary.
13 It can use https if you specify the flag --https=CERT where CERT is the path 12 It can use https if you specify the flag --https=CERT where CERT is the path
14 to a pem file containing the certificate and private key that should be used. 13 to a pem file containing the certificate and private key that should be used.
15 """ 14 """
16 15
17 import asyncore 16 import asyncore
18 import base64 17 import base64
19 import BaseHTTPServer 18 import BaseHTTPServer
20 import cgi 19 import cgi
21 import errno 20 import errno
22 import optparse 21 import optparse
23 import os 22 import os
23 import random
24 import re 24 import re
25 import select 25 import select
26 import simplejson 26 import simplejson
27 import SocketServer 27 import SocketServer
28 import socket 28 import socket
29 import sys 29 import sys
30 import struct 30 import struct
31 import time 31 import time
32 import urlparse 32 import urlparse
33 import warnings 33 import warnings
34 import zlib 34 import zlib
35 35
36 # Ignore deprecation warnings, they make our output more cluttered. 36 # Ignore deprecation warnings, they make our output more cluttered.
37 warnings.filterwarnings("ignore", category=DeprecationWarning) 37 warnings.filterwarnings("ignore", category=DeprecationWarning)
38 38
39 import echo_message
39 import pyftpdlib.ftpserver 40 import pyftpdlib.ftpserver
40 import tlslite 41 import tlslite
41 import tlslite.api 42 import tlslite.api
42 43
43 try: 44 try:
44 import hashlib 45 import hashlib
45 _new_md5 = hashlib.md5 46 _new_md5 = hashlib.md5
46 except ImportError: 47 except ImportError:
47 import md5 48 import md5
48 _new_md5 = md5.new 49 _new_md5 = md5.new
(...skipping 1438 matching lines...) Expand 10 before | Expand all | Expand 10 after
1487 1488
1488 1489
1489 class TCPEchoHandler(SocketServer.BaseRequestHandler): 1490 class TCPEchoHandler(SocketServer.BaseRequestHandler):
1490 """The RequestHandler class for TCP echo server. 1491 """The RequestHandler class for TCP echo server.
1491 1492
1492 It is instantiated once per connection to the server, and overrides the 1493 It is instantiated once per connection to the server, and overrides the
1493 handle() method to implement communication to the client. 1494 handle() method to implement communication to the client.
1494 """ 1495 """
1495 1496
1496 def handle(self): 1497 def handle(self):
1497 data = self.request.recv(65536) 1498 """Handles the request from the client and responds back with a response."""
1498 if not data: 1499
1500 data = self.request.recv(65536).strip()
1501 # Verify the "echo request" message received from the client. Send back
1502 # "echo response" message if "echo request" message is valid.
1503 try:
1504 return_data = echo_message.GetEchoResponseData(data)
1505 if not return_data:
1499 return 1506 return
1500 self.request.send(data) 1507 except ValueError:
1508 return
1509
1510 self.request.send(return_data)
1501 1511
1502 1512
1503 class UDPEchoHandler(SocketServer.BaseRequestHandler): 1513 class UDPEchoHandler(SocketServer.BaseRequestHandler):
1504 """The RequestHandler class for UDP echo server. 1514 """The RequestHandler class for UDP echo server.
1505 1515
1506 It is instantiated once per connection to the server, and overrides the 1516 It is instantiated once per connection to the server, and overrides the
1507 handle() method to implement communication to the client. 1517 handle() method to implement communication to the client.
1508 """ 1518 """
1509 1519
1510 def handle(self): 1520 def handle(self):
1521 """Handles the request from the client and responds back with a response."""
1522
1511 data = self.request[0].strip() 1523 data = self.request[0].strip()
1512 socket = self.request[1] 1524 socket = self.request[1]
1513 socket.sendto(data, self.client_address) 1525 # Verify the "echo request" message received from the client. Send back
1526 # "echo response" message if "echo request" message is valid.
1527 try:
1528 return_data = echo_message.GetEchoResponseData(data)
1529 if not return_data:
1530 return
1531 except ValueError:
1532 return
1533 socket.sendto(return_data, self.client_address)
1514 1534
1515 1535
1516 class FileMultiplexer: 1536 class FileMultiplexer:
1517 def __init__(self, fd1, fd2) : 1537 def __init__(self, fd1, fd2) :
1518 self.__fd1 = fd1 1538 self.__fd1 = fd1
1519 self.__fd2 = fd2 1539 self.__fd2 = fd2
1520 1540
1521 def __del__(self) : 1541 def __del__(self) :
1522 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: 1542 if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr:
1523 self.__fd1.close() 1543 self.__fd1.close()
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1570 server._device_management_handler = None 1590 server._device_management_handler = None
1571 server.policy_keys = options.policy_keys 1591 server.policy_keys = options.policy_keys
1572 server.policy_user = options.policy_user 1592 server.policy_user = options.policy_user
1573 elif options.server_type == SERVER_SYNC: 1593 elif options.server_type == SERVER_SYNC:
1574 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler) 1594 server = SyncHTTPServer(('127.0.0.1', port), SyncPageHandler)
1575 print 'Sync HTTP server started on port %d...' % server.server_port 1595 print 'Sync HTTP server started on port %d...' % server.server_port
1576 print 'Sync XMPP server started on port %d...' % server.xmpp_port 1596 print 'Sync XMPP server started on port %d...' % server.xmpp_port
1577 server_data['port'] = server.server_port 1597 server_data['port'] = server.server_port
1578 server_data['xmpp_port'] = server.xmpp_port 1598 server_data['xmpp_port'] = server.xmpp_port
1579 elif options.server_type == SERVER_TCP_ECHO: 1599 elif options.server_type == SERVER_TCP_ECHO:
1600 # Used for generating the key (randomly) that encrypts the "echo request"
1601 # message.
1602 random.seed()
1580 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler) 1603 server = TCPEchoServer(('127.0.0.1', port), TCPEchoHandler)
1581 print 'Echo TCP server started on port %d...' % server.server_port 1604 print 'Echo TCP server started on port %d...' % server.server_port
1582 server_data['port'] = server.server_port 1605 server_data['port'] = server.server_port
1583 elif options.server_type == SERVER_UDP_ECHO: 1606 elif options.server_type == SERVER_UDP_ECHO:
1607 # Used for generating the key (randomly) that encrypts the "echo request"
1608 # message.
1609 random.seed()
1584 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler) 1610 server = UDPEchoServer(('127.0.0.1', port), UDPEchoHandler)
1585 print 'Echo UDP server started on port %d...' % server.server_port 1611 print 'Echo UDP server started on port %d...' % server.server_port
1586 server_data['port'] = server.server_port 1612 server_data['port'] = server.server_port
1587 # means FTP Server 1613 # means FTP Server
1588 else: 1614 else:
1589 my_data_dir = MakeDataDir() 1615 my_data_dir = MakeDataDir()
1590 1616
1591 # Instantiate a dummy authorizer for managing 'virtual' users 1617 # Instantiate a dummy authorizer for managing 'virtual' users
1592 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() 1618 authorizer = pyftpdlib.ftpserver.DummyAuthorizer()
1593 1619
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1700 'random key if none is specified on the command ' 1726 'random key if none is specified on the command '
1701 'line.') 1727 'line.')
1702 option_parser.add_option('', '--policy-user', default='user@example.com', 1728 option_parser.add_option('', '--policy-user', default='user@example.com',
1703 dest='policy_user', 1729 dest='policy_user',
1704 help='Specify the user name the server should ' 1730 help='Specify the user name the server should '
1705 'report back to the client as the user owning the ' 1731 'report back to the client as the user owning the '
1706 'token used for making the policy request.') 1732 'token used for making the policy request.')
1707 options, args = option_parser.parse_args() 1733 options, args = option_parser.parse_args()
1708 1734
1709 sys.exit(main(options, args)) 1735 sys.exit(main(options, args))
OLDNEW
« net/tools/testserver/echo_message.py ('K') | « net/tools/testserver/echo_message.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698