C++ iostream、ostream、istream等标准库都是什么?看完这篇就知道了

article/2025/10/10 0:33:58

目录

  • `iostream` 库
    • 组成
      • 基本类模板
      • 类模板实例
      • 标准对象
      • 类型
      • 操纵符
    • 源码
  • `ostream` 库
  • `istream` 库
  • `fstream` 库
  • `ios` 库

我们在写C++代码的时候,总会用到 iostream 库,这是C++的标准库,几乎每个程序都会用到它(有一些人则会用 cstdio )。我们细扒出来 iostream 库的源码,却发现 iostream 库里面几乎都是 include 、预处理、 externnamespace 这些东西,其中还有引入 iosostreamistreamstreambuf 等头文件。这又是什么呢?本文就为大家揭秘一下。首先先说最常用的 iostream 库。

iostream

iostream 是指 iostream 库。iostream 的意思是输入输出流,直接点说就是 in(输入) out(输出) stream(流),取in、out的首字母与stream合成。

组成

iostream 库的基础是两种命名为 istreamostream 的类型,分别表示输入流和输出流。流是指要从某种 IO 设备上读出或写入的字符序列。术语“流”试图说明字符是随着时间顺序生成或消耗的。

标准库定义了 4 个 IO 对象。处理输入时使用命名为 cin(读作 see-in )的 istream 类型对象。这个对象也称为标准输入。处理输出时使用命名为 cout(读作 see-out )的 ostream 类型对象,这个对象也称为标准输出。标准库还定义了另外两个 ostream 对象,分别命名为 cerrclog(分别读作 see-errsee-log )。cerr 对象又叫作标准错误,通常用来输出警告和错误信息给程序的使用者。而 clog 对象用于产生程序执行的一般信息。

基本类模板

iostream.h )库的基础是类模板的层级结构。类模板以一种与类型无关的方式,提供了这个库的大部分功能。

基本类模板是一个类模板的集合,其中每个类模板有两个参数:字符类型(charT)参数决定处理的元素类型,而特性参数 对每个特定的元素类型提供一些额外的特征。

这个类层级结构中的类模板和它们的字符类型的实例相比,名字前面多了前缀 basic_ 。例如,istream 由之实例化而来的类模板名为 basic_istreamfstream 由之实例化而来的类模板名为 basic_fstream ,等等。唯一的例外是 ios_base ,因为它本身就是类型无关的,所以它并不基于任何类模板,而是一个正规类。

类模板实例

名称和关系

iostream.h )库中集成了两组标准的整个 iostream 类模板层级结构的实例:一组是面向单字节的,处理 char 类型的元素;另一组是面向宽字节的,处理 wchar_t 类型的元素。
面向单字节( char 型)的实例可能是 iostream.h )库更为人所知的一部分。iosistreamofstream 等类都是面向单字节的。上图是面向单字节的所有类的名称和关系。面向宽字节( wchar_t 型)的实例的命名规则与面向单字节的实例相同,但所有类和对象名称前有前缀 w ,例如 wioswistreamwofstream

标准对象

作为 iostream.h )库的一部分,头文件声明了一些用来在标准输入输出设备上进行输入输出操作的对象。
这些对象分为两组:面向单字节的,即常见的 cincoutcerrclog ;其面向宽字节的对应物,声明为 wcinwcoutwcerrwclog

类型

iostream.h )库中的类很少对其成员的原型使用基本类型,而是通常使用根据其实例的特性定义的类型。对默认的 charwchar_t 型的实例,类型 streamposstreamoffstreamsize分别用以表示位置、偏移和大小。

操纵符

操纵符是用来与对流对象进行操作的插入( << )和提取( >> )运算符一同使用的全局函数。它们通常变更流的属性和格式设置。endlhexscientific 是一些操纵符的例子。

源码

这是 iostream 库的源码:

 
// -*- C++ -*-
//===--------------------------- iostream ---------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//#ifndef _LIBCPP_IOSTREAM
#define _LIBCPP_IOSTREAM#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>namespace std {extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;}  // std#include <__config>
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif_LIBCPP_BEGIN_NAMESPACE_STD#ifndef _LIBCPP_HAS_NO_STDIN
extern _LIBCPP_FUNC_VIS istream cin;
extern _LIBCPP_FUNC_VIS wistream wcin;
#endif
#ifndef _LIBCPP_HAS_NO_STDOUT
extern _LIBCPP_FUNC_VIS ostream cout;
extern _LIBCPP_FUNC_VIS wostream wcout;
#endif
extern _LIBCPP_FUNC_VIS ostream cerr;
extern _LIBCPP_FUNC_VIS wostream wcerr;
extern _LIBCPP_FUNC_VIS ostream clog;
extern _LIBCPP_FUNC_VIS wostream wclog;_LIBCPP_END_NAMESPACE_STD#endif  // _LIBCPP_IOSTREAM

