import
serial
import
re
import
base64
class
SerialTool(
object
):
def
__init__(
self
, portx
=
"COM5"
, bps
=
115200
, timeout
=
1
):
try
:
self
.p
=
serial.Serial(portx, bps, timeout
=
timeout)
self
.rn
=
self
.getRN()
except
Exception as e:
print
(e)
self
.rn
=
b
"\n"
def
getRN(
self
):
rn
=
b
"\n"
self
.p.write(rn)
data
=
self
.p.readall()
prompt
=
data.split(rn)[
1
]
if
b
"\n"
+
prompt
=
=
data:
rn
=
b
"\n"
elif
b
"\r\n"
+
prompt
=
=
data:
rn
=
b
"\r\n"
return
rn
def
exec
(
self
, cmd
=
b
"pwd"
):
if
isinstance
(cmd,
str
):
cmd
=
cmd.encode()
self
.p.write(cmd
+
b
"\n"
)
data
=
self
.p.readall()
print
(data.decode())
def
downloadFileByHexdump(
self
, rfile
=
"/etc/passwd"
, outfile
=
""):
if
not
outfile:
outfile
=
rfile.split(
"/"
)[
-
1
]
file_size
=
self
.getFileSize(rfile)
self
.p.write(
"hexdump {rfile}\n"
.
format
(rfile
=
rfile).encode())
data
=
self
.p.readall()
self
.writeFile(outfile
+
".hex"
, data)
content
=
self
.rn.join(data.split(
self
.rn)[
1
:
-
1
])
file_data
=
self
.decodeHexdump(content)
if
isinstance
(file_size,
int
):
file_data
=
file_data[
0
:file_size]
self
.writeFile(outfile, file_data)
def
decodeHexdump(
self
, content, byteorder
=
"little"
):
lst
=
content.split(b
"\n"
)
flag_star
=
False
pre_addr
=
0
cur_addr
=
0
data
=
b""
for
line
in
lst:
if
b
"*"
not
in
line:
r
=
line.strip().split()
cur_addr
=
int
(r[
0
],
16
)
if
flag_star:
data
+
=
int
(
0
).to_bytes(
1
, byteorder)
*
(cur_addr
-
pre_addr
-
16
)
flag_star
=
False
for
h
in
r[
1
:]:
n
=
int
(
len
(h)
/
2
)
data
+
=
int
(h,
16
).to_bytes(n, byteorder)
pre_addr
=
cur_addr
else
:
flag_star
=
True
return
data
def
downloadFileByBase64(
self
, rfile
=
"/etc/passwd"
, outfile
=
""):
if
not
outfile:
outfile
=
rfile.split(
"/"
)[
-
1
]
self
.p.write(
"cat {rfile} |base64\n"
.
format
(rfile
=
rfile).encode())
data
=
self
.p.readall()
self
.writeFile(outfile
+
".base64"
, data)
content
=
self
.rn.join(data.split(
self
.rn)[
1
:
-
1
])
file_data
=
self
.decodeBase64(content)
self
.writeFile(outfile, file_data)
def
decodeBase64(
self
, content):
content
=
content.replace(b
"\r"
, b"
").replace(b"
\n
", b"
")
file_data
=
base64.b64decode(content)
return
file_data
def
uploadFileByBase64(
self
, lfile
=
"a.txt"
, rfile
=
None
):
if
not
rfile:
print
(
"rfile=None"
)
exit(
1
)
data
=
self
.readFile(lfile)
print
()
self
.p.write(
"cat <<EOF > {rfile}.tmpfile\n"
.
format
(rfile
=
rfile).encode()
+
base64.b64encode(data)
+
b
"\nEOF\n"
)
ignor_data
=
self
.p.readall()
self
.p.write(
"cat {rfile}.tmpfile | base64 -d > {rfile}\n"
.
format
(rfile
=
rfile).encode())
ignor_data
=
self
.p.readall()
self
.p.write(
"rm -f {rfile}.tmpfile\n"
.
format
(rfile
=
rfile).encode())
ignor_data
=
self
.p.readall()
def
close(
self
):
self
.p.close()
def
getFileSize(
self
, rfile):
try
:
self
.p.write(
"ls -l {rfile}\n"
.
format
(rfile
=
rfile).encode())
data
=
self
.p.readall()
filter
=
re.
compile
(b
"[rwx-]+[\s]+\d+[\s]+[\S]+[\s]+[\S]+[\s]+(\d+)[\s]+"
)
res
=
filter
.findall(data)
if
res:
size
=
int
(res[
0
])
return
size
except
Exception as e:
print
(e)
def
writeFile(
self
,
file
, data):
fout
=
open
(
file
,
"wb"
)
fout.write(data)
fout.close()
def
readFile(
self
,
file
):
fout
=
open
(
file
,
"rb"
)
data
=
fout.read()
fout.close()
return
data
def
test(
self
):
data
=
self
.readFile(
file
=
"cert.pem.base64"
)
outfile
=
"test.tmp"
content
=
self
.rn.join(data.split(
self
.rn)[
1
:
-
1
])
self
.writeFile(outfile,
self
.decodeBase64(content))
def
main():
m
=
SerialTool(portx
=
"COM6"
, bps
=
115200
, timeout
=
1
)
m.uploadFileByBase64(
"a.txt"
,
"/tmp/a.txt"
)
if
"__main__"
=
=
__name__:
main()