Python101


加解密

HASH

import hashlib

# md5, sha1, sha224, sha256, sha384, sha512
s = 'hello world'
m = hashlib.md5()
m.update(s.encode('utf8'))
print(m.digest())
print(m.hexdigest())

# ripemd160, whirlpool
s = 'hello world'
m = hashlib.new('ripemd160', s.encode('utf8'))
print(m.digest())
print(m.hexdigest())

RSA gmpy2

import gmpy2

p = 168870409632549765296862502254899759857248288652407554850383477768823119986297924033151555409082356346297282197467254809081931208549224155851315637344747298357415893525472097154103238042019866682938382139834279447488738548244853184293595933654527554670457923545588565351501521501815389298997833861578226633099
q = 136505636991931352215759862754723380107419404176934963805672183755717752956589135238497377957828097441347957566489629211243389022288160648357324629288547621047463026266886900467398707294913322326802718447765393605735412855505214780439471729354761056917299208670362196028628829497574575055117449119342100056517
e = 65537

n = p * q
fn = (p - 1) * (q - 1)
d = gmpy2.invert(e, fn)

# encode
plain = "hello world"
cipher = gmpy2.powmod(int(plain.hex(),16), e, n)
print cipher

# decode
cipher = 17123126358168532314364171789745947147158203528255189528376034042576955961464108007241396193221317579386199055678187296416631757350036173529280264692509105557539380445658352098757386832691606291403716717255406122398828996166165137869000756490419628668837727171987655840934249710275220041791912411702429566338522067039808551028470065040909781794268938955092150084715883613062506445552253043511850366325327543440113474870896494812927181373067664361593089869517445577240126156141735331979758927134194252186254770372018574046630061371952143328299376196926784339615150604268752279279335534713614030849861471450015448322452
plaint = gmpy2.powmod(cipher, d, n)
s = '%x' % plaint
if len(s) % 2 != 0:
    s = '0' + s
print(s)

编码

中文转十六进制

字节数组与十六进制

二进制格式转换

[0xab, 0xbc, 0xcd, 0xde] -> '\xab\xbc\xcd\xde'

'\xde\xad\xbe\xef' -> [222, 173, 190, 239]

'\xab\xbc\xcd\xde' -> 'abbccdde'

'aabbccdd' -> '\xaa\xbb\xcc\xdd'

'\xde\xad\xbe\xef' -> 0xdeadbeef

0xdeadbeef -> '\xde\xad\xbe\xef'

ASCII 转 HEX

HEX 转 ASCII

base64 编解码

url safe 的 base64 编解码

base64 换表


数值转换


字符串转换


字符串处理

每个字符减 1

数组中每个字符与自身位数异或

十六进制不带 0x 补零 (ASCII 字节类型)

十六进制不带 0x 补零 (字符串类型)

字符串反转


声音

windows

linux


执行系统命令

os

commands


延时

sleep()


时间戳

time()

time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。


输出

python字符串去掉前缀b

需要去掉字符串的前缀b,只需要进行utf-8的转换即可,即


安全脚本

写 python 目录遍历 POC 时遇到的问题

  • https://mazinahmed.net/blog/testing-for-path-traversal-with-python/


Docker Engine SDK

运行一个容器,并执行容器中的一个命令

列出所有镜像


mysql

连接 Mysql 的 TESTDB 数据库

创建数据库表


oss2

  • https://help.aliyun.com/document_detail/32027.html

创建examplebucket存储空间

上传文件