ostream

说完了 iostream 库,我们再来看看 ostream 库。上面已经提到过,ostream 是专为窄字节设计的输出库。这是 ostream 库的源码,比较长:

// Output streams -*- C++ -*-// Copyright (C) 1997-2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>./** @file include/ostream*  This is a Standard C++ Library header.*///
// ISO C++ 14882: 27.6.2  Output streams
//#ifndef _GLIBCXX_OSTREAM
#define _GLIBCXX_OSTREAM 1#pragma GCC system_header#include <ios>
#include <bits/ostream_insert.h>namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION/***  @brief  Template class basic_ostream.*  @ingroup io**  @tparam _CharT  Type of character stream.*  @tparam _Traits  Traits for character type, defaults to*                   char_traits<_CharT>.**  This is the base class for all output streams.  It provides text*  formatting of all builtin types, and communicates with any class*  derived from basic_streambuf to do the actual output.*/template<typename _CharT, typename _Traits>class basic_ostream : virtual public basic_ios<_CharT, _Traits>{public:// Types (inherited from basic_ios):typedef _CharT			 		char_type;typedef typename _Traits::int_type 		int_type;typedef typename _Traits::pos_type 		pos_type;typedef typename _Traits::off_type 		off_type;typedef _Traits			 		traits_type;// Non-standard Types:typedef basic_streambuf<_CharT, _Traits> 		__streambuf_type;typedef basic_ios<_CharT, _Traits>		__ios_type;typedef basic_ostream<_CharT, _Traits>		__ostream_type;typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >__num_put_type;typedef ctype<_CharT>	      			__ctype_type;/***  @brief  Base constructor.**  This ctor is almost never called by the user directly, rather from*  derived classes' initialization lists, which pass a pointer to*  their own stream buffer.*/explicitbasic_ostream(__streambuf_type* __sb){ this->init(__sb); }/***  @brief  Base destructor.**  This does very little apart from providing a virtual base dtor.*/virtual~basic_ostream() { }/// Safe prefix/suffix operations.class sentry;friend class sentry;//@{/***  @brief  Interface for manipulators.**  Manipulators such as @c std::endl and @c std::hex use these*  functions in constructs like "std::cout << std::endl".  For more*  information, see the iomanip header.*/__ostream_type&operator<<(__ostream_type& (*__pf)(__ostream_type&)){// _GLIBCXX_RESOLVE_LIB_DEFECTS// DR 60. What is a formatted input function?// The inserters for manipulators are *not* formatted output functions.return __pf(*this);}__ostream_type&operator<<(__ios_type& (*__pf)(__ios_type&)){// _GLIBCXX_RESOLVE_LIB_DEFECTS// DR 60. What is a formatted input function?// The inserters for manipulators are *not* formatted output functions.__pf(*this);return *this;}__ostream_type&operator<<(ios_base& (*__pf) (ios_base&)){// _GLIBCXX_RESOLVE_LIB_DEFECTS// DR 60. What is a formatted input function?// The inserters for manipulators are *not* formatted output functions.__pf(*this);return *this;}//@}//@{/***  @name Inserters**  All the @c operator<< functions (aka <em>formatted output*  functions</em>) have some common behavior.  Each starts by*  constructing a temporary object of type std::basic_ostream::sentry.*  This can have several effects, concluding with the setting of a*  status flag; see the sentry documentation for more.**  If the sentry status is good, the function tries to generate*  whatever data is appropriate for the type of the argument.**  If an exception is thrown during insertion, ios_base::badbit*  will be turned on in the stream's error state without causing an*  ios_base::failure to be thrown.  The original exception will then*  be rethrown.*///@{/***  @brief Integer arithmetic inserters*  @param  __n A variable of builtin integral type.*  @return  @c *this if successful**  These functions use the stream's current locale (specifically, the*  @c num_get facet) to perform numeric formatting.*/__ostream_type&operator<<(long __n){ return _M_insert(__n); }__ostream_type&operator<<(unsigned long __n){ return _M_insert(__n); }__ostream_type&operator<<(bool __n){ return _M_insert(__n); }__ostream_type&operator<<(short __n);__ostream_type&operator<<(unsigned short __n){// _GLIBCXX_RESOLVE_LIB_DEFECTS// 117. basic_ostream uses nonexistent num_put member functions.return _M_insert(static_cast<unsigned long>(__n));}__ostream_type&operator<<(int __n);__ostream_type&operator<<(unsigned int __n){// _GLIBCXX_RESOLVE_LIB_DEFECTS// 117. basic_ostream uses nonexistent num_put member functions.return _M_insert(static_cast<unsigned long>(__n));}#ifdef _GLIBCXX_USE_LONG_LONG__ostream_type&operator<<(long long __n){ return _M_insert(__n); }__ostream_type&operator<<(unsigned long long __n){ return _M_insert(__n); }
#endif//@}//@{/***  @brief  Floating point arithmetic inserters*  @param  __f A variable of builtin floating point type.*  @return  @c *this if successful**  These functions use the stream's current locale (specifically, the*  @c num_get facet) to perform numeric formatting.*/__ostream_type&operator<<(double __f){ return _M_insert(__f); }__ostream_type&operator<<(float __f){// _GLIBCXX_RESOLVE_LIB_DEFECTS// 117. basic_ostream uses nonexistent num_put member functions.return _M_insert(static_cast<double>(__f));}__ostream_type&operator<<(long double __f){ return _M_insert(__f); }//@}/***  @brief  Pointer arithmetic inserters*  @param  __p A variable of pointer type.*  @return  @c *this if successful**  These functions use the stream's current locale (specifically, the*  @c num_get facet) to perform numeric formatting.*/__ostream_type&operator<<(const void* __p){ return _M_insert(__p); }#if __cplusplus >= 201703L__ostream_type&operator<<(nullptr_t){ return *this << "nullptr"; }
#endif/***  @brief  Extracting from another streambuf.*  @param  __sb  A pointer to a streambuf**  This function behaves like one of the basic arithmetic extractors,*  in that it also constructs a sentry object and has the same error*  handling behavior.**  If @p __sb is NULL, the stream will set failbit in its error state.**  Characters are extracted from @p __sb and inserted into @c *this*  until one of the following occurs:**  - the input stream reaches end-of-file,*  - insertion into the output sequence fails (in this case, the*    character that would have been inserted is not extracted), or*  - an exception occurs while getting a character from @p __sb, which*    sets failbit in the error state**  If the function inserts no characters, failbit is set.*/__ostream_type&operator<<(__streambuf_type* __sb);//@}//@{/***  @name Unformatted Output Functions**  All the unformatted output functions have some common behavior.*  Each starts by constructing a temporary object of type*  std::basic_ostream::sentry.  This has several effects, concluding*  with the setting of a status flag; see the sentry documentation*  for more.**  If the sentry status is good, the function tries to generate*  whatever data is appropriate for the type of the argument.**  If an exception is thrown during insertion, ios_base::badbit*  will be turned on in the stream's error state.  If badbit is on in*  the stream's exceptions mask, the exception will be rethrown*  without completing its actions.*//***  @brief  Simple insertion.*  @param  __c  The character to insert.*  @return  *this**  Tries to insert @p __c.**  @note  This function is not overloaded on signed char and*         unsigned char.*/__ostream_type&put(char_type __c);/***  @brief  Core write functionality, without sentry.*  @param  __s  The array to insert.*  @param  __n  Maximum number of characters to insert.*/void_M_write(const char_type* __s, streamsize __n){const streamsize __put = this->rdbuf()->sputn(__s, __n);if (__put != __n)this->setstate(ios_base::badbit);}/***  @brief  Character string insertion.*  @param  __s  The array to insert.*  @param  __n  Maximum number of characters to insert.*  @return  *this**  Characters are copied from @p __s and inserted into the stream until*  one of the following happens:**  - @p __n characters are inserted*  - inserting into the output sequence fails (in this case, badbit*    will be set in the stream's error state)**  @note  This function is not overloaded on signed char and*         unsigned char.*/__ostream_type&write(const char_type* __s, streamsize __n);//@}/***  @brief  Synchronizing the stream buffer.*  @return  *this**  If @c rdbuf() is a null pointer, changes nothing.**  Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,*  sets badbit.*/__ostream_type&flush();/***  @brief  Getting the current write position.*  @return  A file position object.**  If @c fail() is not false, returns @c pos_type(-1) to indicate*  failure.  Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).*/pos_typetellp();/***  @brief  Changing the current write position.*  @param  __pos  A file position object.*  @return  *this**  If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos).  If*  that function fails, sets failbit.*/__ostream_type&seekp(pos_type);/***  @brief  Changing the current write position.*  @param  __off  A file offset object.*  @param  __dir  The direction in which to seek.*  @return  *this**  If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).*  If that function fails, sets failbit.*/__ostream_type&seekp(off_type, ios_base::seekdir);protected:basic_ostream(){ this->init(0); }#if __cplusplus >= 201103L// Non-standard constructor that does not call init()basic_ostream(basic_iostream<_CharT, _Traits>&) { }basic_ostream(const basic_ostream&) = delete;basic_ostream(basic_ostream&& __rhs): __ios_type(){ __ios_type::move(__rhs); }// 27.7.3.3 Assign/swapbasic_ostream& operator=(const basic_ostream&) = delete;basic_ostream&operator=(basic_ostream&& __rhs){swap(__rhs);return *this;}voidswap(basic_ostream& __rhs){ __ios_type::swap(__rhs); }
#endiftemplate<typename _ValueT>__ostream_type&_M_insert(_ValueT __v);};/***  @brief  Performs setup work for output streams.**  Objects of this class are created before all of the standard*  inserters are run.  It is responsible for <em>exception-safe prefix and*  suffix operations</em>.*/template <typename _CharT, typename _Traits>class basic_ostream<_CharT, _Traits>::sentry{// Data Members.bool 				_M_ok;basic_ostream<_CharT, _Traits>& 	_M_os;public:/***  @brief  The constructor performs preparatory work.*  @param  __os  The output stream to guard.**  If the stream state is good (@a __os.good() is true), then if the*  stream is tied to another output stream, @c is.tie()->flush()*  is called to synchronize the output sequences.**  If the stream state is still good, then the sentry state becomes*  true (@a okay).*/explicitsentry(basic_ostream<_CharT, _Traits>& __os);#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"/***  @brief  Possibly flushes the stream.**  If @c ios_base::unitbuf is set in @c os.flags(), and*  @c std::uncaught_exception() is true, the sentry destructor calls*  @c flush() on the output stream.*/~sentry(){// XXX MTif (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception()){// Can't call flush directly or else will get into recursive lock.if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)_M_os.setstate(ios_base::badbit);}}
#pragma GCC diagnostic pop/***  @brief  Quick status checking.*  @return  The sentry state.**  For ease of use, sentries may be converted to booleans.  The*  return value is that of the sentry state (true == okay).*/
#if __cplusplus >= 201103Lexplicit
#endifoperator bool() const{ return _M_ok; }};//@{/***  @brief  Character inserters*  @param  __out  An output stream.*  @param  __c  A character.*  @return  out**  Behaves like one of the formatted arithmetic inserters described in*  std::basic_ostream.  After constructing a sentry object with good*  status, this function inserts a single character and any required*  padding (as determined by [22.2.2.2.2]).  @c __out.width(0) is then*  called.**  If @p __c is of type @c char and the character type of the stream is not*  @c char, the character is widened before insertion.*/template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c){ return __ostream_insert(__out, &__c, 1); }template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&operator<<(basic_ostream<_CharT, _Traits>& __out, char __c){ return (__out << __out.widen(__c)); }// Specializationtemplate <class _Traits>inline basic_ostream<char, _Traits>&operator<<(basic_ostream<char, _Traits>& __out, char __c){ return __ostream_insert(__out, &__c, 1); }// Signed and unsignedtemplate<class _Traits>inline basic_ostream<char, _Traits>&operator<<(basic_ostream<char, _Traits>& __out, signed char __c){ return (__out << static_cast<char>(__c)); }template<class _Traits>inline basic_ostream<char, _Traits>&operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c){ return (__out << static_cast<char>(__c)); }//@}//@{/***  @brief  String inserters*  @param  __out  An output stream.*  @param  __s  A character string.*  @return  out*  @pre  @p __s must be a non-NULL pointer**  Behaves like one of the formatted arithmetic inserters described in*  std::basic_ostream.  After constructing a sentry object with good*  status, this function inserts @c traits::length(__s) characters starting*  at @p __s, widened if necessary, followed by any required padding (as*  determined by [22.2.2.2.2]).  @c __out.width(0) is then called.*/template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s){if (!__s)__out.setstate(ios_base::badbit);else__ostream_insert(__out, __s,static_cast<streamsize>(_Traits::length(__s)));return __out;}template<typename _CharT, typename _Traits>basic_ostream<_CharT, _Traits> &operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);// Partial specializationstemplate<class _Traits>inline basic_ostream<char, _Traits>&operator<<(basic_ostream<char, _Traits>& __out, const char* __s){if (!__s)__out.setstate(ios_base::badbit);else__ostream_insert(__out, __s,static_cast<streamsize>(_Traits::length(__s)));return __out;}// Signed and unsignedtemplate<class _Traits>inline basic_ostream<char, _Traits>&operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s){ return (__out << reinterpret_cast<const char*>(__s)); }template<class _Traits>inline basic_ostream<char, _Traits> &operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s){ return (__out << reinterpret_cast<const char*>(__s)); }//@}// Standard basic_ostream manipulators/***  @brief  Write a newline and flush the stream.**  This manipulator is often mistakenly used when a simple newline is*  desired, leading to poor buffering performance.  See*  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering*  for more on this subject.*/template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&endl(basic_ostream<_CharT, _Traits>& __os){ return flush(__os.put(__os.widen('\n'))); }/***  @brief  Write a null character into the output sequence.**  <em>Null character</em> is @c CharT() by definition.  For CharT*  of @c char, this correctly writes the ASCII @c NUL character*  string terminator.*/template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&ends(basic_ostream<_CharT, _Traits>& __os){ return __os.put(_CharT()); }/***  @brief  Flushes the output stream.**  This manipulator simply calls the stream's @c flush() member function.*/template<typename _CharT, typename _Traits>inline basic_ostream<_CharT, _Traits>&flush(basic_ostream<_CharT, _Traits>& __os){ return __os.flush(); }#if __cplusplus >= 201103Ltemplate<typename _Ch, typename _Up>basic_ostream<_Ch, _Up>&__is_convertible_to_basic_ostream_test(basic_ostream<_Ch, _Up>*);template<typename _Tp, typename = void>struct __is_convertible_to_basic_ostream_impl{using __ostream_type = void;};template<typename _Tp>using __do_is_convertible_to_basic_ostream_impl =decltype(__is_convertible_to_basic_ostream_test(declval<typename remove_reference<_Tp>::type*>()));template<typename _Tp>struct __is_convertible_to_basic_ostream_impl<_Tp,__void_t<__do_is_convertible_to_basic_ostream_impl<_Tp>>>{using __ostream_type =__do_is_convertible_to_basic_ostream_impl<_Tp>;};template<typename _Tp>struct __is_convertible_to_basic_ostream: __is_convertible_to_basic_ostream_impl<_Tp>{public:using type = __not_<is_void<typename __is_convertible_to_basic_ostream_impl<_Tp>::__ostream_type>>;constexpr static bool value = type::value;};template<typename _Ostream, typename _Tp, typename = void>struct __is_insertable : false_type {};template<typename _Ostream, typename _Tp>struct __is_insertable<_Ostream, _Tp,__void_t<decltype(declval<_Ostream&>()<< declval<const _Tp&>())>>: true_type {};template<typename _Ostream>using __rvalue_ostream_type =typename __is_convertible_to_basic_ostream<_Ostream>::__ostream_type;/***  @brief  Generic inserter for rvalue stream*  @param  __os  An input stream.*  @param  __x  A reference to the object being inserted.*  @return  os**  This is just a forwarding function to allow insertion to*  rvalue streams since they won't bind to the inserter functions*  that take an lvalue reference.*/template<typename _Ostream, typename _Tp>inlinetypename enable_if<__and_<__not_<is_lvalue_reference<_Ostream>>,__is_convertible_to_basic_ostream<_Ostream>,__is_insertable<__rvalue_ostream_type<_Ostream>,const _Tp&>>::value,__rvalue_ostream_type<_Ostream>>::typeoperator<<(_Ostream&& __os, const _Tp& __x){__rvalue_ostream_type<_Ostream> __ret_os = __os;__ret_os << __x;return __ret_os;}
#endif // C++11_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std#include <bits/ostream.tcc>#endif	/* _GLIBCXX_OSTREAM */

可以看到有700多行。非常多。

istream

istream 库是窄字节输入库。由于代码实在找不到,这里就不放代码了。。。

fstream

对文件进行操作的头文件。

ios

你可以理解为以上那些库(这还没说全,比如 ofstreamwistream 等,太多了)的总共的一个库,就像 iostream 是Java,ios 库是Oracle一样的。注意,这不是进行iOS开发的库,很多人都理解错了。

本文完。整理不易,希望大家支持。


http://chatgpt.dhexx.cn/article/UtmM9LEh.shtml

相关文章

C++之 ostream详细用法

概述 在 C中&#xff0c;ostream表示输出流&#xff0c;英文”output stream“的简称。在 C中常见的输出流对象就是标准输出流cout&#xff0c;很少自定义ostream的对象&#xff0c;更多的是直接使用cout。那么 ostream 有什么用呢&#xff0c;来看一个场景&#xff1a; clas…

Simple_SSTI_1

Simple_SSTI_1&#xff1a; 打开题目 查看一下网页源代码 flag在secret_key下 以get方式传递参数flag{{config.SECRET_KEY}}即得到flag。 也就是114.67.246.176.12930/?flag{{config.SECRET_KEY}} SECRET_KEY是config配置里面的一个值 告自己&#xff1a;要先截图再提交flag…

SSTI(模板注入) 解析 和 ctf 做法

基础知识补充&#xff1a; __class__ 返回类型所属的对象__mro__ 返回一个包含对象所继承的基类元组&#xff0c;方法在解析时按照元组的顺序解析。__base__ 返回该对象所继承的基类// __base__和__mro__都是用来寻找基类的__subclasses__ 每个新类都保留了子类的引用&#xff…

SSTI注入————php的SSTI

SSTI 就是服务器端模板注入&#xff08;Server-Side Template Injection&#xff09; ​ 当前使用的一些框架&#xff0c;比如python的flask&#xff0c;php的tp&#xff0c;java的spring等一般都采用成熟的的MVC的模式&#xff0c;用户的输入先进入Controller控制器&#xff0…

SSTI

模板注入在py2和py3中有些不同&#xff0c;但是没有本质上的区别。 模板注入的流程&#xff1a;找到父类<type ‘object’>–>寻找子类–>找关于命令执行或者文件操作的模块。 几个魔术方法&#xff1a; class 返回类型所属的对象 mro 返回一个包含对象所继承的基类…

SSTI模板注入及绕过姿势(基于Python-Jinja2)

前言&#xff1a;​SSTI&#xff08;服务端模板注入&#xff09;&#xff0c;已然不再是一个新话题&#xff0c;近年来的CTF中还是也经常能遇到的&#xff0c;比如护网杯的easy_tonado、TWCTF的Shrine&#xff0c;19年的SCTF也出了Ruby ERB SSTI的考点&#xff1b;本篇对这部分…

SSTI (服务器模板注入)

先来一波flask ssti漏洞的代码。 #python3 #Flask version:0.12.2 #Jinja2: 2.10 from flask import Flask, request from jinja2 import Template app Flask(__name__) app.route("/") def index():name request.args.get(name, guest)t Template("Hello &…

初步认识SSTI

SSTI简介 SSTI&#xff0c;即服务端模板注入&#xff0c;起因是服务端接收了用户的输入&#xff0c;将其作为 Web 应用模板内容的一部分&#xff0c;在进行目标编译渲染的过程中&#xff0c;执行了用户插入的恶意内容&#xff0c;从而导致各种各样的问题。 Python SSTI(flask…

ISCC SSTI

先找参数吧&#xff0c;通过信息搜集&#xff0c;参数是xiaodouni 就是小豆泥的英文&#xff0c;这个是暹罗猫的一个名字吧 然后直接放两个payload的吧 看不懂的可以看一下我以前的文章CTFshow ssti里面讲了思路&#xff0c;这里就不再解释了。 {%set pp(dict(popa))|join%} …

SSTI 学习笔记

PHP SSTI(Twig) 学习文章 进入环境&#xff0c;左上角有flag,hint 都检查看看 flag页面显示ip&#xff0c;hint页面源代码有提示 考虑XFF头或者referer头 测试一下 注&#xff1a;这里不用加上“&#xff1b;” 出来了 python flask ssti 学习文章 原理&#xff1a;因为对输…

浅学Go下的ssti

前言 作为强类型的静态语言&#xff0c;golang的安全属性从编译过程就能够避免大多数安全问题&#xff0c;一般来说也唯有依赖库和开发者自己所编写的操作漏洞&#xff0c;才有可能形成漏洞利用点&#xff0c;在本文&#xff0c;主要学习探讨一下golang的一些ssti模板注入问题…

SSTI——java里的ssti

1.Velocity 2.FreeMarker 因为从来没接触过java语言 所以对这些也是基本上一窍不通 这里只简单的提及 不做具体介绍 会找一下题来做 但是没有找到有关java ssti的题目 confusion1 看一下描述 打开题目 没发现什么东西 但是 login register页面显示访问不成功 查看源代码找到…

详解SSTI模板注入

详解SSTI模板注入 SSTI简介常见的模板引擎PHPJAVAPYTHONRUBYGOLANG SSTI产生的原因常用检测工具 TplmapFlask/Jinja模板引擎的相关绕过Flask简介demo漏洞代码基础知识沙盒逃逸Python的内建函数名称空间类继承 寻找Python-SSTI攻击载荷的过程攻击载荷过程常用的目标函数常见的中…

web安全-SSTI模板注入漏洞

一.初识SSTI 1.什么是SSTI注入&#xff1f; SSTI模板注入(Server-Side Template Injection)&#xff0c;通过与服务端模板的输入输出交互&#xff0c;在过滤不严格的情况下&#xff0c;构造恶意输入数据&#xff0c;从而达到读取文件或者getshell的目的。 2.SSTI漏洞成因 ​…

BugKu:Simple_SSTI(SSTI模板注入)

目录 1.Simple_SSTI_1 2.Simple_SSTI_2 1.Simple_SSTI_1 点击链接进入&#xff0c;题目说&#xff1a; You need pass in a parameter named flag。(你需要传入一个名为flag的参数)然后我们可以直接f12查看&#xff0c;也可以右击页面--->“检查” 如图所示&#xff0c;我…

SSTI模板注入绕过(进阶篇)

文章目录 语法变量过滤器总结获取内置方法 以chr为例字符串构造获取键值或下标获取属性 下面的内容均以jinja2为例&#xff0c;根据官方文档来探寻绕过方法 文档链接 默认大家都已经可以利用没有任何过滤的模板注入 语法 官方文档对于模板的语法介绍如下 {% ... %} for State…

学习ssti

ssti也叫做模板注入 当不正确的使用模板引擎进行渲染时&#xff0c;则会造成模板注入 比如render_template_string函数&#xff0c;当参数可控时&#xff0c;会造成模板注入 在Python的ssti中&#xff0c;大部分是依靠基类->子类->危险函数的方式来利用ssti python沙…

Simple_SSTI_1与Simple_SSTI_2

目录 一&#xff0c;Simple_SSTI_1 二&#xff0c;Simple_SSTI_2 一&#xff0c;Simple_SSTI_1 首先打开场景&#xff1a; 然后F12查看一下源码&#xff1a; 于是通过百度相关知识寻找线索&#xff1a; 1&#xff0c;SSTI &#xff1a;服务器端模版注入是指攻击者能够使用本…

Flask SSTI漏洞介绍及利用

1.ssti成因 flask使用jinjia2渲染引擎进行网页渲染&#xff0c;当处理不得当&#xff0c;未进行语句过滤&#xff0c;用户输入{{控制语句}}&#xff0c;会导致渲染出恶意代码&#xff0c;形成注入。 2.使用render_template()渲染页面时不存在注入漏洞。 对传入的参数不会执行…

ssti小总结

漏洞简介 SSTI即服务端模版注入攻击。由于程序员代码编写不当&#xff0c;导致用户输入可以修改服务端模版的执行逻辑&#xff0c;从而造成XSS,任意文件读取&#xff0c;代码执行等一系列问题. 1. 几种常用于ssti的魔术方法 __class__ 返回类型所属的对象 __mro__ 返回一个…